一、时间序列(2_2)AR、MA、ARMA简单编程(R语言)

在这里插入图片描述
在这里插入图片描述

library(TSA)
#eg.1
#ar(1)
set.seed(1)
series.all=arima.sim(n=48,list(ar=0.8))+100
future=window(series.all,start=41)
series=window(series.all,end=40)
model1=arima(series,order=c(1,0,0))
model1
result=plot(model1,n.ahead=8,ylab='Series&Forecasts',col=NULL,pch=19)
abline(h=coef(model1)[names(coef(model1))=='intercept'])
forecast=result$pred
cbind(future,forecast)
plot(model1,n.ahead=8,ylab='Seires,Forecasts, Actual&Limits',pch=19)
points(x=(41:48),y=future,pch=3)
abline(h=coef(model1)[names(coef(model1))=='intercept'])

#eg.2
#ma2

set.seed(2)
series.all=arima.sim(n=36,list(ma=c(-1,0.6)))+100
actual=window(series.all,start=33)
series=window(series.all,end=32)
model2=arima(series,order=c(0,0,2))
model2
result=plot(model2,n.ahead=4,ylab='Series&Forecasts',col=NULL,pch=19)
abline(h=coef(model2)[names(coef(model2))=='intercept'])
forecast=result$pred
cbind(actual,forecast)
plot(model2,n.ahead=4,ylab='Seires,Forecasts, Actual&Limits',type='o',pch=19)
points(x=(33:36),y=actual,pch=3)
abline(h=coef(model2)[names(coef(model2))=='intercept'])

例题三:analysis of U.S.GNP growth rate series
Notes:
Use 5% level in all tests.
The notation 𝜌i
is the lag } i autocorrelation coefficient
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#### Problem 1:daily VIX index of the Chicago Board Options Exchange (CBOE)
#remove those missing values:
#unit root
#ACF and PACF of the growth rate

da=read.table("VIXCLS.txt",header=T)
head(da)
idx=c(1:nrow(da))[da$VALUE < 0.1]
VIX=da$VALUE[-idx]
lvix=log(VIX)

#a
par(mfcol=c(2,1))
tdx=c(1:length(VIX))/252+1990
#252 trading days a year
plot(tdx,VIX,xlab='year',ylab='VIX',type='l')
plot(tdx,lvix,xlab='year',ylab='ln(VIX)',type='l')

#b
require(fUnitRoots)
m1=ar(diff(lvix),method="mle")
m1$order
adfTest(lvix,lags=11,type="nc")
#no drift
#null hypothesis cannot be rejected.
#b1
library(tseries)
adf.test(lvix)
#c
t.test(diff(lvix))
#VIX should not have drift.Otherwise, there exists arbitrage opportunity.
#d
zt=diff(lvix)
par(mfcol=c(2,1))
acf(zt)
pacf(zt)
Box.test(zt,lag=10,type='Ljung')
#serial correlations in the zt series.

### Problem 2:zt of Problem 1.
#AR fit,forecast
#a
m2=arima(zt,order=c(11,0,0),include.mean=F)
m2
tsdiag(m2,gof=20)##对估计进行诊断,判断残差是否为白噪声 
m3=arima(lvix,order=c(11,1,0))
m3
p3=predict(m3,3)
p3
#c
up=p3$pred+1.96*p3$se
lb=p3$pred-1.96*p3$se
pintv=cbind(lb,up)
print(round(pintv,3))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值