0

termstrc を使用して、ネルソン シーガル パラメータ推定値の時系列を計算しようとしています。以下のコードを実行しようとしていますが、正しい形式のデータがあるとは思いません。データを「dyncouponbonds」クラスとして適切に編成する方法がわかりません。私が得るエラーは次のとおりです。

エラー (pos_cf[i] + 1):pos_cf[i + 1]: NA/NaN 引数 さらに: 警告メッセージ: In max(n_of_cf): no non-missing arguments to max; -Inf を返す

正しい形式で質問していないなどの場合はお詫び申し上げます。

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)
4

0 に答える 0