01章
install.packages("forecast")
library(forecast)
price=c(101,82,66,35,31,7)
price=ts(price,start=c(2005,1),frequency=12)
price
price=scan()
price=ts(price,start=c(2005,1),frequency=12)
price
x=read.table("E:/data/file1.csv",sep=",",header=T)
x
x$Year
y=log(x$yield)
y
z=subset(x,Year>1925,select=yield)
z
a=c(1:7)
a[4]<NA
a
y1=na.approx(a)
y1
y2=na.spline(a)
y2
02章
yield=c(15.2,16.9,15.3,14.9,15.7,15.1,16.7)
yield=ts(yield,start=1884)
plot(yield)
plot(yield,type="p")
plot(yield,type="o")
plot(yield,type="o",pch=17)
plot(yield,lty=2)
plot(yield,lwd=2)
plot(yield,main="1884-1890 年英格兰和威尔士地区小麦平均亩产量",xlab="年份",ylab="亩产量")
plot(yield,xlim=c(1886,1890))
plot(yield,ylim=c(15,16))
plot(yield)
abline(v=1887,lty=2)
plot(yield)
abline(v=c(1885,1889),lty=2)
plot(yield)
abline(h=c(15.5,16.5),lty=2)
acf(yield)
sha=read.table("E:/data/file4.csv",sep=",",header=T)
output=ts(sha$output,start=1964)
plot(output)
a=read.table("E:/data/file5.csv",sep=",",header=T)
milk=ts(a$milk,start=c(1962,1),frequency=12)
plot(milk)
b=read.table("E:/data/file6.csv",sep=",",header=T)
temp=ts(b$temp,start=1949)
plot(temp)
acf(output,lag=25)
acf(milk)
acf(temp)
white_noise=rnorm(1000)
white_noise=ts(white_noise)
plot(white_noise)
acf(white_noise)
Box.test(white_noise,lag=6)
Box.test(white_noise,lag=12)
for(i in 1:2) print(Box.test(white_noise,lag=6*i))
for(i in 1:2) print(Box.test(temp,lag=6*i))
for(i in 1:2) print(Box.test(temp,lag=6*i))
03章
04章
05章
a=read.table("E:/data/file4.csv",",",header=T)
x=ts(a$output,start=1964)
x.dif=diff(x)
plot(x.dif)
b=read.table("E:/data/file15.csv",",",header=T)
x=ts(b$vehicle,start=1950)
plot(x)
x.dif=diff(x)
plot(x.dif)
x.dif2=diff(x,1,2)
plot(x.dif2)
c=read.table("E:/data/file16.csv",",",header=T)
x=ts(c$export,start=c(2001,1),frequency=12)
plot(x)
x.dif=diff(x)
plot(x.dif)
x.dif1_12=diff(diff(x),12)
plot(x.dif1_12)
x=arima.sim(n=1000,list(order=c(0,1,0)),sd=10)
plot(x)
d=read.table("E:/data/file17.csv",",",header=T)
x=ts(d$index,start=1952)
plot(x)
x.dif=diff(x)
plot(x.dif)
acf(x.dif)
pacf(x.dif)
x.fit=arima(x,order=c(0,1,1))
x.fit
for(i in 1:2) print(Box.test(x.fit$residual,lag=6*i))
library(zoo)
library(forecast)
d=read.table("E:/data/file17.csv",",",header=T)
x=ts(d$index,start=1952)
x.fit=arima(x,order=c(0,1,1))
x.fore=forecast(x.fit,h=10)
x.fore
plot(x.fore)
e=read.table("E:/data/file18.csv",",",header=T)
x=ts(e$fertility,start=1917)
plot(x)
x.dif=diff(x)
plot(x.dif)
acf(x.dif)
pacf(x.dif)
x.fit=arima(x,order=c(4,1,0),transform.pars=F,fixed=c(NA,0,0,NA))
x.fit
for(i in 1:2) print(Box.test(x.fit$residual,lag=6*1))
x.fore=forecast(x.fit,h=5)
x.fore
plot(x.fore)
f=read.table("E:/data/file19.csv",",",header=T)
x=ts(f$unemployment_rate,start=c(1962,1),frequency=4)
plot(x)
x.dif=diff(diff(x),4)
plot(x.dif)
acf(x.dif)
pacf(x.dif)
x.fit=arima(x,order=c(4,1,0),seasonal=list(order=c(0,1,0),period=4),transform.par=F,fixed=c(NA,0,0,NA))
x.fit
for(i in 1:2) print(Box.test(x.fit$residual,lag=6*i))
x.fore=forecast(x.fit,h=12)
x.fore
plot(x.fore)
g=read.table("E:/data/file20.csv",sep=",",header=T)
x=ts(g$unemployment_rate,start=c(1948,1),frequency=12)
plot(x)
x.dif=diff(diff(x),12)
plot(x.dif)
acf(x.dif)
pacf(x.dif)
x.fit=arima(x,order=c(1,1,1),seasonal=list(order=c(0,1,0),period=12))
for(i in 1:2) print(Box.test(x.fit$residual,lag=6*i))
x.fit=arima(x,order=c(1,1,1),seasonal=list(order=c(0,1,1),period=12))
x.fit
for(i in 1:2) print(Box.test(x.fit$ residual,lag=6*i))
d=read.table("E:/data/file17.csv",",",header=T)
x=ts(d$index,start=1952)
t=c(1:37)
x.fit1=lm(x~t)
summary(x.fit1)
xlag=x[2:37]
x2=x[1:36]
x.fit2=lm(x2~xlag)
summary(x.fit2)
fit1=ts(x.fit1$fitted.value,start=1952)
fit2=ts(x.fit2$fitted.value,start=1952)
plot(x,type="p",pch=8)
lines(fit1,col=2)
lines(fit2,col=4)
library(lmtest)
dwtest(x.fit1)
dwtest(x.fit2,order.by =xlag)
acf(x.fit1$residual)
pacf(x.fit1$residual)
r.fit=arima(x.fit1$residual,order=c(2,0,0),include.mean=F)
r.fit
for(i in 1:2) print(Box.test(r.fit$residual,lag=6*i))
h=read.table("E:/data/file21.csv",sep=",",header=T)
x=ts(h$yield_rate,start=c(1963,4),frequency=12)
plot(x)
x.dif=diff(x)
plot(x.dif)
plot(x.dif^2)
lnx=log(x)
plot(lnx)
dif.lnx=diff(lnx)
plot(dif.lnx)
for(i in 1:2) print(Box.test(dif.lnx,lag=6*i))
k=read.table("E:/data/file22.csv",sep=",",header=T)
x=ts(k$returns,start=c(1926,1),frequency=12)
plot(x)
plot(x^2)
for(i in 1:5) print(ArchTest(x,lag=i))
for (i in 1:5) print(Box.test(x^2,lag=i))
x.fit=garch(x,order=c(0,3))
summary(x.fit)
x.pred=predict(x.fit)
plot(x.pred)
plot(x)
lines(x.pred[,1],col=2)
lines(x.pred[,2],col=2)
abline(h=1.96*sd(x),col=4,lty=2)
abline(h=-1.96*sd(x),col=4,lty=2)
w=read.table("E:/data/file23.csv",sep=",",header=T)
x=ts(w$exchange_rates,start=c(1979,12,31),frequency=365)
plot(x)
d=read.table("E:/datali/习题5.1数据.csv",sep=",",header=T)
summary(d)
x=ts(d)
plot(x)
Box.test(x)
x.dif=diff(x);plot(x.dif)
acf(x.dif)
pacf(x.dif)
x.fit=arima(x,order=c(1,1,1));x.fit
library(forecast);librart(zoo)
x.fit2=auto.arima(x);x.fit2
x.fore1=forecast(x.fit);x.fore1
x.fore2=forecast(x.fit);x/fore2
plot(x.fore1,J=283.4);plot(x.fore2)
line(x.fore1)
lines
plot(x.fore1);abline(h=283.4,col=2)
e=read.table("E:/datali/习题5.2数据.csv",sep=",",header=T)
View(e)
y=ts(e$货运量,start=1949)
plot(y)
y.dif=diff(y);plot(y.dif)
acf(y.dif)
pacf(y.dif)
y.fit1=arima(y,order=c(1,1,1));y.fit1
y.fit2=auto.arima(y);y.fit2
y.dif2=diff(y.dif);plot(y.dif2)
acf(y.dif2)
pacf(y.dif2)
06章
附