0

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)
  • Unit Price 84.000 86.960 87.260 85.280 84.290 92.350 91.260 91.340 89.950 89.250 87.490 83.170 82.640 84.170 80.490 79.570 80.460 86.130 89.000 94.000 94.000 95.000 94.000 95.000 93.000 97.000 97.000 97.000 99.000 110.000 112.000 115.000 114.000 115.000 113.800 111.290 110.660 111.310 112.580 112.670 112.340 119.180 122.150 119.140 114.370 111.390 108.090 103.220 – Ryan Leveille Mar 09 '23 at 07:03
  • Time 1/1/2019 2/1/2019 3/1/2019 4/1/2019 5/1/2019 6/1/2019 7/1/2019 8/1/2019 9/1/2019 10/1/2019 11/1/2019 12/1/2019 1/1/2020 2/1/2020 3/1/2020 4/1/2020 5/1/2020 6/1/2020 7/1/2020 8/1/2020 9/1/2020 10/1/2020 11/1/2020 12/1/2020 1/1/2021 2/1/2021 3/1/2021 4/1/2021 5/1/2021 6/1/2021 7/1/2021 8/1/2021 9/1/2021 10/1/2021 11/1/2021 12/1/2021 1/1/2022 2/1/2022 3/1/2022 4/1/2022 5/1/2022 6/1/2022 7/1/2022 8/1/2022 9/1/2022 10/1/2022 11/1/2022 12/1/2022 – Ryan Leveille Mar 09 '23 at 07:04
  • Please [edit the question](https://stackoverflow.com/posts/75681301/edit) with the output of `dput(data)`. Or, if it is too big with the output of `dput(head(data, 20))`. – Rui Barradas Mar 09 '23 at 07:06
  • Added Dput in the question Rui! – Ryan Leveille Mar 09 '23 at 07:12
  • Sorry, I meant your data, you have posted the function `data`. – Rui Barradas Mar 09 '23 at 07:16
  • Use `forecast::forecast(fit_arima, h = 24)` instead. And you are missing an `"a"` in the next instruction, it should be `autoplot(fcast)`. – Rui Barradas Mar 09 '23 at 07:39
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 09 '23 at 09:09
  • Thank you Rui, I added the 'a' to the auto(fcast). However, the original question still stands when trying to create a forecast and then autoplot it. The same error message still occurs. Thank you for your help. – Ryan Leveille Mar 09 '23 at 14:15
  • Don't load the fable package. You don't use it, and it might be causing a clash with the forecast function if you're using an old version of fable. – Rob Hyndman Mar 09 '23 at 22:26

0 Answers0