I'm fairly new to R. I've searched and tried different changes and can't seem to find anything that helps.
I am running multiple forecasts in a loop for different periods to compare the automatically generated forecast to those done by hand. I've gotten it to work manually, but now I'm trying to automate the process so that it goes through all 7 month. For some reason, when I try to execute the following code, I get this error message:
Error in is.constant(x) :
(list) object cannot be coerced to type 'double'
Here is the traceback()
> 2: is.constant(x)
1: auto.arima(mth5[i])
Here's the code:
#Creating data frame for loop
year=c(2017,2017,2017,2017,2017,2017,2017)
month=c(1, 2, 3, 4, 5, 6, 7)
df1=data.frame(year, month)
library(forecast)
library(zoo)
library(sweep)
library(timekit)
library(pacman)
# List for storing the data being created in the loop
additive <- list()
mth5 <- list()
fit <- list()
#START LOOP
for (i in 1:length(unique(df1$month)))
{
mth5[[i]] <- ts(mth3[c("TPCMTD")], frequency=12, start=c(2012,1), end=c(df1$year,df1$month)[i])
fit[[i]] <- auto.arima(mth5[i])
additive[[i]] <- data.frame(
Add_Value = hw(mth5[i], h= 2, seasonal="additive")$mean[i],
Mul_Value = hw(mth5[i], h= 2, seasonal="multiplicative")$mean[i],
Arima_Value = forecast(fit[i], h = 2)$mean[i],
Arima_Method = forecast(fit[i], h = 2)$method[i],
Mth = unique(df1$month)[i]
)
}
# END LOOP
the data of mth3 looks like this:
tail(mth3)
# A tibble: 6 x 2
Date TPCMTD
<date> <dbl>
1 2017-02-27 513.0
2 2017-03-30 448.4
3 2017-04-29 419.8
4 2017-05-30 512.7
5 2017-06-29 515.9
6 2017-07-30 508.2
I believe that to be all of the relevant information. If I can provide anything else, please let me know.