I am getting this error:
Error in switch(names(interval), day = "days", hour = "hours", minute = "mins", : EXPR must be a length 1 vector**
When I am forecasting my ARIMA using the following line of code:
fcast = forecast(fit_arima,h=24)
I have tried troubleshooting this error on my own but after a couple hours I figured I would ask for some helps from the experts!
Here is my data, and below is my full script:
install.packages("fpp2")
install.packages("fable")
\#Install fpp2 package for Time Series data
library(fpp2)
library(fable)
\#Clear All Variables in workspace
rm(list = ls())
data = data.table(read.csv("spft.csv"))
\#Read DT
\#Declare this as time series data (Time Series Variable = Y)
Y = ts(data[,2],start=c(2019,1),frequency = 12)
\###########################################################################
# Preliminary Analysis
\###########################################################################
# Time Plot
autoplot(Y) +
ggtitle("Time Plot: Sparefoot Rental Rate Per Month") +
ylab("Adjusted for Inflation")
\#Data has a strong trend. Investigate Transformations
# Take the first difference of the data to remove the trend
DY = diff(Y)
# Time Plot Difference Data
autoplot(DY) +
ggtitle("Time Plot: Sparefoot Rental Rate Per Month") +
ylab("Adjusted for Inflation")
# Series appears trend-stationary, use to investigate seasonality
ggseasonplot(DY) + ggtitle("Seasonal Plot: Change in Sparefoot Street Rates") +
ylab("Sparefoot Street Rates")
\#The data is very seasonal - increase especially during the summertime May-August
# The data is very seasonal - decrease especially during August-Jan
# Let's look at another seasonal plot, the subseries plot
ggsubseriesplot(DY)
\#Average Changes in February,May,June,July,August are all positive
\#Average Changes in Jan,Mar,April,Sep,October,November,Dec are all negative
\##############################################################
# Our series, Y, has trend and seasonality
# To remove the trend, we take the first difference
# The first difference series still has seasonality
# Forecast with various methods.
######################################
####
# Use a benchmark method to forecast.
# Let's use seasonal naive method as our benchmark.
# y_t = y\_{t-s} + e_t
# We want to use the difference data because have a trend
####
fit = snaive(DY) # Residual SD = 2.86
print(summary(fit)) # This model is working well, The ACF model is within the 95% CI
checkresiduals(fit) # The residuals fit nicely in the bell curve
#####
# Fit ETS Method
#####
fit_ets = ets(Y)
print(summary(fit_ets))
checkresiduals(fit_ets)
# This model is also solid
#####
# Fit an ARIMA model
#####
fit_arima = auto.arima(Y,d=1,D=1, stepwise = FALSE, approximation = FALSE, trace = TRUE)
#this is fitting an arima, including difference (d=1), including seasonality (D=1)
# this fits the best arima model
print(summary(fit_arima))
#take square root of squared error
checkresiduals(fit_arima)
sqrt(4.526) #2.12
#This is the best model for this data
##############################
# Forecast with ARIMA model
#############################
fcast = forecast(fit_arima,h=24)
autoplot(fcast)
Edit
Data set posted in comments as a data.frame.
df1 <-
structure(list(Time = structure(c(17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536,
18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779, 18809,
18840, 18871, 18901, 18932, 18962, 18993, 19024, 19052, 19083,
19113, 19144, 19174, 19205, 19236, 19266, 19297, 19327), class = "Date"),
UnitPrice = c(84, 86.96, 87.26, 85.28, 84.29, 92.35, 91.26,
91.34, 89.95, 89.25, 87.49, 83.17, 82.64, 84.17, 80.49, 79.57,
80.46, 86.13, 89, 94, 94, 95, 94, 95, 93, 97, 97, 97, 99,
110, 112, 115, 114, 115, 113.8, 111.29, 110.66, 111.31, 112.58,
112.67, 112.34, 119.18, 122.15, 119.14, 114.37, 111.39, 108.09,
103.22)), class = "data.frame", row.names = c(NA, -48L))
Y <- ts(df1[, 2], start = c(2019, 1), frequency = 12)