简易时间序列分析的方法总结(R实现)
from http://www.cnblogs.com/aquastar/archive/2012/12/16/2820106.html
预备操作
以下方法请先安装和加载forecast/fpp包
install.packages("forecast")
install.packages("fpp")
library('forecast')
library('fpp')
移动平均分解法
用经典的移动平均分解时序为季节性,趋势和不规则部分,使用加法或乘法季节部分。
(Decompose a time series into seasonal, trend and irregular components using moving averages. Deals with additive or multiplicative seasonal component.)
births <- scan("http://robjhyndman.com/tsdldata/data/nybirths.dat")
birthstimeseries <- ts(births, frequency=12, start=c(1946,1))
注意这里的frequency要和最小单周期长度匹配效果才好(待学习)
如需查看可以 birthstimeseries 或 plot.ts(birthstimeseries)
birthstimeseriescomponents <- decompose(birthstimeseries)
估计出的季节性、趋势的和不规则部分现在被存储在变量birthstimeseriescomponents$seasonal, birthstimeseriescomponents$trend 和 birthstimeseriescomponents$random
plot(birthstimeseriescomponents)
以下是去掉季节性的时序
birthstimeseriesseasonallyadjusted <- birthstimeseries - birthstimeseriescomponents$seasonal
plot(birthstimeseriesseasonallyadjusted)
或
birthstimeseriesadj <- seasadj(birthstimeseries)
plot(birthstimeseriesadj)
局部加权回归法
STL分解基于Loess,即局部加权回归散点平滑法,是1990年由密歇根大学的R. B. Cleveland教授以及AT&T Bell实验室的W. S. Cleveland等人提出来的一种对时间序列进行分解的方法。STL分解将时间序列分解成季节项、趋势项及残余项。
births <- scan("http://robjhyndman.com/tsdldata/data/nybirths.dat")
birthstimeseries <- ts(births, frequency=12, start=c(1946,1))
如需查看可以 birthstimeseries
或 plot.ts(birthstimeseries)
birthstimeseriescomponents <- stl(birthstimeseries,s.window="periodic")
plot(birthstimeseriescomponents)
birthstimeseriescomponents <- forecast(birthstimeseriescomponents)
plot(birthstimeseriescomponents)
笔者尝试用STL预测手头的一个时序数据(第一列是实际观察值(a),第二列是预测值(b),第三列是1-(b-a)/a的百分比格式数据),总体偏大5%-10%,还是不错的