I work for an auditing firm and perform time series analysis with reference to the online training("Forecasting Using R" at DataCamp) and the books.
In order to deepen my understanding, I have recalculated various methods myself, but I cannot reproduce the prediction interval of the meanf function.
Perhaps my formula is wrong, but it would be helpful if someone could teach me the correct formula.
Packages
library(fpp2)
library(zoo)
Create a data
mydata <- c(10, 20, 30, 40)
Create a ts object called myts
myts <- ts(mydata, start = c(2019, 1), frequency = 4)
Create an autoplot
autoplot(myts)
meanf
fc_mean <- meanf(myts, h = 2)
autoplot(fc_mean) +
scale_x_yearqtr(format = "%Y-%q") +
labs(x = "", y = "")
SD of Residuals
sd_resid <- fc_mean$model$sd
Value of Point forecast
pf <- fc_mean$mean[1]
Culculate the upper of 95% prediction interval
(ans1 <- pf + sd_resid * qnorm(0.975))
the upper of 95% prediction interval of the model
(ans2 <- fc_mean$upper[3])
Not identical!
identical(ans1, ans2)
Referred here by Forecasting: Principles and Practice, by Rob J Hyndman and George Athanasopoulos