日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

时间序列R语言操作——非平稳时间序列变平稳

發(fā)布時間:2023/12/31 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 时间序列R语言操作——非平稳时间序列变平稳 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 前言
  • 一、方差不平穩(wěn)
  • 二、具有趨勢性
    • 1、確定性趨勢的刪除——趨勢擬合法
    • 2、隨機趨勢的刪除——差分法
    • 3、未知趨勢的刪除——平滑法
      • 1. 移動平均法
      • 2. 指數(shù)平滑法
    • 4、季節(jié)性
      • 1.Method1: 減掉趨勢成分后取平均
      • 2.Method2:使用啞元變量做回歸分析


前言

時間序列為什么要平穩(wěn)后建模?

  • 時間序列是平穩(wěn)的,則沒有任何預測價值,因為均值不變,所以實際上人們只會對非平穩(wěn)時間序列感興趣
  • 之所以研究平穩(wěn)時間序列,因為總希望可以通過差分等方式把非平穩(wěn)序列變成平穩(wěn)的,而平穩(wěn)時間序列可以通過數(shù)學公式予以解釋

一、方差不平穩(wěn)

最簡單的是做對數(shù)變換,使得前后方差沒有那么變化。

#Log transform: variance stationary data(AirPassengers) AirPassengers par(mfrow=c(1,2)) plot(AirPassengers) plot(log(AirPassengers))

二、具有趨勢性

1、確定性趨勢的刪除——趨勢擬合法

最簡單的處理方法就是考慮均值函數(shù)可以由一個時間的確定性函數(shù)來描述,比如可以用回歸模型來描述。對于一個有線性趨勢的序列,可以做一個線性模型。

  • Linear trend
##read the gasline data ##Abraham and Ledolter (1983) on the ##monthly gasoline demand in Ontario over the period 1960 - 1975. gas = scan("gas.dat") ## note that read.table() would give an error here since the data is not in rectangular format. gas.ts = ts(gas, frequency = 12, start = 1960) gas.ts plot(gas.ts, main = "Gasoline demand in Ontario", ylab = "Million gallons") #fit trend:OLS with constant and trend time = 1:length(gas.ts) fit = lm(gas.ts~time) summary(fit)#R-squared: 0.8225 par(mfrow=c(2,1)) plot(time, gas.ts, type='l', xlab='months', ylab='Million gallons gasoline', main="OLS fit") legend(.1, 256000, c("Time plot", "OLS fit"), col = c(1,2), text.col = "black", merge = TRUE, lty=c(1,1)) abline(fit,col=2,lwd=2) plot(time, fit$resid, type='l', xlab="months", ylab="Residuals",main="Residuals OLS [yt-fit(yt)]") abline(a=0, b=0)#殘差在0附近點波動 #savePlot("plot of Residuals_gas",type="pdf")

  • Quadratic trend
fit2 = lm(gas.ts~time+I(time^2)) summary(fit2)#R-squared: 0.8327 names(fit2) par(mfrow=c(2,1)) plot(time, gas.ts, type='l', xlab='months', ylab='Million gallons gasoline',main="OLS fit") legend(.1, 256000, c("Time plot", "OLS fit"), col = c(1,2), text.col = "black",merge = TRUE, lty=c(1,1)) lines(time,fitted(fit2),col=2) plot(time, fit2$resid, type='l', xlab="months", ylab="Residuals",main="Residuals OLS [yt-fit(yt)]") abline(a=0, b=0)

  • Polynomial trend

2、隨機趨勢的刪除——差分法

隨機趨勢的刪除——差分法。

  • 一階差分可以使線性趨勢的序列實現(xiàn)趨勢平穩(wěn)
Zt = as.ts(rnorm(1000, sd = 20)) RW1 = as.ts(cumsum(Zt)) par(mfrow = c(2,2)) plot(RW1, main = "Random walk") acf(RW1, main = "Correlogram random walk") Yt=as.ts(diff(RW1)) plot(Yt,main='Diff(RW1)') acf(Yt)

  • 序列蘊含著曲線趨勢,通常低階(二階或三階)差分可提取出曲線趨勢的影響 ,這里效果不好的話,可以先取對數(shù),再做差分
  • 對于蘊含著固定周期的序列進行步長為周期長度的差分運算,通??梢暂^好地提取周期信息
  • 差分的階數(shù)要適度,避免過差分

