此代码使我可以使用Dielbold-Li模型构建收益曲线。但是我在代码级别遇到了“构建收益曲线矢量”的问题。 应该注意的是,数据的长度与速率相同。 您可以生成它。
我正在尝试使用Dielbold-Li模型avec R建立收益率曲线
ben <- read.csv( "C:\\Users\\BALLA MBAYE DIOP\\Desktop\\Thèse bureau\\Sortie R\\data\\nex\\ben.csv",sep = ",")
#########Function to derive Diebold Li Factors################
di.li.factor.function <- function ( ben, start.date = 0, set.frequency = 1, tau = c(3,6,12,2*12,3*12,4*12,5*12,6*12,7*12,10*12), lambda = 0.0609)
# derives the diebold li factors from yield cross section in dataframe yield.data
# and returns factor, tau is a vector containing the maturity segments of the yield curve in
#month; start is the starting date of the time series in year + month/12; frequency = 12 for
#monthly data; tau is the vector of the maturity structure of the yield data passed in yield.data;
#lambda is fixed at 0.0609
{
dimvar <- dim(ben) # writes dimension of yield matrix in variable dimvar(number of
# rows, number of colums)
di.li.factor <- mat.or.vec(dimvar[1],3) # initializes matrix (number of rows, number of
# colums)
di.li.loading.1 <- mat.or.vec(length(tau),1)
di.li.loading.2 <- mat.or.vec(length(tau),1)
di.li.loading.3 <- mat.or.vec(length(tau),1)
for (loading.count in 1:length(tau))
# establishes the factor loadings in maturity tau
{
di.li.loading.1[loading.count] <- 1
di.li.loading.2[loading.count] <-
(1-exp( -lambda*tau[loading.count]))/(lambda*tau[loading.count])
di.li.loading.3[loading.count] <-
(1-exp(-lambda *tau[loading.count]))/( lambda *tau[loading.count])-exp(-
lambda *tau[loading.count])
}
yield.curve <- mat.or.vec(length(tau),1)
time.count <- 0
maturity.count <- 0
for (time.count in 1:dimvar[1])
{
for (maturity.count in 1:length(tau))
#building yield curve vector
{
yield.curve[maturity.count] <- ben[time.count, maturity.count]
}
#The lm function derives the regression model explainingo the
#yield.curve vector by the di.li.loading vectors without a constant
di.li.factor.lm <- lm ( yield.curve ~ -1 +di.li.loading.1
+di.li.loading.2
+di.li.loading.3)
#regression coefficients of the regression are written in to the
#di.li.factor matrix
di.li.factor[time.count,] <- di.li.factor.lm$coef
}
#colums of di.li.factors are labeled according to their interpretation
colnames(di.li.factor) <- c( "level" , "slope" , "curvature")
#time series is build by function ts
di.li.factor <- ts(di.li.factor, start = start.date, end = 10,
frequency = set.frequency)
# di.li.factor multiple time series is returned
return (di.li.factor)
}
显示此错误:yield.curve [maturity.count] <-ben [time.count,maturity.count]中的错误: 替换的长度为零 ben [time.count,maturity.count]错误:下标超出范围