
Tutorial 8 Dayu 2022-05-2】 案例1,P35,绘制时序图 x1
Tutorial 8 Dayu 2022-05-21 案例 1, P35, 绘制时序图 x1 <- c(12.85,13.29,12.41,15.21,14.23,13.56) x2 <- c(15.21,14.23,14.69,13.27,16.75,15.33) da <- data.frame(x1, x2) # ts 函数:是将数值型数据向量转化成一个时间序列对象。 # 即每个数据带上一个日期标签 da1 <- ts(da[,1],frequency = 12, start = c(2004,7)) da2 <- ts(da[,2],frequency = 12, start = c(2004,7)) # frequency = 1(年), 12(月), 4(季度) # 将两张图放在一页 par(mfcol = c(2,1)) plot(da1,type = 'l') #'l'表示画线 plot(da2,type = 'l') 1

2004.5 2004.6 2004.7 2004.8 2004.9 Time 2004.5 2004.6 2004.7 2004.8 2004.9 Time #将两个数据放在一张图上 par(mfcol =c(1,1)) ts.plot(da1,da2,gpars-list(col-c("blue","red"))) 2004.5 2004.6 2004.7 2004.8 2004.9 Time 案例2,P37,平稳性检验
Time da1 2004.5 2004.6 2004.7 2004.8 2004.9 12.5 Time da2 2004.5 2004.6 2004.7 2004.8 2004.9 13.5 # 将两个数据放在一张图上 par(mfcol = c(1,1)) ts.plot(da1,da2,gpars=list(col=c("blue","red"))) Time 2004.5 2004.6 2004.7 2004.8 2004.9 13 14 15 16 案例 2, P37, 平稳性检验 2

x
x <- c(97,154,137.7,149,164,157,188,204,179,210,202,218,209, 204,211,206,214,217,210,217,219,211,233,316,221,239, 215,228,219,239,224,234,227,298,332,245,357,301,389) y <- ts(x, frequency = 1, start = 1970) library(forecast) ## Registered S3 method overwritten by 'quantmod': ## method from ## as.zoo.data.frame zoo library(tseries) # 步骤 1,平稳性检验 # 时序图检验 par(mfcol = c(1,1)) plot(y) Time y 1970 1980 1990 2000 100 150 200 250 300 350 400 # 不平稳 #par(mfcol = c(2,1)) # 自相关图 Acf(y) 3

Series y 123456789101112131415 Lag HA(q)的藏尾性,=0当>g时。 #ac(y)ACF时acf的改进,主要时Ac时没有在滞后0阶的时候绘制峰值 #偏自相关图 Pacf(y)
−0.4 0.0 0.2 0.4 0.6 Lag ACF Series y 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #MA(q) 的截尾性,i=0 当 i>q 时。 #acf(y) ACF 时 acf 的改进,主要时 Acf 没有在滞后 0 阶的时候绘制峰值 # 偏自相关图 Pacf(y) 4

Series y 00 女0- 1011121314 15 Lag #平稳的AR(P)模型一般有阶藏尾性,即=0当>时. #随机性检验 LBQ
−0.4 0.0 0.2 0.4 0.6 Lag Partial ACF Series y 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 平稳的 AR(P) 模型一般有 p 阶截尾性,即 Φii=0 当 i>p 时。 # 随机性检验 LBQ <- Box.test(y,type = "Ljung-Box") # 因为这里样本量很小,用 box-ljung 检验比较准确 P31 LBQ ## ## Box-Ljung test ## ## data: y ## X-squared = 14.037, df = 1, p-value = 0.0001792 # 拒绝原假设,不随机, 值得研究 # 差分次数 ndiffs(y) ## [1] 1 # 显示要差分一次 dy <- diff(y) plot(dy) 5

1970 1980 1990 2000 Time #adf.test(dy) #平稳序列建模步骤P66 1.判断序列是否为平稳非白噪声序列(时序图,随机性检验) 2.自相关图和偏自相关图,ARIMA(p,q)定阶 #3。进行模型拟合,估计参数值 4.有效性检验 5。模型改进优化 6.棋型预测 P681950-2008年我国邮政及农村投递线路每年新增里程数序列 x
Time dy 1970 1980 1990 2000 −100 −50 0 50 100 #adf.test(dy) # 平稳序列建模步骤 P66 #1. 判断序列是否为平稳非白噪声序列(时序图,随机性检验) #2. 自相关图和偏自相关图, ARIMA(p,q) 定阶 #3. 进行模型拟合,估计参数值 #4. 有效性检验 #5. 模型改进优化 #6. 模型预测 P68 1950-2008 年我国邮政及农村投递线路每年新增里程数序列 x <- c(15.71,24.43,18.23,22.50,12.53,9.94,7.19,41.13,79.03,119.32,-12.10,-89.71,-52.26,20.01,19.92,42.81,18.78,-0.75,-1.08,5.09,26.39,31.09,19.78,2.56,12.95,15.54,3.97,2.42,0.31,-0.51,-7.52,-7.69,1.61,4.46,10.97,15.15,6,-0.9,-3.22,-8.54,6.76,-0.83,4.67,11.68,0.82,8.54,24.51,28.91,44.94,11.16,11.08,15.75,-0.31,20.99,6.50,10.45,-3.51,23.42,17.99) da <- ts(x,frequency =1, start = c(1950)) 1. 随机性检验 plot(da,type = 'l') #'l'表示画线 6

