I have the following data:
library(forecast)
library(lubridate)
weeks <- rep(seq(as.Date("2010-01-01"), as.Date("2023-01-01"), by = "week"), each = 1)
counts <- rpois(length(weeks), lambda = 50)
df <- data.frame(Week = as.character(weeks), Count = counts)
I am trying to adapt the R code found here (Rob J Hyndman – Time series cross-validation: an R example) for my problem:
# Convert Week column to Date format
df$Week <- as.Date(df$Week)
# Create a time series object
ts_data <- ts(df$Count, frequency = 52, start = c(year(min(df$Week)), 1))
# Set the length of data for fitting models
k <- 60
# Initialize matrices to store the MAE values
mae1 <- mae2 <- mae3 <- matrix(NA, nrow = length(ts_data) - k, ncol = 12)
# Loop through each time point
for(i in 1:(length(ts_data)-k))
{
# Define the training and testing sets
train_data <- window(ts_data, end = c(year(min(df$Week)) + floor((i+k-2)/52), (i+k-2)%%52+1))
test_data <- window(ts_data, start = c(year(min(df$Week)) + floor((i+k-1)/52), (i+k-1)%%52+1),
end = c(year(min(df$Week)) + floor((i+11+k-1)/52), (i+11+k-1)%%52+1))
# Fit and forecast using three different models
fit1 <- tslm(train_data ~ trend + season, lambda=0)
fcast1 <- forecast(fit1, h=12)
fit2 <- auto.arima(train_data, seasonal = TRUE, lambda = 0)
fcast2 <- forecast(fit2, h = 12)
fit3 <- ets(train_data)
fcast3 <- forecast(fit3, h = 12)
# Calculate MAE for each model's forecast
mae1[i, ] <- abs(fcast1[['mean']] - test_data)
mae2[i, ] <- abs(fcast2[['mean']] - test_data)
mae3[i, ] <- abs(fcast3[['mean']] - test_data)
}
# Plot the MAE values for each model
plot(1:12, colMeans(mae1, na.rm = TRUE), type = "l", col = 2, xlab = "horizon", ylab = "MAE", ylim = c(0, max(colMeans(mae1, na.rm = TRUE), colMeans(mae2, na.rm = TRUE), colMeans(mae3, na.rm = TRUE))))
lines(1:12, colMeans(mae2, na.rm = TRUE), type = "l", col = 3)
lines(1:12, colMeans(mae3, na.rm = TRUE), type = "l", col = 4)
legend("topleft", legend = c("LM", "ARIMA", "ETS"), col = 2:4, lty = 1)
The code seems to partly work, but I still get this error:
number of items to replace is not a multiple of replacement length
Can someone please show me how I can fix this?
Thanks!