zhangtt123 发表于 2020-5-20 11:02

毕业实用模型(三)——时间序列forecast包的使用 ——————————————...

00引言
在毕业实用统计模型(一)——时间序列1中介绍了时间序列的模型的基本建模思路,在毕业设计实用模型(二)——时间序列之SARIMA2中说明了ARMA、ARIMA、SARIMA三大模型的关系与参数调整。但是在实际建模中往往会遇到更加实际的需求:模型的评估、参数检验、预测图的展示、模型参数的调整。对于很对不喜欢编程的人来说很是痛苦。本文将会重点从上述几个方面讲述forecast包里的主要的函数,并给出实例。从本文中你将会学会:

更加高效的的模型预测图。
模型的参数检验
模型定阶的函数
模型的评估函数
拟合线性的模型函数
相关图绘画,以及误差的标注
输出模型的预测误差
注:本文部分代码案例来自forecast包,大家有疑问可以自行去查找3.如果进不去可以运行下面代码会找到。
library(forecast)
help(package = forecast)

1、accuracy函数
描述:输入参数是模型,输出下面的结果:



函数示例:



最后的图片:
https://img-blog.csdnimg.cn/20200417163525888.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center

2、Acf、Pacf、taperedacf、taperedpacf
这四个函数分别是自相关函数、偏自相关函数、带有误差的自相关函数、带有误差的偏自相关函数。前两个很熟悉和内置的acf、pacf一样,下面只给出后两个的示例:


https://img-blog.csdnimg.cn/20200417165204121.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center
3、arfima
可以建立长期记忆的时间序列模型。
直接给例子了哦:


输出模型效果:


画出残差信息:
tsdisplay(residuals(fit))
https://img-blog.csdnimg.cn/2020041717103322.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center

4、Arima函数
函数介绍:这个函数可以拟合平稳或者非平稳的且已经知道参数的时间序列模型。也可以带有季节因素。

n = 50set.seed(0)x = rnorm(n, 1,3)  # 生成数据library(ggplot2)  # 载入画图包x %>%  Arima(order=c(3,1,1)) %>%  forecast(h=5) %>%  autoplot

上述代码用了管道函数,ggplot2包。对模型从数据到预测到建模一部到位。给大家看看Arima的参数。

function (y, order = c(0, 0, 0), seasonal = c(0, 0, 0), xreg = NULL,
    include.mean = TRUE, include.drift = FALSE, include.constant,
    lambda = model$lambda, biasadj = FALSE, method = c("CSS-ML",
        "ML", "CSS"), model = NULL, x = y, ...)
https://img-blog.csdnimg.cn/20200417171908204.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center
5、arima.errors函数
该函数使用简单,用于输出模型预测每一期的误差。注意和residuals函数进行区分。
# 生成数据建模n = 50set.seed(0)x = rnorm(n, 1,3)fit <- arfima(x)
# 计算结果
> arima.errors(fit)  #  预测误差
Deprecated, use residuals.Arima(object, type='regression') instead
Time Series:
Start = 1
End = 50
Frequency = 1
  4.78886285  0.02129992  4.98939779  4.81728796  2.24392430 -3.61985013
-1.78570110  0.11583866  0.98269848  8.21396017  3.29078038 -1.39702775
-2.44297103  0.13161528  0.10235465 -0.23453250  1.75667034 -1.67576338
  2.30704990 -2.71261527  0.32719634  2.13218694  1.40000908  3.41256853
  0.82867968  2.51082392  4.25730809 -1.07286152 -2.85379806  1.14017852
  0.29288033 -0.62866477 -0.29993095 -0.94841494  3.18025224  4.45573526
  3.97648110 -0.28853933  4.71491230  0.16196115  6.27370927  2.68223827
-0.35835192 -1.49612989 -2.49971164 -2.19677174 -3.69134615  4.46961099
  3.49614139  0.31801393
> residuals(fit)  # 残差
Time Series:
Start = 1
End = 50
Frequency = 1
  3.71706994 -1.28784846  3.87358515  3.45503095  0.78349981 -4.98056775
-2.74303846 -0.77184435  0.03669199  7.20508533  1.79698739 -2.80377609
-3.54880551 -0.77827016 -0.86325716 -1.20139553  0.81773766 -2.72392791
  1.43279254 -3.76501026 -0.47528735  1.24438107  0.37471968  2.37151650
-0.35743144  1.41677218  3.08432283 -2.39421040 -3.91173996  0.30272726
-0.68356344 -1.59285606 -1.19943417 -1.83611913  2.34677217  3.38917930
  2.72730075 -1.60715574  3.61473632 -1.17228009  5.12889059  1.21871171
-1.73495615 -2.66242875 -3.50034594 -3.04300026 -4.46595111  3.84607121
  2.44020165 -0.85475604

6、arimaorder
函数功能:输出函数的阶数,用于时间序列的自动定阶。下面用自动定阶函数auto.arima和管道函数结合输出参数。给出示例。

n = 50set.seed(100)x = rnorm(n, 3, 5) + 1:50  # 生成数据x %>% auto.arima %>% arimaorderp d q 3 1 0
可以配合arima函数进行模型的参数的确定。

