用R语言随机生成AR,MA,ARMA,ARIMA模型的命令,画出acf,pacf图

set.seed(10);x=rnorm(150)
par(mfrow=c(1,2))#一行两列,两个图
ts.plot(x);acf(x)


set.seed(101);
x=NULL;for(i in 1:200)x=c(x,0.5-0.3*i+rnorm(1))#随机产生数
par(mfrow=c(2,2));ts.plot(x);#画时间序列图
acf(x);ts.plot(diff(x));acf(diff(x))#diff(x)是对原序列进行差分


set.seed(10)
#产生63个ms(2)随机数
rw<-arima.sim(n=63,list(ma=c(0.5,0.3)))
#产生63个符合arma(2,2)模型的数字
arima.sim(n = 63, list(ar = c(0.8897, -0.4858), ma = c(-0.2279, 0.2488)),
          sd = sqrt(0.1796))
#产生ARIMA(1,1,1),第一个数字是AR部分,第二个数字是嗲表差分几次
#第三个数字是MA部分

arima.sim(list(order = c(1,1,1), ar = 0.7,ma=0.3), n = 200)


#acf()函数中plot是一个逻辑值,他的用法,如果是T,默认也是T就会
#把acf值给画出来,如果是f,ACF值就不会被画出来
set.seed(10);x=rnorm(150)
par(mfrow=c(1,2))#一行两列,两个图
ts.plot(x);acf(x,plot=T)

#注意acf(x,plot=T),acf(x,plot=F)区别



acf(x,plot=T,main="dai T")#如果是f就不会画出图


set.seed(63010)
tw<-arima.sim(n=200,list(ma=c(0.5,0.3)))
tw1<-acf(tw,plot = F)
#==两个等号是恒等于,是个全局变量,一个等号就是赋值,注意theta的书写
plot(tw1,type="o",pch=16,main=expression(paste("ma(2):",theta[1]==0.5,",",theta[2]==0.3)))

plot(acf(arima.sim(n=200,list(ma=c(0.5,0.3))),plot = F),type="o",pch=16,main=expression(paste("ma(2):",theta[1]==0.5,",",theta[2]==0.3)))


#main=expression(paste("ma(2):",theta[1]==0.5,",",theta[2]==0.3)),注意这个expression的用法

#模拟arma(2,2)模型
tw<-arima.sim(n=200,list(ar=c(0.5,0.3),ma=c(0.5,0.3)))
tw1<-acf(arima.sim(n=200,list(ar=c(0.5,0.3),ma=c(0.5,0.3))),plot = F)
plot(tw1,type="o",pch=16,main=expression(paste("arma(2,2):",phi[1]==0.5,",",phi[2]==0.3,",",
                theta[1]==0.5,",",theta[2]==0.3)),cex.main=0.7)
#连着写
plot(acf(arima.sim(n=200,list(ar=c(0.5,0.3),ma=c(0.5,0.3))),plot = F),
     type="o",pch=16,main=expression(paste("arma(2,2):",phi[1]==0.5,",",

phi[2]==0.3,",",theta[1]==0.5,",",theta[2]==0.3)),cex.main=0.7)


#模拟arma(2,2)模型画pacf图,只需要在上面的acf换成pacf值就行了
tw<-arima.sim(n=200,list(ar=c(0.5,0.3),ma=c(0.5,0.3)))
tw1<-pacf(arima.sim(n=200,list(ar=c(0.5,0.3),ma=c(0.5,0.3))),plot = F)
plot(tw1,type="o",pch=16,main=expression(paste("arma(2,2):",phi[1]==0.5,",",phi[2]==0.3,",",
                                               theta[1]==0.5,",",theta[2]==0.3)),cex.main=0.7)
#连着写
plot(pacf(arima.sim(n=200,list(ar=c(0.5,0.3),ma=c(0.5,0.3))),plot = F),
     type="o",pch=16,main=expression(paste("arma(2,2):",phi[1]==0.5,",",

                                           phi[2]==0.3,",",theta[1]==0.5,",",theta[2]==0.3)),cex.main=0.7)


#模拟ARIMA模型,并画出acf、pacf图

set.seed(63010)
par(mfrow=c(1,3))
tw<-arima.sim(n=200,list(order=c(1,1,1),ar=0.3,d=1,ma=0.4))
tw1<-acf(tw,plot=F)
tw2<-pacf(tw,plot=F)
plot(tw,type="o",pch=16,ylab="tw")
title(expression(paste("arima(1,1,1):",phi[1]==0.4,",",theta[1]==0.4,cex.main=0.7)))
plot(tw1,type="o",pch=16,ylab="acf图")
plot(tw2,type="o",pch=16,ylab="pacf图")
#title是在图画好之后加标题

title(expression(paste("arima(1,1,1):",phi[1]==0.4,",",theta[1]==0.4,cex.main=0.7)))


#注意ar部分,与ma部分系数的书写方式














猜你喜欢

转载自blog.csdn.net/weixin_42099676/article/details/80490353