get.acf<-function(maxlag,oldei,the.lags) { #standardize variable ei<-(oldei-mean(oldei))/sd(oldei) #initialize variables N.list<-length(ei) ACF.list<-1 total.sum<-0 total.count<-0 #cycle through all desired lags for (i in 1:maxlag) { all.guys<-NULL full.list<-NULL #cycle through lag lists looking for desired lag for (j in 1:length(the.lags)) { cur.lag<-the.lags[[j]] #calculate numerator terms of Morans I first.ei<-ei[((1+j):(length(the.lags[[j]])+j))[the.lags[[j]]==i]] second.ei<-ei[(1:(length(the.lags[[j]])))[the.lags[[j]]==i]] cur.term<-first.ei*second.ei full.list<-c(full.list,cur.term) #determine which observations were used for this lag cur.guys<-unique(c(((1+j):(length(the.lags[[j]])+j))[the.lags[[j]]==i], (1:(length(the.lags[[j]])))[the.lags[[j]]==i])) all.guys<-unique(c(all.guys,cur.guys)) } #average sum of squares of all observations used bottom<-sum(ei[all.guys]^2)/length(ei[all.guys]) #average lagged sum of squares top<-sum(full.list)/length(full.list) cur.ACF<-top/bottom cur.N<-length(full.list) ACF.list<-c(ACF.list,cur.ACF) N.list<-c(N.list,cur.N) } out.mat<-cbind(0:maxlag,ACF.list,N.list) colnames(out.mat)<-c('lag','ACF','N') out.mat }