R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
#第二题
t2=read.csv("D:/班级文件/uzhuan/时间序列/第二节/zye22.csv",header = FALSE)#读csv数据
t2_vector <- as.vector(t(t2))#数据预处理,创建时间序列对象,起始时间为 1975 年 1 月,频率为 12(月度数据)
co<-ts(t2_vector,start = c(1975,1),frequency = 12)
co
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 1975 330.45 330.97 331.64 332.87 333.61 333.55 331.90 330.05 328.58 328.31
## 1976 331.63 332.46 333.36 334.45 334.82 334.32 333.05 330.87 329.24 328.87
## 1977 332.81 333.23 334.55 335.82 336.44 335.99 334.65 332.41 331.32 330.73
## 1978 334.66 335.07 336.33 337.39 337.65 337.57 336.25 334.39 332.44 332.25
## 1979 335.89 336.44 337.63 338.54 339.06 338.95 337.41 335.71 333.68 333.69
## 1980 337.81 338.16 339.88 340.57 341.19 340.87 339.25 337.19 335.49 336.63
##         Nov    Dec
## 1975 329.41 330.63
## 1976 330.18 331.50
## 1977 332.05 333.53
## 1978 333.59 334.76
## 1979 335.05 336.53
## 1980 337.74 338.36
plot(co)#该序列以半年为周期呈现明显周期性,因此,该序列一定不是平稳序列。

acf(co,lag.max = 24)#样本自相关系数图,自相关系数具有长期相关性且有三角对称性,故该序列为非平稳序列。,

acf(co)$acf#延迟阶数24,样本自相关系数

## , , 1
## 
##              [,1]
##  [1,]  1.00000000
##  [2,]  0.90750778
##  [3,]  0.72171377
##  [4,]  0.51251814
##  [5,]  0.34982244
##  [6,]  0.24689637
##  [7,]  0.20309427
##  [8,]  0.21020799
##  [9,]  0.26428810
## [10,]  0.36433219
## [11,]  0.48471672
## [12,]  0.58456166
## [13,]  0.60197891
## [14,]  0.51841257
## [15,]  0.36856286
## [16,]  0.20671211
## [17,]  0.08138070
## [18,]  0.00135460
## [19,] -0.03247916
#第7题
library(readr)
zye27 <- read_csv("D:/班级文件/uzhuan/时间序列/第二节/zye27.csv", locale = locale(encoding = "GBK"))
## Rows: 90 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (2): 年, 死亡率
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(zye27)
ts_data <- ts(zye27$死亡率, start = 1915, frequency = 1)  # 年度数据,频率为 1
ts_data
## Time Series:
## Start = 1915 
## End = 2004 
## Frequency = 1 
##  [1] 0.5215052 0.4248284 0.4250311 0.4771938 0.8280212 0.6156186 0.3666270
##  [8] 0.4308883 0.2810287 0.4646245 0.2693951 0.5779049 0.5661151 0.5077584
## [15] 0.7507175 0.6808395 0.7661091 0.4561473 0.4977496 0.4193273 0.6095514
## [22] 0.4573370 0.5705478 0.3478996 0.3874993 0.5824285 0.2391033 0.2367445
## [29] 0.2626158 0.4240934 0.3652750 0.3750758 0.4090056 0.3891676 0.2402610
## [36] 0.1589496 0.4393373 0.5094681 0.3743465 0.4339828 0.4130557 0.3288928
## [43] 0.5186648 0.5486504 0.5469111 0.4963494 0.5308929 0.5957761 0.5570584
## [50] 0.5731325 0.5005416 0.5431269 0.5593657 0.6911693 0.4403485 0.5676662
## [57] 0.5969114 0.4735537 0.5923935 0.5975556 0.6334127 0.6057115 0.7046107
## [64] 0.4805263 0.7026860 0.7009017 0.6030854 0.6980919 0.5976560 0.8023421
## [71] 0.6017109 0.5993127 0.6025625 0.7016625 0.4995714 0.4980918 0.4975690
## [78] 0.6001830 0.3339542 0.2744370 0.3209428 0.5406671 0.4050209 0.2885961
## [85] 0.3275942 0.3132606 0.2575562 0.2138386 0.1861856 0.1592713
plot(ts_data)#绘制时序图,通过时序图无法直观判断是否平稳,绘制自相关图

acf(ts_data)
acf(ts_data)$acf#自相关系数并没有迅速衰减到0,故判断为非平稳序列

## , , 1
## 
##              [,1]
##  [1,]  1.00000000
##  [2,]  0.56076761
##  [3,]  0.46196842
##  [4,]  0.37628563
##  [5,]  0.39972314
##  [6,]  0.32391693
##  [7,]  0.21044131
##  [8,]  0.25533506
##  [9,]  0.18721880
## [10,]  0.21006106
## [11,]  0.05404696
## [12,]  0.08762089
## [13,] -0.06481815
## [14,] -0.09531789
## [15,] -0.15266937
## [16,] -0.16984920
## [17,] -0.11922124
## [18,] -0.13976241
## [19,] -0.18842019
## [20,] -0.28441719
ts_diff <- diff(ts_data, differences = 1)# 计算一阶差分
plot(ts_diff)

acf(ts_diff)

Box.test(ts_diff,lag = 6,type = "Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  ts_diff
## X-squared = 21.986, df = 6, p-value = 0.001218
Box.test(ts_diff,lag = 12,type = "Ljung-Box")#P值都小于显著水平,所以该滞后一阶序列拒绝纯随机性的原假设。 
## 
##  Box-Ljung test
## 
## data:  ts_diff
## X-squared = 40.071, df = 12, p-value = 6.998e-05