0

I am trying to using termstrc to calculate a time series of nelson siegal parm estimates. I am trying to get the code below to run, but I don't think I have the data in the right format. I'm not sure how to get the data to be organised as a "dyncouponbonds" class properly. The error I get is:

Error in (pos_cf[i] + 1):pos_cf[i + 1] : NA/NaN argument In addition: Warning message: In max(n_of_cf) : no non-missing arguments to max; returning -Inf

Apologies if I have not asked in the correct format etc. First time using 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")

NEW WORKING R CODE:

        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)
  • Welcome to SO! As I see it, there is more work on your side needed so that people can actually help you. Currently, you show us a huge function and tell us that this isn't working. Note that you give us file names to files that only you, but not we have. And it is somehow related to Datastream, a finance database many R users wouldn't know of...Those are two many free parameters to let us help you. So how can you improve that question? Have a look [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – Christoph_J Apr 10 '13 at 04:16
  • Basically, you have to try to find the exact statement that is causing the problem. As soon as you have found that, try to make it reproducible, i.e. reduce the problem more and more until you are left with a small set of variables you can post that produce the same error. And then your chances to get help here will definitely improve! (And often, you find the problem yourself by trying to make it reproducible). – Christoph_J Apr 10 '13 at 04:18
  • Not a solution to this question, but I've found the [YieldCurve](http://cran.r-project.org/web/packages/YieldCurve/) package to be easier to use. – Joshua Ulrich Apr 10 '13 at 09:29
  • Thanks for the tips! I've figured it out now. See new post above. I need to use this package because it deals with coupon bonds and doesn't require me to bootstrap before I fit the nelson siegel. Thanks again for everyone's tips. – Itsnota Tax Apr 17 '13 at 22:04

0 Answers0