Hello, everyone,
I have a question regarding the DIfferencing for an ARIMA model. In the first step I transform the data with:
Box.test(bc_ts, type="Ljung")
The next step is to give me
ndiffs(bc_ts)
that I need to differentiate the time series, which I will do in the next step:
diff_ts <- diff(bc_ts, differences = ndiffs(bc_ts))
After this, I will run auto.arima() as normal. Afterward I would like to test the accuracy and superimpose the graphs. First I transform the data back with BoxCox:
InvBoxCox(finalarima$x, lambda = lambda)
However, I don't know how to differentiate the data back to make the models comparable and plot them on top of each other. Unfortunately, I can't find a function to calculate back the forecasting series to get the total values. Enclosed once again my model.
I highly appreciate your help and best regards
Luke
# STEP 1: Check Volitality
# Box-Cox Transformation
lambda <- BoxCox.lambda(ts)
bc_ts <- BoxCox(ts,lambda=lambda)
tsdisplay(BoxCox(ts,lambda), ylab="Box Cox Transformed Demand")
# STEP 2: Detect Non-Stationary Data
# Box-Ljung test (tests the null hypothesis of absence of serial correlation)
Box.test(bc_ts, type="Ljung")
# ADF Test (non-stationary)
adf.test(bc_ts)
#KPSS Test (stationarity)
kpss.test(bc_ts)
kpss.test(diff(bc_ts))
# STEP 3: Differencing Non-Stationary Data
ndiffs(bc_ts)
diff_ts <- diff(bc_ts, differences = ndiffs(bc_ts))
plot.ts (diff_ts, ylab="Differenced & Box Cox Transformed Demand")
# STEP 4: Model Identification and Estimation
# METHOD: Minimum AIC/BIC Criteria
new_train <- window(diff_ts,
start = c(2016, 2),
end = c(2018, 52))
new_test <- window(diff_ts,
start = c(2019, 1),
end = c(2019, 52))
ARIMA <- auto.arima(new_train, trace=TRUE, ic="aicc", approximation = FALSE)
summary (ARIMA)
# Multi-step forecasts, re-estimation, 1-steps ahead
ARIMA_order <- arimaorder(ARIMA)
ARIMA_mat <- matrix(0, nrow=n, ncol=h)
for(i in 1:n)
{
x <- window(diff_ts, end=c(2018, 52) + (i-1)/52)
refit <- Arima(x, model=ARIMA)
ARIMA_mat[i,] <- forecast(refit, h=h)$mean
}
ARIMA_mat2 <- ts(ARIMA_mat, start=c(2019,1), end=c(2019,52), frequency=52)
IBC_ARIMA <- InvBoxCox(ARIMA_mat2, lambda=lambda)
#HOW TO BACKDIFFERENTIATE?
accuracy(IBC_ARIMA, test)
IBC_ARIMA %>% autoplot()
bind_ARIMA <- bind_cols(IBC_ARIMA, test)
colnames(bind_ARIMA) <- c("ARIMA_mat2","test")
bind_ARIMA2 <- bind_ARIMA %>%
mutate(week = 1:nrow(ARIMA_mat2)) %>%
gather(type, value, 1:2)
bind_ARIMA2
ggplot(data=bind_ARIMA2, mapping=aes(x=week, y=value, colour=type)) +
geom_line() + ggtitle("Comparison ARIMA_mat2 and test set") +
ylab ("Value") + xlab("Week in 2019")
In the end, the Plot looks like this: