1
我有以下數據結構:TSLM錯誤:更換長度也爲零
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))
試圖訓練模型結果在下面的ER ror:
Error in forecast:::datamat(train.ts) : replacement has length zero
什麼是這個錯誤,我該如何解決它?
注:我最初懷疑這個錯誤是由於整個數據集中的NA;然而,我已經強迫這些零到無濟於事!
編輯:這是完整再現的示例(與來自@Scarabee關於轉換到XTS的TS建議):
輸出sessionInfo()
的
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()
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")
最後一行有'<='您是不是指'<-' – epi99
另外,'lm'是一個函數名。最好不要將輸出分配給與函數名稱類似的對象名稱(如果它是'< - '而不是'<=') – akrun
當我輸入問題時是深夜,是的我的意思是< - '感謝捕捉。代碼導致相同的錯誤。任何想法爲什麼? – user1477388