7、auto.arima
函数功能:可以使用函数进行自动定阶。下面时该函数的参数。这里会进行讲解并给出具体的实例:
auto.arima(
  y, # 数据
  d = NA,  # 原始数据差分的阶数
  D = NA, # 季节因素的差分阶数
  max.p = 5,  # 遍历达到的最大的AR模型的阶数。
  max.q = 5,  # 遍历达到的最大的MA模型的阶数。
  max.P = 2,  # 季节因素遍历达到的最大的AR模型的阶数。
  max.Q = 2,  # 季节因素遍历达到的最大的MA模型的阶数。
  max.order = 5,
  max.d = 2,
  max.D = 1,
  start.p = 2,  # 开始遍历的AR模型的阶数。
  start.q = 2,  # 开始遍历的MA模型的阶数。
  start.P = 1,
  start.Q = 1,
  stationary = FALSE,  #
  seasonal = TRUE,
  ic = c("aicc", "aic", "bic"),  # 常用的选择模型的标准
  stepwise = TRUE,
  nmodels = 94,
  trace = FALSE,# 是否跟踪模型的选择
  approximation = (length(x) > 150 | frequency(x) > 12),
  method = NULL,
  truncate = NULL,
  xreg = NULL,
  test = c("kpss", "adf", "pp"),  # 平稳性检验的方式
  test.args = list(),
  seasonal.test = c("seas", "ocsb", "hegy", "ch"),  # 季节性分析
  seasonal.test.args = list(),
  allowdrift = TRUE,  # 可以去掉漂移项
  allowmean = TRUE,  # 可以去掉均值项
  lambda = NULL,
  biasadj = FALSE,
  parallel = FALSE,
  num.cores = 2,
  x = y,
  ...
)

auto.arima函数的参数众多,我把上述函数的参数给了一定的备注。大家可以根据这个注释和数据的其他的检验依据自己去逐一的验证调试。最终得出一个比较满意的模型。
下面给出部分调参的实例:
# 画时序图plot(ts(x))
https://img-blog.csdnimg.cn/20200417182400809.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center

下面展示追踪模型的参数:


更改准则AICc成AIC继续追踪模型的参数:


这个函数的参数就介绍到这里,大家用到自己继续探索。

8、ggplot2中的时间序列相关系数
这里把函数内置的实例给出来。

# 载入画图的ggplot2包
library(ggplot2)

下面的例子使用winneind数据。是个时间序列数据。

1 ggAcf(wineind)
https://img-blog.csdnimg.cn/2020041718264312.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center


1 wineind %>% taperedacf(plot=FALSE) %>% autoplot
https://img-blog.csdnimg.cn/20200417182656402.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center


1 ggCcf(mdeaths, fdeaths)

https://img-blog.csdnimg.cn/20200417182712741.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center

9、tslm
函数功能:用时间序列分量拟合线性模型
为了我方便第一次见到的同学学习,先贴出模型的参数:

1 > tslm
2 function (formula, data, subset, lambda = NULL, biasadj = FALSE,
3    ...)

formula参数是公式、data是数据。下面使用函数例子展示函数功能。

1  > y <- ts(rnorm(120,0,3) + 20*sin(2*pi*(1:120)/12), frequency=12) # 构造数据
2  > fit1 <- tslm(y ~ trend + season)  # 趋势+季节
3  > fit1
4  Call:
5  tslm(formula = y ~ trend + season)
6  Coefficients:
7  (Intercept)        trend      season2      season3      season4  
8     10.20906      0.01175      7.31274      8.82928      7.04245  
9      season5      season6      season7      season8      season9  
10   -2.04022    -10.24111    -20.57768    -27.61414    -30.16768  
11   season10     season11     season12  
12  -27.56840    -21.53062    -10.35272  

1 plot(forecast(fit1, h=20))  # 画出模型1的预测图
https://img-blog.csdnimg.cn/20200417184005479.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center

1  > fit2 <- tslm(y ~ season)
2  > fit2
3  Call:
4  tslm(formula = y ~ season)
5  Coefficients:
6  (Intercept)      season2      season3      season4      season5  
7     10.856        7.324        8.853        7.078       -1.993  
8    season6      season7      season8      season9     season10  
9    -10.182      -20.507      -27.532      -30.074      -27.463  
10   season11     season12  
11    -21.413      -10.223

1  plot(forecast(fit2, h=20))  # 画出模型2的预测图
https://img-blog.csdnimg.cn/20200417184018988.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center

10、CV交叉验证
CV函数显示模型的CV AIC AICc BIC AdjR2值,用上述模型直接给出例子。

1  > CV(fit1)
2          CV        AIC       AICc        BIC      AdjR2
3   12.851627 306.718526 310.718526 345.743411   0.945285
4  > CV(fit2)
5           CV         AIC        AICc         BIC       AdjR2
6   12.7985986 306.6337582 310.0677205 342.8711509   0.9449195

11、forecast
这个函数可以给出模型的预测值用于画图和分析。
下面给出上述模型一的预测值。


结果说明:第一列是时间、第二列是预测值、后面4列是预测值的80%、95%的置信下限和置信下限。

12、geom_forecast
这个图是ggplot2的预测图,下面贴出代码给出效果。这个函数也需要结合ggplot2包进行。

1  library(ggplot2)
2  autoplot(USAccDeaths) + geom_forecast()  # 图一
3  lungDeaths <- cbind(mdeaths, fdeaths)
4  autoplot(lungDeaths) + geom_forecast()  # 图二
https://img-blog.csdnimg.cn/20200417185235750.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center
https://img-blog.csdnimg.cn/20200417185312310.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjExMTgxNA==,size_16,color_FFFFFF,t_70#pic_center

13、总结
forecast包里的函数众多,上面只是介绍很少的一部分,更多函数的使用方式留着给大家探索。

14、参考文献
https://blog.csdn.net/weixin_46111814/article/details/105348265 ↩︎

https://blog.csdn.net/weixin_46111814/article/details/105370507 ↩︎

http://127.0.0.1:17627/library/forecast/html/00Index.html ↩︎
————————————————
版权声明:本文为CSDN博主「逆天者顺A」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46111814/article/details/105583080


星辰的微光 发表于 2020-5-20 15:43

点个赞,感谢分享
页: [1]
查看完整版本: 毕业实用模型(三)——时间序列forecast包的使用 ——————————————...