Reputation: 3057
I am using acf
function to calculate Autocorrelation. I want to have a scatter plot for a special lag for example lag 2. Does it possible to do it with acf
function.
Is there any package that help me to have a scatter plot for each lag?
Upvotes: 2
Views: 2926
Reputation: 4335
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)
The full call is lag.plot1(x,m,corr=TRUE,smooth=TRUE)
and it will generate a grid of scatterplots of x(t-h)
versusx(t)
for h = 1,...,m, along with the autocorrelation
values in blue
and a lowess fit in red
.If you don't want any correlations or lines, simply use R's lag.plot
library(forecast)
lag.plot(LakeHuron,lags=3,do.lines=FALSE)
Upvotes: 5