自相关散点图为特殊滞后

时间:2014-02-03 10:17:03

标签: r

我正在使用acf函数来计算自相关。我希望有一个特殊滞后的散点图,例如滞后2.是否可以使用acf函数执行此操作。

是否有任何软件包可以帮助我获得每个滞后的散点图?

1 个答案:

答案 0 :(得分:4)

lag.plot1=function(data1,max.lag=1,corr=TRUE,smooth=FALSE){ 
   name1=paste(deparse(substitute(data1)),"(t-",sep="")
   name2=paste(deparse(substitute(data1)),"(t)",sep="")
   data1=as.ts(data1)
   max.lag=as.integer(max.lag)
   prow=ceiling(sqrt(max.lag))
   pcol=ceiling(max.lag/prow)
   a=acf(data1,max.lag,plot=FALSE)$acf[-1]
  par(mfrow=c(prow,pcol), mar=c(2.5, 4, 2.5, 1), cex.main=1.1, font.main=1)
  for(h in 1:max.lag){                       
   plot(lag(data1,-h), data1, xy.labels=FALSE, main=paste(name1,h,")",sep=""), ylab=name2, xlab="") 
    if (smooth==TRUE) 
    lines(lowess(ts.intersect(lag(data1,-h),data1)[,1],
                 ts.intersect(lag(data1,-h),data1)[,2]), col="red")
    if (corr==TRUE)
    legend("topright", legend=round(a[h], digits=2), text.col ="blue", bg="white", x.intersp=0)
   }
}

lag.plot1(dat,12,smooth=TRUE)  

enter image description here

完整调用为lag.plot1(x,m,corr=TRUE,smooth=TRUE),它会生成x(t-h)x(t)的散点图网格,其中h = 1,...,m,以及{{1} } autocorrelationblue中的值。如果您不想要任何相关或行,只需使用lowess fit in red

R's lag.plot

enter image description here