国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

這篇具有很好參考價(jià)值的文章主要介紹了時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題

1.考慮序列{1,2,3,4,5,…,20}

1.1判斷該序列是否平穩(wěn)

x <- seq(1,20);x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

1.2樣本自相關(guān)系數(shù)

max_lag <- 6
acf_x <- acf(x, lag.max = max_lag, plot = FALSE)$acf
r_1_to_6 <- acf_x[2:(max_lag + 1)] ;r_1_to_6
## [1] 0.8500000 0.7015038 0.5560150 0.4150376 0.2800752 0.1526316
# 注意要從第2個(gè)元素開始提取,因?yàn)閍cf函數(shù)的結(jié)果向量的第一個(gè)元素對應(yīng)滯后期數(shù)為0的自相關(guān)系數(shù)。

1.3序列自相關(guān)圖

#加載必要的包
library(ggplot2)
library(ggfortify)
library(RColorBrewer)
library(forecast)
#設(shè)置顏色主題
my_palette <- brewer.pal(n = 8, name = "Dark2")

#繪制序列自相關(guān)圖并美化
windowsFonts(myFont = windowsFont("思源宋體 SemiBold"))
ggAcf(x) +
  ggtitle("序列自相關(guān)圖") + #添加標(biāo)題
  xlab("滯后") + #添加x軸標(biāo)簽
  ylab("自相關(guān)系數(shù)") + #添加y軸標(biāo)簽
  theme_minimal(base_size = 12, base_family = 'myFont') +
  theme(plot.title = element_text(hjust = 0.5), #居中標(biāo)題
        panel.grid.minor = element_blank(), #去除次要網(wǎng)格線
        panel.grid.major.x = element_line(size = 0.2, linetype = "dashed", color = "grey70"), #設(shè)置x軸主要網(wǎng)格線樣式
        axis.line.x = element_line(size = 0.5, color = "black"), #設(shè)置x軸線條粗細(xì)和顏色
        axis.line.y = element_line(size = 0.5, color = "black"), #設(shè)置y軸線條粗細(xì)和顏色
        axis.text = element_text(size = 10, color = my_palette[1]), #設(shè)置刻度標(biāo)簽字體大小和顏色
        axis.title = element_text(size = 12, face = "bold", color = my_palette[2]), #設(shè)置軸標(biāo)題字體大小、加粗和顏色
        panel.border = element_blank(), #去除面板邊框
        panel.background = element_blank(), #去除面板背景
        legend.background = element_blank(), #去除圖例背景
        legend.title = element_blank(), #去除圖例標(biāo)題
        legend.text = element_text(size = 10, color = my_palette[3]), #設(shè)置圖例文本字體大小和顏色
        legend.position = c(0.9, 0.8)) + #設(shè)置圖例位置
  scale_color_manual(values = my_palette[4:8]) #設(shè)置線條顏色
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

2.1975-1980年夏威夷島莫那羅亞火山每月釋放的CO?數(shù)據(jù)

2.1繪制時(shí)序圖,判斷平穩(wěn)性

d2=c(t(read.table('./時(shí)間序列分析——基于R(第2版)習(xí)題數(shù)據(jù)/習(xí)題2.2數(shù)據(jù).txt', sep = '\t')))
ts_d2 <- ts(d2, start = c(1975,1), frequency = 12);ts_d2
##         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
#加載必要的包library(ggplot2)
library(ggfortify)
library(RColorBrewer)
library(forecast)

