首页 > 解决方案 > R中的简单移动平均函数“sma”,“level”参数无法正常工作

问题描述

我需要为我正在尝试执行的预测获得 80% 和 95% 的置信水平,而简单的移动平均函数除了 95% 的置信水平之外不提供任何东西。有没有办法解决这个问题或任何一种变通方法来获得 80% 的置信水平?

这是一个示例代码,您会注意到下(上)界 80% 和下(上)界 95% 的输出完全相同。

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

标签: rpredictionmoving-averageforecast

解决方案


您有两种选择如何找到预测间隔。在第一个sma模型中,没有指定所需水平或预测范围。然后可以使用forecast包中的函数:smooth

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)

第二个选项是在sma调用中指定预测属性:

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

预测数据存储在变量 x1 中

x1$forecast
x1$lower
x1$upper

推荐阅读