圖形展示:
plot.zoo(x)
plot.xts(x)
plot.zoo(x, plot.type=”single”) #支持多個時間序列數(shù)據(jù)在一個圖中展示
plot(x, plot.type=”single”) #支持多個時間序列數(shù)據(jù)在一個圖中展示,僅對xts不行
1、自相關系數(shù)、偏自相關系數(shù)等
例題2.1
d=scan("sha.csv")
sha=ts(d,start=1964,freq=1)
plot.ts(sha)? ?#繪制時序圖
acf(sha,22)? ?#繪制自相關圖,滯后期數(shù)22
pacf(sha,22)??#繪制偏自相關圖,滯后期數(shù)22
corr=acf(sha,22)? ?#保存相關系數(shù)
cov=acf(sha,22,type = "covariance")? ?#保存協(xié)方差
2、同時繪制兩組數(shù)據(jù)的時序圖
d=read.csv("double.csv",header=F)
double=ts(d,start=1964,freq=1)
plot(double, plot.type = "multiple")? ?#兩組數(shù)據(jù)兩個圖
plot(double, plot.type = "single")? ???#兩組數(shù)據(jù)一個圖
plot(double, plot.type = "single",col=c("red","green"),lty=c(1,2)) #設置每組數(shù)據(jù)圖的顏色、曲線類型)
3、純隨機性檢驗
例題2.3續(xù)
d=scan("temp.csv")
temp=ts(d,freq=1,start=c(1949))
Box.test(temp, type="Ljung-Box",lag=6)
4、差分運算和滯后運算
diff
lag
5、模擬ARIMA模型的結果
arima.sim(n = 100, list(ar = 0.8))
plot.ts(arima.sim(n = 100, list(ar = 0.8)))? ?#會隨機產(chǎn)生一個包含100個隨機數(shù)的時序圖
plot.ts(arima.sim(n = 100, list(ar = -1.1)))? ?#非平穩(wěn),無法得到時序圖。
plot.ts(arima.sim(n = 100, list(ar = c(1,-0.5))))
plot.ts(arima.sim(n = 100, list(ar = c(1,0.5))))
arima.sim(n = 1000, list(ar = 0.5, ma = -0.8))
acf(arima.sim(n = 1000, list(ar = 0.5, ma = -0.8)),20)
pacf(arima.sim(n = 1000, list(ar = 0.5, ma = -0.8)),20)
【單位根檢驗】
#方法1
b=ts(read.csv("6_1.csv",header=T))
x=b[,1]
y=b[,1]
summary(ur.df(x,type="trend",selectlags="AIC"))
#方法2:單位根檢驗更好的函數(shù),加了畫圖的功能
library(fUnitRoots)
urdfTest(x)
#方法3:ADF檢驗的一個自編函數(shù)
library(urca)
#...
ur.df.01=function(x,lags=8){? ?
??#將三種ADF檢驗形式匯總的函數(shù)(結果和EVIEWS不一致)
??res=matrix(0,5,3)
??colnames(res)=c("無","含常數(shù)項","含常數(shù)項和趨勢項")
??rownames(res)=c("tau統(tǒng)計量","1%臨界值","5%臨界值",
? ?? ?? ?? ?? ?? ?"10%臨界值","是否穩(wěn)定(1/0)")
??types=c("none","drift","trend")
??for(i in 1:3){
? ? x.adf=ur.df(x,type=types,lags=lags,selectlags="AIC")
x.adf.1=x.adf@teststat??#統(tǒng)計量
x.adf.2=x.adf@cval? ?? ?#臨界值
? ? res[1,i]??=x.adf.1[1]
? ? res[2:4,i]=x.adf.2[1,]
? ? res[5,i]=if( abs(res[1,i]) > abs(res[3,i]) ) 1 else 0
??}
??return(res)
}
#...
ur.df.01(x)? ?? ?? ?? ???#對原序列進行判斷
【一般的ARIMA模型】
d=scan("a1.5.txt")? ?? ?? ?? ?? ?#導入數(shù)據(jù)
prop=ts(d,start=1950,freq=1)? ?? ?#轉化為時間序列數(shù)據(jù)
plot(prop)? ?? ?? ?? ?? ?? ? #作時序圖
acf(prop,12)? ?? ?? ?? ?? ???#作自相關圖,拖尾
pacf(prop,12)? ?? ?? ?? ?? ? #作偏自相關圖,1階截尾
Box.test(prop, type="Ljung-Box",lag=6)??
#純隨機性檢驗,p值小于5%,序列為非白噪聲
Box.test(prop, type="Ljung-Box",lag=12)
( m1=arima(prop, order = c(1,0,0),method="ML") )? ? #用AR(1)模型擬合,如參數(shù)method="CSS",估計方法為條件最小二乘法,用條件最小二乘法時,不顯示AIC。
( m2=arima(prop, order = c(1,0,0),method="ML", include.mean = F) ) #用AR(1)模型擬合,不含截距項。
tsdiag(m1)??#對估計進行診斷,判斷殘差是否為白噪聲








暫無數(shù)據(jù)