3、未知趨勢的刪除——平滑法

平滑法——顧名思義,使序列變平滑。 期望結(jié)果,是顯示出趨勢變化的規(guī)律。

1. 移動平均法

假定在短的時間間隔內(nèi),序列的取值是比較穩(wěn)定的,序列的大小差異主要是由隨機波動造成的。用一段時間間隔內(nèi)的平均值作為某一期的估計值。

  • 簡單移動平均
##Moving average Model Vt5 = filter(Zt, rep(1/5, 5), sides = 2) Vt21 = filter(Zt, rep(1/21, 21), sides = 2) par(mfrow = c(2,2)) plot(Vt5, main = "Moving 5-point average of above white noise series") acf(Vt5, main = "Correlogram 5-point moving average", lag.max = 40, na.action = na.pass) plot(Vt21, main = "Moving 21-point average of above white noise series") acf(Vt21, main = "Correlogram 21-point moving average", lag.max = 40, na.action = na.pass) par(mfrow = c(1,1))

  • 權(quán)重移動平均

2. 指數(shù)平滑法

4、季節(jié)性

Seasonality means an effect that happens at the same time and with the same magnitude and direction every year. 從而使得經(jīng)過季節(jié)調(diào)整的序列能夠較好的反應社會經(jīng)濟指標運行基本態(tài)勢。

1.Method1: 減掉趨勢成分后取平均

## Investigating seasonality library(fields) z = matrix(fit$resid, ncol=12, byrow=TRUE) colnames(z) = c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") bplot(z, xlab="Month", ylab="Detrended demand of gasoline", main="Annual seasonality of the detrended demand of gasoline") mean=matrix(0, nrow=ncol(z), ncol=1) #mean for each month for(i in 1:ncol(z)){mean[i,1]=mean(z[,i]) } Seas = as.matrix(rep(mean,16)) # matrix with seasonal term des.gas = fit$resid - Seas # detrended and now deseasonized series par(mfrow=c(2,1)) plot(fit$resid, type='l', xlab="months", ylab="Residuals", main="Residuals OLS [yt-fit(yt)]") abline(a=0, b=0) plot(des.gas, type='l', xlab="months", ylab="Deseasonized residuals", main="Deseasonized ResidualsOLS") abline(a=0, b=0)

2.Method2:使用啞元變量做回歸分析

##seasonal modeling and estimating seasonal trends ##such as for the average monthly temperature data library(TSA) #install package TSA win.graph(width=4.875, height=2.5,pointsize=8) data(tempdub); plot(tempdub,ylab='Temperature',type='o') month.=season(tempdub) # period added to improve table display model2=lm(tempdub~month.) # -1 removes the intercept term summary(model2) Call: lm(formula = tempdub ~ month.)Residuals:Min 1Q Median 3Q Max -8.2750 -2.2479 0.1125 1.8896 9.8250 Coefficients:Estimate Std. Error t value Pr(>|t|) (Intercept) 16.608 0.987 16.828 < 2e-16 *** month.February 4.042 1.396 2.896 0.00443 ** month.March 15.867 1.396 11.368 < 2e-16 *** month.April 29.917 1.396 21.434 < 2e-16 *** month.May 41.483 1.396 29.721 < 2e-16 *** month.June 50.892 1.396 36.461 < 2e-16 *** month.July 55.108 1.396 39.482 < 2e-16 *** month.August 52.725 1.396 37.775 < 2e-16 *** month.September 44.417 1.396 31.822 < 2e-16 *** month.October 34.367 1.396 24.622 < 2e-16 *** month.November 20.042 1.396 14.359 < 2e-16 *** month.December 7.033 1.396 5.039 1.51e-06 *** --- Signif. codes: 0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 3.419 on 132 degrees of freedom Multiple R-squared: 0.9712, Adjusted R-squared: 0.9688 F-statistic: 405.1 on 11 and 132 DF, p-value: < 2.2e-16

總結(jié)

以上是生活随笔為你收集整理的时间序列R语言操作——非平稳时间序列变平稳的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。