I have the following data set (code requires the forecast
package for the tslm
call.
x <- rnorm(11, mean = 534, sd = 79)
y <- rnorm(9, mean = 800, sd = 56)
p <- list(x, y)
tsl <- list(); ts_trend <- list()
for(i in seq_along(p)) {
tsl[[i]] <- ts(p[[i]], start = c(2018, 1), frequency = 52)
}
for(i in seq_along(tsl)) {
ts_trend[[i]] <- tslm(tsl[[i]] ~ trend)
}
When I run it, I get the error
Error in tsl[[i]] : subscript out of bounds
The subscript, to my knowledge, is clearly not out of bounds. I use the same reference in the prior loop, with no error.
I have no idea how to fix this. What am I missing?
for
loops. i think the nested is not needed.for(i in seq_along(tsl)) ts_trend[[i]] <- tslm(tsl[[i]] ~ trend)
– akrunfor
loop. What is trend? – Parfaittslm
call. You can calltrend
and the coef will be the linear trend for the time series object. You can also construct the calltslm(x ~ trend + season)
and season will product seasonality coefficients as well as trend. – Todd Shannon