簡體   English   中英

TSLM錯誤:替換長度為零

[英]TSLM Error: Replacement Has Length Zero

我有以下數據結構:

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):替換長度為零

這是什么錯誤,我該如何解決?

注意:我最初懷疑此錯誤是由於整個數據集中的NA所致; 但是,自那以后,我將其強制為零,無濟於事!

編輯:這是完整的可復制示例(來自@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")

將R並將packages 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()卻沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM