Search code examples
rpredictionmoving-averageforecast

Simple moving average function "sma" in R, "level" argument not working properly


I need to obtain the 80% and 95% confidence levels for a forecast I am trying to perform and the simple moving average function does not provide anything other than the 95% confidence levels. Is there a way to fix this or any kind of work-around to obtain the 80% confidence levels?

Here is a sample code, you'll notice that the outputs of the Lower (Upper) bound 80% and Lower (Upper) bound 95% are exactly the same.

library(smooth)
library(forecast)
iris
x1 = sma(iris$Sepal.Length, level = .80)
x2 = sma(iris$Sepal.Length, level = .95)
forecast(x1)
forecast(x2)

> forecast(x1)
Time Series:
Start = 151 
End = 160 
Frequency = 1 
    Point forecast Lower bound (2.5%) Upper bound (97.5%)
151       6.320000           5.153756            7.486244
152       6.244000           5.054660            7.433340
153       6.232800           5.010969            7.454631
154       6.179360           4.912203            7.446517
155       6.175232           4.845518            7.504946
156       6.230278           4.815333            7.645224
157       6.212334           4.755426            7.669243
158       6.206001           4.702861            7.709141
159       6.200641           4.648248            7.753034
160       6.204897           4.602211            7.807583
> forecast(x2)
Time Series:
Start = 151 
End = 160 
Frequency = 1 
    Point forecast Lower bound (2.5%) Upper bound (97.5%)
151       6.320000           5.153756            7.486244
152       6.244000           5.054660            7.433340
153       6.232800           5.010969            7.454631
154       6.179360           4.912203            7.446517
155       6.175232           4.845518            7.504946
156       6.230278           4.815333            7.645224
157       6.212334           4.755426            7.669243
158       6.206001           4.702861            7.709141
159       6.200641           4.648248            7.753034
160       6.204897           4.602211            7.807583

Solution

  • You have two options how to find predictive intervals. In the first one sma model is fitted without specification of desired level or forecasting horizon. Then function forecast from the package smooth can be used:

    x1 <- sma(iris$Sepal.Length)
    x2 <- sma(iris$Sepal.Length)
    smooth::forecast(x1, level = 0.80, h=12) # no need to used forecast package
    smooth::forecast(x2, level = 0.95, h=12)
    

    The second option is to specify forecasting attributes within the sma call:

    x1 <- sma(iris$Sepal.Length, level = 0.80, h=12, intervals="parametric")
    

    Forecast data is stored in the variable x1

    x1$forecast
    x1$lower
    x1$upper