我正在尝试使用 termstrc 来计算 nelson siegal parm 估计的时间序列。我试图让下面的代码运行,但我认为我的数据格式不正确。我不确定如何正确地将数据组织为“dyncouponbonds”类。我得到的错误是:
(pos_cf[i] + 1) 中的错误:pos_cf[i + 1] : NA/NaN 参数另外:警告消息:在 max(n_of_cf) 中:max 没有非缺失参数;返回-Inf
抱歉,如果我没有以正确的格式询问等。第一次使用 stackoverflow
importdatastream <- function(datafiles){
#datafiles = c("france S.csv", "france AC.csv", "france CP.csv")
rawdata <- read.csv(datafiles[1], dec=".",sep=",",colClasses = "character")
rawdata_AC <- read.csv(datafiles[2], dec=".",sep=",",colClasses = "character", header=TRUE, check.names=FALSE)
rawdata_CP <- read.csv(datafiles[3], dec=".",sep=",",colClasses = "character", header=TRUE, check.names=FALSE)
DATES <- as.character(as.Date(names(rawdata_AC)[-(1:2)], format="%d.%m.%y"))
DATES <- paste(substring(DATES,9,10),substring(DATES,6,7),substring(DATES,1,4),sep="")
AC <- rawdata[,"ACCRUED"]
CP <- rawdata[,"PRICE"]
dslist <- list()
for(i in 1:length(DATES)){
TODAY <- DATES[i]
AC <- rawdata_AC[-(1:2)][,i]
CP <- rawdata_CP[-(1:2)][,i]
datastreamlist <- function(rawdata, TODAY, AC, CP){
data <- list()
data$ISIN <- rawdata[,"ISIN"]
data$MATURITYDATE <- as.Date(rawdata[,"MATURITYDATE"],format="%d%m%Y")
data$ISSUEDATE <- as.Date(rawdata[,"ISSUEDATE"],format="%d%m%Y")
data$COUPONRATE <- as.numeric(rawdata[,"COUPONRATE"])
data$PRICE <- as.numeric(CP)
data$ACCRUED <- as.numeric(AC)
data$CASHFLOWS <- list()
data$TODAY <- as.Date(TODAY,format="%d%m%Y")
NEXTCOUPON <- ifelse(as.Date(paste(rawdata[,"COUPONDATE"],substring(TODAY,5,8),sep=""),format="%d%m%Y") > data$TODAY,
paste(rawdata[,"COUPONDATE"],substring(TODAY,5,8),sep=""),
paste(rawdata[,"COUPONDATE"],as.character(as.numeric(substring(TODAY,5,8))+1),sep=""))
NCOUPON <- as.numeric(substring(rawdata[,"MATURITYDATE"],5,8)) - as.numeric(substring(NEXTCOUPON,5,8))
# cash flows ISIN
data$CASHFLOWS$ISIN <- vector()
for(i in 1:length(NCOUPON)){
data$CASHFLOWS$ISIN <- c(data$CASHFLOWS$ISIN,rep(data$ISIN[i],NCOUPON[i]+1))
}
# cash flows
data$CASHFLOWS$CF <- vector()
for(i in 1:length(NCOUPON)){
data$CASHFLOWS$CF <- c(data$CASHFLOWS$CF,c(rep(data$COUPONRATE[i]*100,NCOUPON[i]),100+data$COUPONRATE[i]*100))
}
# cash flow dates
data$CASHFLOWS$DATE <- vector()
for(i in 1:length(NCOUPON)){
data$CASHFLOWS$DATE <- c(data$CASHFLOWS$DATE,paste(rawdata[i,"COUPONDATE"],as.numeric(substring(NEXTCOUPON[i],5,8)) + seq(0,NCOUPON[i]),sep=""))
}
data$CASHFLOWS$DATE <- as.Date(data$CASHFLOWS$DATE,format="%d%m%Y")
data
}
dslist[[i]] <- datastreamlist(rawdata, TODAY, AC, CP)
}
dslist
}
datafiles = c("france S.csv", "france AC.csv", "france CP.csv")
govbondsts <- importdatastream(datafiles)
class(govbondsts)="couponbonds"
#class(govbondsts)="dyncouponbonds"
ns_res <- estim_nss(govbondsts, c("FRANCE"), matrange="all" ,method = "ns", tauconstr = list(c(0.2, 7, 0.2)), optimtype = "allglobal")
新的工作 R 代码:
if (!("termstrc" %in% installed.packages())) install.packages("termstrc")
library(termstrc)
datafiles = c("france S.csv", "france AC.csv", "france CP.csv")
rawdata <- read.csv(datafiles[1], dec=".", sep=",", colClasses = "character")
rawdata_AC <- read.csv(datafiles[2], dec=".",sep=",",colClasses = "character", header=TRUE, check.names=FALSE)
rawdata_CP <- read.csv(datafiles[3], dec=".",sep=",",colClasses = "character", header=TRUE, check.names=FALSE)
DATES <- as.character(as.Date(names(rawdata_AC)[-(1:2)], format="%d.%m.%y"))
DATES <- paste(substring(DATES,9,10),substring(DATES,6,7),substring(DATES,1,4),sep="")
AC <- rawdata[,"ACCRUED"]
CP <- rawdata[,"PRICE"]
dslist <- list()
for(i in 1:length(DATES)) {
TODAY <- DATES[i]
AC <- rawdata_AC[-(1:2)][,i]
CP <- rawdata_CP[-(1:2)][,i]
datastreamlist <- function(rawdata, TODAY, AC, CP){
data <- list()
data$ISIN <- rawdata[,"ISIN"]
data$MATURITYDATE <- as.Date(rawdata[,"MATURITYDATE"],format="%d%m%Y")
data$ISSUEDATE <- as.Date(rawdata[,"ISSUEDATE"],format="%d%m%Y")
data$COUPONRATE <- as.numeric(rawdata[,"COUPONRATE"])
data$PRICE <- as.numeric(CP)
data$ACCRUED <- as.numeric(AC)
data$CASHFLOWS <- list()
data$TODAY <- as.Date(TODAY,format="%d%m%Y")
NEXTCOUPON <- ifelse(as.Date(paste(rawdata[,"COUPONDATE"],substring(TODAY,5,8),sep=""),format="%d%m%Y") > data$TODAY,
paste(rawdata[,"COUPONDATE"],substring(TODAY,5,8),sep=""),
paste(rawdata[,"COUPONDATE"],as.character(as.numeric(substring(TODAY,5,8))+1),sep=""))
NCOUPON <- as.numeric(substring(rawdata[,"MATURITYDATE"],5,8)) - as.numeric(substring(NEXTCOUPON,5,8))
# cash flows ISIN
data$CASHFLOWS$ISIN <- vector()
for(i in 1:length(NCOUPON)){
data$CASHFLOWS$ISIN <- c(data$CASHFLOWS$ISIN,rep(data$ISIN[i],NCOUPON[i]+1))
}
# cash flows
data$CASHFLOWS$CF <- vector()
for(i in 1:length(NCOUPON)){
data$CASHFLOWS$CF <- c(data$CASHFLOWS$CF,c(rep(data$COUPONRATE[i]*100,NCOUPON[i]),100+data$COUPONRATE[i]*100))
}
# cash flow dates
data$CASHFLOWS$DATE <- vector()
for(i in 1:length(NCOUPON)){
data$CASHFLOWS$DATE <- c(data$CASHFLOWS$DATE,paste(rawdata[i,"COUPONDATE"],as.numeric(substring(NEXTCOUPON[i],5,8)) + seq(0,NCOUPON[i]),sep=""))
}
data$CASHFLOWS$DATE <- as.Date(data$CASHFLOWS$DATE,format="%d%m%Y")
data
}
dslist[[i]] <- list(FRANCE=datastreamlist(rawdata, TODAY, AC, CP))
class(dslist[[i]]) <- "couponbonds"
}
class(dslist) <- "dyncouponbonds"
dl_res <- estim_nss(dslist, c("FRANCE"), method = "dl", lambda = 1/3)
plot(dl_res)