library(lubridate)
arrival$Date<- as.Date(paste0(arrival$Date,"01"), format = "%YM%m%d")
arrival_ts <- ts(Actual.Countsnew, start= c(1921,04), end=c(2021, 05), frequency = 12)
library("forecast")
t <- time(arrival_ts)
lm1 <- tslm(arrival_ts ~ t)
tsforecast <- forecast(lm1, h=50)
**Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : object is not a matrix**
**'newdata' had 50 rows but variable found had 1 rowError in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : **
** object is not a matrix**
plot(tsforecast)
You're trying to fit a regression model with a time trend, and you've generated the trend variable for the training period, but not for the test period. In other words, the forecast() function doesn't know what the values of t are in the future unless you tell it.
You could fix the problem by including a newdata argument in the call to forecast() which would include the future values of the t variable.
But a simpler fix is to use the built-in trend variable which is recognized by both tslm() and forecast() as a linear time trend and the values are generated for you.
If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: