Skip to content
Prev 26682 / 29559 Next

Spatial autocorrelation help

Hi Orcun

I am not quite sure if im doing this correctly but I do understand that i first need to check spatial autocorrelation occurs in my data. so i did this below steps and after that checked it again in best model residuals

# Another approach to find SAC by creating neighbors first, then get distances between each point and neighbors, then
# inverse the distance and then check the SAC using mora's I
coord <- cbind(data$long, data$lat)
coords <- coordinates(coord)

# creates a matrix of nn indexes - knearneigh to get nearest neighbors
nn5 <- knearneigh(coords, k=5)  
mi5.nlist <- knn2nb(nn5, row.names = NULL, sym=FALSE)

# creates a sp weights matrix
mi5.sw <- nb2listw(mi5.nlist) 

# cal moran's I using distance as weights
# calculates the distance
mi5.dist <- nbdists(mi5.nlist, coords) 

# now invert the distnace to determine weights (closer =higher)
mi5.dist1 <- lapply(mi5.dist, function(x){ifelse(is.finite(1/x), (1/x), (1/0.001))})
mi5.dist2 <- lapply(mi5.dist, function(x){ifelse(is.finite(1/x^2), (1/x^2), (1/0.001^2))})

# check the distance between the distribution
summary(unlist(mi5.dist1)) 

# now create sp weights matrix weighted on distance
mi5.d1sw <- nb2listw(mi5.nlist, glist=mi5.dist1)
mi5.d2sw <- nb2listw(mi5.nlist, glist=mi5.dist2)

# morans test
moran.test(as.numeric(data$response), mi5.d1sw)
moran.test(as.numeric(data$response), mi5.d2sw)

This first moran?s test gives :
Moran I statistic standard deviate = 2.0328, p-value = 0.02104
alternative hypothesis: greater
sample estimates:
Moran I statistic       Expectation          Variance 
      0.105850408      -0.004608295       0.002952729 

Second morans test gives:

Moran I statistic standard deviate = 2.3848, p-value = 0.008545
alternative hypothesis: greater
sample estimates:
Moran I statistic       Expectation          Variance 
      0.154097396      -0.004608295       0.004428848 

And both indicates presence of spatial autocorrelation in the raw data.

Should i account for this in all models or if i perform logistic mixed model it is fine??help is much appreciated. Difficult to understand what the problem is and how to solve it