简体   繁体   中英

TSLM Error: Replacement Has Length Zero

I have the following data structure:

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

And, using the xts package I create a time series like so:

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

Finally, I attempt to partition the data and train a linear model:

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

Attempting to train the model results in the following error:

Error in forecast:::datamat(train.ts) : replacement has length zero

What is this error and how can I resolve it?

Note: I initially suspected this error was due to NAs throughout the dataset; however, I have since coerced these to zero to no avail!

Edit: This is the full reproducible example (with a suggestion from @Scarabee regarding converting the xts to a 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

Output of 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 

Errors updating xts package:

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

After updating R and packages forecast and xts to their latest versions, the error message is different:

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

We can avoid it by converting train.ts into a ts object:

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  

Note: it seems that ts() keeps the index of the times series, while as.ts() doesn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM