TSLM错误:替换长度为零

时间:2017-08-08 02:50:22

标签: r time-series

我有以下数据结构:

d <- structure(list(Date = structure(c(17349, 17350, 17351, 17352, 
                                       17353, 17354, 17355, 17356, 17357, 17358, 17359, 17360, 17361, 
                                       17362, 17363, 17364, 17365, 17366, 17367, 17368, 17369, 17370, 
                                       17371, 17372, 17373, 17374, 17375, 17376, 17377, 17378, 17379, 
                                       17380, 17381, 17382, 17383), class = "Date"), Ratio = c(67, 50, 
                                                                                               67, 50, 100, 50, 33, 67, 0, 0, 0, 0, 100, 75, 0, 0, 75, 100, 
                                                                                               67, 33, 33, 33, 50, 50, 67, 100, 67, 50, 25, 25, 33, 33, 100, 
                                                                                               33, 0)), .Names = c("Date", "Ratio"), row.names = 183:217, class = "data.frame")

然后,使用xts包我创建一个如下的时间序列:

library(xts)
dates = as.Date(d$Date,"%Y-%m-%d")
xs = xts(d$Ratio,dates)

最后,我尝试对数据进行分区并训练线性模型:

library("forecast")
train.ts <- window(xs, start = as.Date("2017-07-01"), end = as.Date("2017-08-01"))
val.ts <- window(xs, start = as.Date("2017-08-02"), end = as.Date("2017-08-04"))
d.lm <- tslm(train.ts ~ trend + I(trend^2))

尝试训练模型会导致以下错误:

  

预测错误::: datamat(train.ts):替换长度为零

这是什么错误,我该如何解决?

注意:我最初怀疑这个错误是由于整个数据集中的NAs所致;但是,从那以后,我一直强迫这些零到无济于事!

编辑:这是完全可重复的示例(@Scarabee建议将xts转换为ts):

d <- structure(list(Date = structure(c(17349, 17350, 17351, 17352, 
                                       17353, 17354, 17355, 17356, 17357, 17358, 17359, 17360, 17361, 
                                       17362, 17363, 17364, 17365, 17366, 17367, 17368, 17369, 17370, 
                                       17371, 17372, 17373, 17374, 17375, 17376, 17377, 17378, 17379, 
                                       17380, 17381, 17382, 17383), class = "Date"), Ratio = c(67, 50, 
                                                                                               67, 50, 100, 50, 33, 67, 0, 0, 0, 0, 100, 75, 0, 0, 75, 100, 
                                                                                               67, 33, 33, 33, 50, 50, 67, 100, 67, 50, 25, 25, 33, 33, 100, 
                                                                                               33, 0)), .Names = c("Date", "Ratio"), row.names = 183:217, class = "data.frame")

library(xts)
dates = as.Date(d$Date,"%Y-%m-%d")
xs = xts(d$Ratio,dates)

library("forecast")
train.ts <- window(xs, start = as.Date("2017-07-01"), end = as.Date("2017-08-01"))
val.ts <- window(xs, start = as.Date("2017-08-02"), end = as.Date("2017-08-04"))
d.lm <- tslm(as.ts(train.ts) ~ trend + I(trend^2)) # results in error Error in [.data.frame(data, , 1) : undefined columns selected

sessionInfo()的输出:

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forecast_7.1      timeDate_3012.100 xts_0.9-7         zoo_1.7-13       

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4 fracdiff_1.4-2   ggplot2_2.1.0    grid_3.1.0       gtable_0.1.2     lattice_0.20-29  munsell_0.4.2   
 [8] nnet_7.3-8       parallel_3.1.0   plyr_1.8.1       quadprog_1.5-5   Rcpp_0.11.1      scales_0.4.0     tools_3.1.0     
[15] tseries_0.10-34 

错误更新xts包:

require(devtools)
# results in error "Error in as.POSIXct.default(value) : do not know how to convert 'value' to class “POSIXct”"
install_version("xts", version = "0.10", repos = "http://cran.us.r-project.org")

# results in error "Warning: invalid package 'https://cran.r-project.org/src/contrib/xts_0.10-0.tar.gz'"
install.packages("https://cran.r-project.org/src/contrib/xts_0.10-0.tar.gz", repos = NULL, type="source")

1 个答案:

答案 0 :(得分:2)

更新R并将forecastxts打包到最新版本后,错误消息不同:

d.lm <- tslm(train.ts ~ trend + I(trend^2))
# Error in names(vars)[length(vars)] <- make.names(colnames(vars[[i]])[j]) : 
#   replacement has length zero

我们可以通过将train.ts转换为ts对象来避免它:

d.lm <- tslm(ts(train.ts) ~ trend + I(trend^2))
d.lm
# Call:
# tslm(formula = ts(train.ts) ~ trend + I(trend^2))
# 
# Coefficients:
# (Intercept)        trend   I(trend^2)  
#    57.52770     -1.67996      0.04963  

注意:似乎ts()保留了时间序列的索引,而as.ts()则没有。