# 繪制時(shí)序圖
windowsFonts(myFont = windowsFont("思源宋體 SemiBold"))
ggplot() +
  geom_line(aes(x = time(ts_d2), y = ts_d2),
            color = "#74759b", size = 1.2) +
  labs(title = "Monthly CO2 Emissions from Mauna Loa, Hawaii, 1975-1980",
       x = "Year", y = "CO2 Emissions") +
  theme_minimal(base_size = 12, base_family = 'myFont') +
  theme(plot.title = element_text(face = "bold"),
        axis.title = element_text(face = "bold", colour = '#132c33'),
        axis.line = element_line(size = 0.75, colour = '#2b73af'),
        axis.ticks = element_line(size = 0.5, colour = '#2376b7'),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

從時(shí)序圖中可以看出,該序列存在較明顯的季節(jié)性,同時(shí)也存在一定的趨勢性。

2.2計(jì)算樣本自相關(guān)系數(shù)

max_lag1 <- 24
acf_d2ts <- acf(ts_d2, lag.max = max_lag1, plot = FALSE)$acf
acf_d2ts[2:(max_lag1 + 1)] 
##  [1]  0.90750778  0.72171377  0.51251814  0.34982244  0.24689637  0.20309427
##  [7]  0.21020799  0.26428810  0.36433219  0.48471672  0.58456166  0.60197891
## [13]  0.51841257  0.36856286  0.20671211  0.08138070  0.00135460 -0.03247916
## [19] -0.02709893  0.01123597  0.08274806  0.17010715  0.24319854  0.25252294

2.3繪制自相關(guān)圖

ggAcf(ts_d2, lag.max = 36) +
  theme_minimal(base_size = 10, base_family = "myFont") +
  theme(plot.title = element_text(face = "bold", size = 14, hjust = 0.5),
        axis.title = element_text(face = "bold", hjust = 0.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(size = 0.75),
        axis.ticks = element_line(size = 0.5),
        axis.text = element_text(size = 12, color = "#815c94"),
        strip.text = element_text(size = 12, color = "#815c94", face = "bold"))
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

從自相關(guān)圖中可以看出,該序列存在較強(qiáng)的季節(jié)性和自相關(guān)性,不具有平穩(wěn)性。

3.1945-1950年費(fèi)城月度降雨量數(shù)據(jù)

3.1樣本自相關(guān)系數(shù)

d3 <- c(t(read.table('./時(shí)間序列分析——基于R(第2版)習(xí)題數(shù)據(jù)/習(xí)題2.3數(shù)據(jù).txt', sep = '\t')));
ts_d3 <- ts(d3, start = c(1945,1), frequency = 12);ts_d3
##        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
## 1945  69.3  80.0  40.9  74.9  84.6 101.1 225.0  95.3 100.6  48.3 144.5  28.3
## 1946  38.4  52.3  68.6  37.1 148.6 218.7 131.6 112.8  81.8  31.0  47.5  70.1
## 1947  96.8  61.5  55.6 171.7 220.5 119.4  63.2 181.6  73.9  64.8 166.9  48.0
## 1948 137.7  80.5 105.2  89.9 174.8 124.0  86.4 136.9  31.5  35.3 112.3  43.0
## 1949 160.8  97.0  80.5  62.5 158.2   7.6 165.9 106.7  92.2  63.2  26.2  77.0
## 1950  52.3 105.4 144.3  49.5 116.1  54.1 148.6 159.3  85.3  67.3 112.8  59.4
max_lag2 <- 24
acf_d3 <- acf(ts_d3, lag.max = max_lag2, plot = FALSE)$acf
acf_d3[2:(max_lag1 + 1)] 
##  [1]  0.012770216  0.041600613 -0.043230426 -0.178692841 -0.251298873
##  [6] -0.093810241 -0.067777725 -0.071978515  0.013882228  0.109450351
## [11]  0.217295088  0.315872697 -0.025053744  0.075320665 -0.141206897
## [16] -0.203589406 -0.245494618  0.066461869 -0.139454035 -0.034028373
## [21]  0.205723132 -0.009866008  0.080311638  0.118056190

3.2判斷平穩(wěn)性

#加載必要的包library(ggplot2)
library(ggfortify)
library(RColorBrewer)
library(forecast)

# 繪制時(shí)序圖
ggplot(data = ts_d3, aes(x = seq_along(ts_d3), y = ts_d3)) +
  geom_line(color = "#0072B2", size = 1.2) +
  labs(x = "Time", y = "Monthly Precipitation (mm)", 
       title = "Monthly Precipitation in Philadelphia, 1945-1950") +
  theme_minimal(base_size = 14, base_family = "myFont") +
  theme(plot.title = element_text(face = "bold", size = 14, hjust = 0.5),
        axis.text = element_text(size = 12, color = "#0072B2"),
        axis.title = element_text(size = 12, face = "bold", color = "#0072B2"))
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
# 繪制自相關(guān)圖
ggAcf(ts_d3, lag.max = 24) +
  theme_minimal(base_size = 14, base_family = "myFont") +
  theme(plot.title = element_text(face = "bold", size = 14, hjust = 0.5),
        axis.text = element_text(size = 12, color = "#0072B2"),
        axis.title = element_text(size = 12, face = "bold", color = "#0072B2"))
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

時(shí)序圖顯示了該數(shù)據(jù)集的所有觀察值,其中每個(gè)點(diǎn)代表一個(gè)月的降雨量。我們可以看到,該序列具有一些季節(jié)性,但沒有明顯的趨勢或周期性。

自相關(guān)圖顯示了每個(gè)滯后時(shí)點(diǎn)的自相關(guān)系數(shù)。我們可以看到,自相關(guān)系數(shù)在較短的滯后期內(nèi)很高,隨著滯后期的增加而逐漸降低。這表明該序列具有一些自相關(guān)性,但可能不是非常平穩(wěn)。

3.3判斷隨機(jī)性

for (k in1:3) print(Box.test(d3, lag = 6*k, type = 'Ljung-Box'))
## 
##  Box-Ljung test
## 
## data:  d3
## X-squared = 8.5225, df = 6, p-value = 0.2023
## 
## 
##  Box-Ljung test
## 
## data:  d3
## X-squared = 23.36, df = 12, p-value = 0.02482
## 
## 
##  Box-Ljung test
## 
## data:  d3
## X-squared = 36.02, df = 18, p-value = 0.007015
#白噪聲序列

4.判斷序列是否為純隨機(jī)序列(ɑ=0.05)

# 假設(shè)前12個(gè)樣本的自相關(guān)系數(shù)存儲(chǔ)在一個(gè)名為 acf_x 的向量中
acf_x <- c(0.02, 0.05, 0.10, -0.02, 0.05, 0.01, 0.12, -0.06, 0.08, -0.05, 0.02, -0.05)

# 假設(shè)最大滯后階數(shù)為10
max_lag <- 10# 計(jì)算樣本自相關(guān)系數(shù)的標(biāo)準(zhǔn)誤
se <- 1/sqrt(100)

# 計(jì)算Ljung-Box檢驗(yàn)的統(tǒng)計(jì)量和臨界值
lb_stat <- sum((acf_x[-1])^2/(1:length(acf_x[-1])))/100/(se^2)
lb_crit <- qchisq(0.95, max_lag)

# 判斷序列是否為純隨機(jī)序列if (lb_stat < lb_crit) {
  cat("序列可能是純隨機(jī)序列")
} else {
  cat("序列不是純隨機(jī)序列")
}
## 序列可能是純隨機(jī)序列

5.某公司在2000-2003年間每月的銷售量

5.1繪制時(shí)序圖和樣本自相關(guān)圖

d5 <- read.table('./時(shí)間序列分析——基于R(第2版)習(xí)題數(shù)據(jù)/習(xí)題2.5數(shù)據(jù).txt', header = TRUE);d5
##    月份 X2000年 X2001年 X2002年 X2003年
## 1   1月     153     134     145     117
## 2   2月     187     175     203     178
## 3   3月     234     243     189     149
## 4   4月     212     227     214     178
## 5   5月     300     298     295     248
## 6   6月     221     256     220     202
## 7   7月     201     237     231     162
## 8   8月     175     165     174     135
## 9   9月     123     124     119     120
## 10 10月     104     106      85      96
## 11 11月      85      87      67      90
## 12 12月      78      74      75      63
ts_d5 <- ts(c(d5$X2000年, d5$X2001年, d5$X2002年, d5$X2003年), 
              start = c(2000, 1), frequency = 12);ts_d5
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2000 153 187 234 212 300 221 201 175 123 104  85  78
## 2001 134 175 243 227 298 256 237 165 124 106  87  74
## 2002 145 203 189 214 295 220 231 174 119  85  67  75
## 2003 117 178 149 178 248 202 162 135 120  96  90  63
#加載必要的包library(ggplot2)
library(ggfortify)
library(RColorBrewer)
library(forecast)

# 繪制時(shí)序圖
ggplot(data = ts_d5, aes(x = seq_along(ts_d5), y = ts_d5)) +
  geom_line(color = "#5d3131", size = 1.2) +
  labs(x = "時(shí)間", y = "銷售量", 
       title = "銷售量時(shí)序圖") +
  theme_minimal(base_size = 14, base_family = "myFont") +
  theme(plot.title = element_text(face = "bold", size = 14, hjust = 0.5),
        axis.line = element_line(size = 0.5, colour = '#ed3333'),
        axis.ticks = element_line(size = 0.5, colour = '#ed3333'),
        axis.text = element_text(size = 12, color = "#daa45a"),
        axis.title = element_text(size = 12, face = "bold", color = "#5d3131"))
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
# 繪制自相關(guān)圖
ggAcf(ts_d5, lag.max = 12) + 
  labs(title = "Sample Autocorrelation Function", x = "Lag", y = "Autocorrelation") +
  theme_minimal() +
  theme(plot.title = element_text(size = 18, face = "bold", hjust = 0.5),
        axis.title = element_text(size = 14, face = "bold"),
        axis.text = element_text(size = 12),
        legend.position = "none")
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

5.2判斷該序列的平穩(wěn)性和純隨機(jī)性

# 單位根檢驗(yàn)library(tseries)
adf.test(ts_d5)
## Warning in adf.test(ts_d5): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ts_d5
## Dickey-Fuller = -6.1123, Lag order = 3, p-value = 0.01
## alternative hypothesis: stationary
# Ljung-Box檢驗(yàn)
Box.test(ts_d5, lag = 12, type = "Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  ts_d5
## X-squared = 190.4, df = 12, p-value < 2.2e-16

如果單位根檢驗(yàn)的 p 值小于 0.05,則時(shí)間序列不是平穩(wěn)的。如果 Ljung-Box 檢驗(yàn)的 p 值小于 0.05,則時(shí)間序列不是純隨機(jī)的。根據(jù)結(jié)果,我們可以得出結(jié)論:該時(shí)間序列是非平穩(wěn)的且非純隨機(jī)的。

6.1969年1月至1973年9月芝加哥海德公園內(nèi)每28天發(fā)生的搶包案件數(shù)

6.1判斷序列時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼的平穩(wěn)性和純隨機(jī)性

d6 <- c(t(read.table('./時(shí)間序列分析——基于R(第2版)習(xí)題數(shù)據(jù)/習(xí)題2.6數(shù)據(jù).txt', sep = '\t')));d6
##  [1] 10 15 10 10 12 10  7  7 10 14  8 17 14 18  3  9 11 10  6 12 14 10 25 29 33
## [26] 33 12 19 16 19 19 12 34 15 36 29 26 21 17 19 13 20 24 12  6 14  6 12  9 11
## [51] 17 12  8 14 14 12  5  8 10  3 16  8  8  7 12  6 10  8 10  5 NA NA
# 設(shè)置主題
theme_custom <- function(base_size = 10, base_family = "myFont") {
  theme(
    text = element_text(size = base_size, family = base_family),
    plot.title = element_text(hjust = 0.5, size = base_size*1.2, face = "bold", colour = '#004a7c'),
    plot.subtitle = element_text(hjust = 0.5, size = base_size, face = "italic"),
    axis.title = element_text(size = base_size*1.1, face = "bold", colour = '#1b262c'),
    axis.text = element_text(size = base_size, colour = '#0f4c75'),
    legend.title = element_text(size = base_size*1.1, face = "bold", colour = '#3282b8'),
    legend.text = element_text(size = base_size, colour = '#bbe1fa')
  )
}

# 畫時(shí)序圖library(ggplot2)
ggplot(data.frame(x = na.omit(d6), t = 1:length(na.omit(d6))), aes(x = t, y = x)) + 
  geom_line(color = "#3B4F63") +
  labs(title = "Robberies in Heide Park (1969-1973)",
       subtitle = "Recorded every 28 days",
       x = "Time", y = "Number of robberies") +
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
# 檢驗(yàn)平穩(wěn)性和純隨機(jī)性library(tseries)
adf.test(na.omit(d6)) # ADF檢驗(yàn)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  na.omit(d6)
## Dickey-Fuller = -2.168, Lag order = 4, p-value = 0.5069
## alternative hypothesis: stationary
kpss.test(na.omit(d6)) # KPSS檢驗(yàn)
## 
##  KPSS Test for Level Stationarity
## 
## data:  na.omit(d6)
## KPSS Level = 0.35581, Truncation lag parameter = 3, p-value = 0.0962
ggAcf(na.omit(d6))+
  ggtitle("Autocorrelation of Robberies in Heide Park (1969-1973)")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
ggPacf(na.omit(d6))+
  ggtitle("Partial Autocorrelation of Robberies in Heide Park (1969-1973)")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
print(adf.test(na.omit(d6)))
## 
##  Augmented Dickey-Fuller Test
## 
## data:  na.omit(d6)
## Dickey-Fuller = -2.168, Lag order = 4, p-value = 0.5069
## alternative hypothesis: stationary
print(kpss.test(na.omit(d6)))
## 
##  KPSS Test for Level Stationarity
## 
## data:  na.omit(d6)
## KPSS Level = 0.35581, Truncation lag parameter = 3, p-value = 0.0962

由ADF和KPSS檢驗(yàn)的結(jié)果可以看出,序列${x_t}$和${y_t}$均不平穩(wěn)。由ACF和PACF圖可以看出,序列${x_t}$和${y_t}$均存在較強(qiáng)的自相關(guān)性和季節(jié)性。

6.2對序列進(jìn)行函數(shù)運(yùn)算:時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼,并判斷序列時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼的平穩(wěn)性和純隨機(jī)性

# 計(jì)算差分序列
diff_data <- diff(na.omit(d6));diff_data
##  [1]   5  -5   0   2  -2  -3   0   3   4  -6   9  -3   4 -15   6   2  -1  -4   6
## [20]   2  -4  15   4   4   0 -21   7  -3   3   0  -7  22 -19  21  -7  -3  -5  -4
## [39]   2  -6   7   4 -12  -6   8  -8   6  -3   2   6  -5  -4   6   0  -2  -7   3
## [58]   2  -7  13  -8   0  -1   5  -6   4  -2   2  -5
# 畫差分序列時(shí)序圖
ggplot(data.frame(y = diff_data, t = 2:length(na.omit(d6))), aes(x = t, y = y)) + 
  geom_line(color = "#F15A60") +
  labs(title = "Differenced Robberies in Heide Park (1969-1973)",
       subtitle = "Recorded every 28 days",
       x = "Time", y = "Difference in number of robberies") +
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
# 檢驗(yàn)平穩(wěn)性和純隨機(jī)性
adf.test(diff_data) # ADF檢驗(yàn)
## Warning in adf.test(diff_data): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff_data
## Dickey-Fuller = -4.3625, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
kpss.test(diff_data) # KPSS檢驗(yàn)
## Warning in kpss.test(diff_data): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  diff_data
## KPSS Level = 0.07078, Truncation lag parameter = 3, p-value = 0.1
ggAcf(diff_data)+
  ggtitle("Autocorrelation of Differenced Robberies in Heide Park (1969-1973)")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
ggPacf(diff_data)+
  ggtitle("Partial Autocorrelation of Differenced Robberies in Heide Park (1969-1973)")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
print(adf.test(diff_data))
## Warning in adf.test(diff_data): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff_data
## Dickey-Fuller = -4.3625, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
print(kpss.test(diff_data))
## Warning in kpss.test(diff_data): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  diff_data
## KPSS Level = 0.07078, Truncation lag parameter = 3, p-value = 0.1

對于差分序列${y_t}$,其平穩(wěn)性得到了明顯改善。ADF和KPSS檢驗(yàn)的結(jié)果均表明,差分序列${y_t}$是平穩(wěn)的。同時(shí),由ACF和PACF圖可以看出,差分序列${y_t}$不存在明顯的自相關(guān)性和季節(jié)性。

7.1915-2004年澳大利亞每年與槍支有關(guān)的兇殺案死亡率(每10萬人)

7.1繪制時(shí)序圖,考察平穩(wěn)特征

d7 <- read.table('./時(shí)間序列分析——基于R(第2版)習(xí)題數(shù)據(jù)/習(xí)題2.7數(shù)據(jù).txt', header = TRUE)
ts_d7 <- ts(d7$死亡率,start = c(1915));ts_d7
## 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
autoplot(ts_d7, xlab = "年份", ylab = "死亡率(每10萬人)")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

根據(jù)時(shí)序圖可以看出,該序列的方差似乎并沒有隨著時(shí)間變化而發(fā)生顯著的變化,因此可以初步認(rèn)為該序列是平穩(wěn)的。但是,為了進(jìn)一步確定該序列的平穩(wěn)性,需要繪制自相關(guān)圖。

7.2繪制自相關(guān)圖,分析該序列的平穩(wěn)性

# 繪制自相關(guān)圖
ggAcf(ts_d7, lag.max = 20)+
  ggtitle("自相關(guān)圖")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

由自相關(guān)圖可以看出,該序列存在顯著的正自相關(guān),且自相關(guān)系數(shù)衰減緩慢,這表明該序列不是平穩(wěn)的。為了進(jìn)一步確定該序列的平穩(wěn)性,需要對其進(jìn)行一階差分,得到一階差分后的序列并繪制時(shí)序圖和自相關(guān)圖。

7.3如果該序列是平穩(wěn)序列,則分析該序列的純隨機(jī)性,如果是非平穩(wěn)序列,則分析該序列一階差分后序列的平穩(wěn)性。

# 進(jìn)行一階差分
diff_ts_d7 <- diff(ts_d7)
par(mfrow=c(1,2))
# 繪制差分后的時(shí)序圖
autoplot(diff_ts_d7, xlab = "年份", ylab = "死亡率(每10萬人)")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
# 繪制差分后的自相關(guān)圖
ggAcf(diff_ts_d7, lag.max = 20)+
  ggtitle("一階差分后的自相關(guān)圖")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

由于差分后序列的自相關(guān)圖中,幾乎所有的自相關(guān)系數(shù)都在顯著水平之下,表明序列具有平穩(wěn)性,因此可以進(jìn)行純隨機(jī)性檢驗(yàn)。

library(lawstat)
## 
## 載入程輯包:'lawstat'
## The following object is masked from 'package:tseries':
## 
##     runs.test
runs.test(diff_ts_d7)
## 
##  Runs Test - Two sided
## 
## data:  diff_ts_d7
## Standardized Runs Statistic = 2.4535, p-value = 0.01415

由于p值大于0.05,無法拒絕原假設(shè),因此認(rèn)為該序列是一個(gè)純隨機(jī)序列。

8.1860-1955年密歇根湖每月平均水位的最高值序列

8.1繪制時(shí)序圖,考察平穩(wěn)特征

d8 <- read.table('./時(shí)間序列分析——基于R(第2版)習(xí)題數(shù)據(jù)/習(xí)題2.8數(shù)據(jù).txt', header = TRUE)
ts_d8 <- ts(d8$水位,start = c(1860));ts_d8
## Time Series:
## Start = 1860 
## End = 1955 
## Frequency = 1 
##  [1] 83.30 83.50 83.20 82.60 82.20 82.10 81.70 82.20 81.60 82.10 82.70 82.80
## [13] 81.50 82.20 82.30 82.10 83.60 82.70 82.50 81.50 82.10 82.20 82.60 83.30
## [25] 83.10 83.30 83.70 82.90 82.30 81.80 81.60 80.90 81.00 81.30 81.40 80.20
## [37] 80.00 80.85 80.83 81.10 80.70 81.10 80.83 80.82 81.50 81.60 81.50 81.60
## [49] 81.80 81.10 80.50 80.00 80.70 81.30 80.70 80.00 81.10 81.87 81.91 81.30
## [61] 81.00 80.50 80.60 79.80 79.60 78.49 78.49 79.60 80.60 82.30 81.20 79.10
## [73] 78.60 78.70 78.00 78.60 78.70 78.60 79.70 80.00 79.30 79.00 80.20 81.50
## [85] 80.80 81.00 80.96 81.10 80.80 79.70 80.00 81.60 82.70 82.10 81.70 81.50
# 繪制時(shí)序圖library(ggplot2)
ggplot(d8, aes(x = 年, y = 水位)) +
  geom_line(color = "steelblue", size = 1) +
  labs(title = "密歇根湖水位(1860-1955)", x = "年", y = "水位") +
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

根據(jù)時(shí)序圖可以看出,該序列存在周期性波動(dòng),但整體趨勢基本穩(wěn)定。

8.2繪制自相關(guān)圖,分析該序列的平穩(wěn)性

# 繪制自相關(guān)圖
ggAcf(ts_d8, lag.max = 30)+
  ggtitle("自相關(guān)圖") +
  xlab("Lag") +
  ylab("ACF") +
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

自相關(guān)圖顯示出較強(qiáng)的正自相關(guān)性,表明該序列不是平穩(wěn)序列,需要進(jìn)行差分。

8.3如果該序列是平穩(wěn)序列,則分析該序列的純隨機(jī)性,如果是非平穩(wěn)序列,則分析該序列一階差分后序列的平穩(wěn)性。

# 進(jìn)行一階差分
diff_ts_d8 <- diff(ts_d8)
ggplot() +
  geom_line(aes(x = d8$年[-1], y = diff_ts_d8), color = "steelblue", size = 1) +
  labs(title = "密歇根湖水位一階差分序列(1861-1955)", x = "年", y = "水位差分") +
  theme_custom()
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼
# 繪制自相關(guān)圖
ggAcf(diff_ts_d8, lag.max = 30)+
  ggtitle("一階差分自相關(guān)圖")+
  theme_custom()
時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼

一階差分的時(shí)序圖顯示出,一階差分序列的波動(dòng)已經(jīng)變得更加平穩(wěn)。自相關(guān)圖顯示出,一階差分序列的自相關(guān)系數(shù)都在置信區(qū)間內(nèi),表明一階差分序列已經(jīng)平穩(wěn)。文章來源地址http://www.zghlxwxcb.cn/news/detail-417113.html

1.本文參考資料為時(shí)間序列分析——基于R/王燕編著. —5版. —北京:中國人民大學(xué)出版社,2020.6
(基于R應(yīng)用的統(tǒng)計(jì)學(xué)叢書)
ISBN 978-7-300-27898-8
2.本文的部分代碼參考了ChatGPT給出的方法,經(jīng)檢驗(yàn)后有效。

到了這里,關(guān)于時(shí)間序列分析——基于R | 第2章 時(shí)間序列的預(yù)處理習(xí)題代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 數(shù)據(jù)分析-Pandas如何輕松處理時(shí)間序列數(shù)據(jù)

    時(shí)間序列數(shù)據(jù)在數(shù)據(jù)分析建模中很常見,例如天氣預(yù)報(bào),空氣狀態(tài)監(jiān)測,股票交易等金融場景。此處選擇巴黎、倫敦歐洲城市空氣質(zhì)量監(jiān)測 N O 2 NO_2 N O 2 ? 數(shù)據(jù)作為樣例。 python數(shù)據(jù)分析-數(shù)據(jù)表讀寫到pandas 經(jīng)典算法-遺傳算法的python實(shí)現(xiàn) 經(jīng)典算法-遺傳算法的一個(gè)簡單例子 大

    2024年01月25日
    瀏覽(27)
  • 【ARMA時(shí)間序列分析】基于matlab ARMA時(shí)間序列分析【含Matlab源碼 2430期】
  • 基于機(jī)器學(xué)習(xí)/深度學(xué)習(xí)的時(shí)間序列分析相關(guān)論文

    基于機(jī)器學(xué)習(xí)/深度學(xué)習(xí)的時(shí)間序列分析相關(guān)論文

    Robust Time Series Analysis and Applications: An Industrial Perspective, in? KDD ?2022. Time Series in Healthcare: Challenges and Solutions, in? AAAI ?2022. [[Link]](https://www.vanderschaar-lab.com/time-series-in-healthcare/) Time Series Anomaly Detection: Tools, Techniques and Tricks, in? DASFAA ?2022. [[Link]](https://www.dasfaa2022.org//tutorials/T

    2024年02月13日
    瀏覽(23)
  • 時(shí)間序列分析——基于R(第2版)—第6章

    6-1 1962年1月至1975年12月牛奶月產(chǎn)奶量 分析它們受哪些確定性因素的影響,為該序列選擇適當(dāng)?shù)拇_定性因素分解模型 提取該序列的趨勢效應(yīng) 提取該序列的季節(jié)效應(yīng) 用指數(shù)平滑法對該序列做2年期預(yù)測 用arima季節(jié)模型擬合并預(yù)測該序列的發(fā)展 比較分析使用過的三種模型的擬合精

    2024年02月04日
    瀏覽(15)
  • 時(shí)間序列分析——基于R(第2版)—第4章

    4_1某公司過去50個(gè)月每月盈虧 繪制該序列的時(shí)序圖 判斷該序列的平穩(wěn)性與純隨機(jī)性#平穩(wěn)非白噪聲 考察該序列自相關(guān)圖和偏自相關(guān)圖的性質(zhì) 利用擬合模型預(yù)測該公司未來5年的盈虧情況 4_2某城市過去四年每個(gè)月人口凈流入量 繪制該序列的時(shí)序圖 判斷該序列的平穩(wěn)性與純隨機(jī)

    2024年02月04日
    瀏覽(23)
  • 基于PyTorch+LSTM的交通客流預(yù)測(時(shí)間序列分析)

    基于PyTorch+LSTM的交通客流預(yù)測(時(shí)間序列分析)

    大家好,我是阿光。 本專欄整理了《PyTorch深度學(xué)習(xí)項(xiàng)目實(shí)戰(zhàn)100例》,內(nèi)包含了各種不同的深度學(xué)習(xí)項(xiàng)目,包含項(xiàng)目原理以及源碼,每一個(gè)項(xiàng)目實(shí)例都附帶有完整的代碼+數(shù)據(jù)集。 正在更新中~ ? ?? 我的項(xiàng)目環(huán)境: 平臺(tái):Windows10 語言環(huán)境:python3.7 編譯器:PyCharm PyTorch版本:

    2023年04月16日
    瀏覽(26)
  • R——《時(shí)間序列分析——基于R》第5章 無季節(jié)效應(yīng)的非平穩(wěn)序列分析 習(xí)題1

    R——《時(shí)間序列分析——基于R》第5章 無季節(jié)效應(yīng)的非平穩(wěn)序列分析 習(xí)題1

    目錄 1.導(dǎo)入數(shù)據(jù)并繪圖 2.進(jìn)行一階差分并繪制該序列時(shí)序圖 3.判斷該序列的平穩(wěn)性與純隨機(jī)性 4.考察該序列的自相關(guān)系數(shù)和偏自相關(guān)系數(shù)的性質(zhì) 5.選擇適當(dāng)模型擬合該序列的發(fā)展 5.1. ARIMA(1,1,0)不帶漂移項(xiàng) 5.2. ARIMA(1,1,0)帶漂移項(xiàng) 5.3. ARIMA(0,1,1)不帶漂移項(xiàng)? ?5.4. ARIMA(0,1,1)帶漂移項(xiàng)

    2024年02月08日
    瀏覽(37)
  • 基于LSTM深度學(xué)習(xí)網(wǎng)絡(luò)的時(shí)間序列分析matlab仿真

    基于LSTM深度學(xué)習(xí)網(wǎng)絡(luò)的時(shí)間序列分析matlab仿真

    目錄 1.算法運(yùn)行效果圖預(yù)覽 2.算法運(yùn)行軟件版本 3.部分核心程序 4.算法理論概述 5.算法完整程序工程 ? matlab2022a ? ? ? ?LSTM是一種循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的變體,專門設(shè)計(jì)用于處理序列數(shù)據(jù)。LSTM網(wǎng)絡(luò)通過記憶單元和門控機(jī)制來捕捉時(shí)間序列中的長期依賴關(guān)系,避免了傳統(tǒng)RNN中

    2024年02月12日
    瀏覽(27)
  • GEE:基于GLDAS數(shù)據(jù)集分析土壤濕度的時(shí)間序列變化

    GEE:基于GLDAS數(shù)據(jù)集分析土壤濕度的時(shí)間序列變化

    作者:CSDN @ _養(yǎng)樂多_ 本篇博客將介紹如何使用Google Earth Engine(GEE)進(jìn)行土壤濕度數(shù)據(jù)的分析。我們將使用NASA GLDAS(Global Land Data Assimilation System)數(shù)據(jù)集,其中包括了關(guān)于土壤濕度的信息。通過該數(shù)據(jù)集,我們將了解土壤濕度在特定區(qū)域和時(shí)間段內(nèi)的變化,并生成時(shí)間序列圖

    2024年02月07日
    瀏覽(18)
  • 時(shí)間序列分析——基于R(第2版)—第2章習(xí)題答案

    2.2 1975-1980年夏威夷島莫那羅亞火山每月釋放的CO2數(shù)據(jù) 繪制時(shí)序圖,并判斷該序列是否平穩(wěn) 計(jì)算該序列的樣本自相關(guān)系數(shù) 繪制自相關(guān)圖,并解釋該圖形 自相關(guān)系數(shù)長期位于0軸的一邊,這是具有單調(diào)趨勢序列的典型特征;同時(shí)呈現(xiàn)出正弦波動(dòng)規(guī)律,這是具有周期性變化規(guī)律的

    2024年02月07日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包