1950 1960 1970 1980 1990 2000 2010 Time #由图显示,序列没有显著的非平稳特征。 #随机性检验 LBQ
Time da 1950 1960 1970 1980 1990 2000 2010 −50 0 50 100 # 由图显示,序列没有显著的非平稳特征。 # 随机性检验 LBQ <- Box.test(da,type = "Ljung-Box") # 因为这里样本量很小,用 box-ljung 检验比较准确 P31 LBQ # 拒绝原假设,不随机, 值得研究 ## ## Box-Ljung test ## ## data: da ## X-squared = 13.753, df = 1, p-value = 0.0002085 2. 自相关图 Acf(da) 7

Series da 5020 10 15 Lag MA(q)的裁尾性,当>q时. #从图中可以看出,除了1-3阶的自相关系数在2倍标准差之外,其他的 #自相关系数都在2倍标准差范围内波动,且衰减到零的过程存在正弦波动 来自相关系数的拖尾典型特征。 偏自相关图 Pacf(da)
−0.4 −0.2 0.0 0.2 0.4 Lag ACF Series da 5 10 15 #MA(q) 的截尾性,当 i>q 时。 # 从图中可以看出,除了 1-3 阶的自相关系数在 2 倍标准差之外,其他的 # 自相关系数都在 2 倍标准差范围内波动,且衰减到零的过程存在正弦波动 # 自相关系数的拖尾典型特征。 # 偏自相关图 Pacf(da) 8

Series da 5000.00- 女0 10 Lag #平稳的AR(P)模型一般有卫阶藏尾性,即i=0当p时。 #偏自相关图,除了1-2阶偏自相关系数在2倍以外,其他部在2倍以内, 2阶载尾典型,所以可以确定为AR(2)模型 习题1 (1)自行模拟P69例3-10并得出书本中的结论 overshot
−0.4 −0.2 0.0 0.2 0.4 Lag Partial ACF Series da 5 10 15 # 平稳的 AR(P) 模型一般有 p 阶截尾性,即 Φii=0 当 i>p 时。 # 偏自相关图,除了 1-2 阶偏自相关系数在 2 倍以外,其他都在 2 倍以内, #2 阶截尾典型,所以可以确定为 AR(2) 模型 习题 1 (1) 自行模拟 P69 例 3-10 并得出书本中的结论 overshot<-c(78, -58, 53, -63, 13, -6, -16, -14, 3, -74, 89, -48, -14, 32, 56, -86, -66, 50, 26, 59, -47, -83, 2, -1, 124, -106, 113, -76, -47, -32, 39, -30, 6, -73, 18, 2, -24, 23, -38, 91, -56, -58, 1, 14, -4, 77, -127, 97, 10, -28, -17, 23, -2, 48, -131, 65, -17) (2) 自行模拟 P69 例 3-11 并得出书本中的结论 temperature<-c(-0.40, -0.37, -0.43, -0.47, -0.72, -0.54, -0.47, -0.54, -0.39, -0.19, -0.40, -0.44, -0.49, -0.38, -0.41, -0.27, -0.18, -0.38, -0.22, -0.03, -0.09, -0.28, -0.36, -0.49, -0.25, -0.17, -0.45, -0.32, -0.33, -0.32, -0.29, -0.32, 0.25, -0.05, -0.01, -0.26, -0.48, -0.37, -0.20, -0.15, -0.08, -0.14, -0.13, -0.12, -0.10, 0.13, -0.01, 0.06, -0.17, -0.01, 0.09, 0.05, -0.16, 0.05, -0.02, 0.04, 0.17, 0.19, 0.05, 0.15, 0.13, 0.09, 0.04, 0.11, -0.03, 0.03, 0.15, 0.04, -0.02, -0.13, 0.02, 0.07, 0.20, -0.03, -0.07, -0.19, 0.09, 0.11, 0.06, 0.01, 0.08, 0.02, 0.02, -0.27, -0.18, -0.09, -0.02, -0.13, 0.02, 0.03, -0.12, -0.08, 0.17, -0.09, -0.04, -0.24, -0.16, -0.09, 0.12, 0.27, 0.42, 0.02, 0.30, 0.09, 0.05) P79 模型优化 (70 次化学反应过程) y <- c(47,64,23,71,38,64,55,41,59,48,71,35,57,40, 58,44,80,55,37,74,51,57,50,60,45,57,50,45, 25,59,50,71,56,74,50,58,45,54,36,54,48,55, 45,57,50,62,44,64,43,52,38,59,55,41,53,49, 34,35,54,45,68,38,50,60,39,59,40,57,54,23) plot(y,type = 'l') 9

e 00 20 Index #平稳 LBQ
0 10 20 30 40 50 60 70 30 40 50 60 70 80 Index y # 平稳 LBQ <- Box.test(y,type = "Ljung-Box") LBQ ## ## Box-Ljung test ## ## data: y ## X-squared = 11.103, df = 1, p-value = 0.0008619 # 非白噪声 Acf(y) 10