Search code examples
rspatialspatial-indexapespdep

Getting different test results between Moran.I and moran.test


I tried to run Moran's I test for the spatial autocorrelation test by using the function Moran.Ifrom the package ape and moran.test from the package spdep I got different results by applying the two methods on the same data. So at this point why we're getting such a difference and what is the most efficient method? See the following code:

library(ape) #For Moran.I
library(spdep) #For moran.test
Var <- rnorm(200,1, 1)
xy<- as.data.frame(cbind(rnorm(200,0, 1), (rnorm(200,0, 1))))
colnames(xy) <-c('X','Y')
dists <- as.matrix(dist(cbind(xy$X, xy$Y)))
dists.inv <- 1/dists
diag(dists.inv) <- 0
# TEST WITH  "Moran.I"
Moran.I(Var, dists.inv, alternative = "greater")
# TEST WITH  "moran.test"
lw <- mat2listw(dists.inv)
moran.test(Var, lw)

Solution

  • The two methods return the same result if you supply the argument style = "W" to mat2listw.

    As seen below: mi$observed has the same value as mt2$estimate[1].

    library(broom) # to tidy output of moran.test
    
    mi <- Moran.I(Var, dists.inv, alternative = "greater")
    mt1 <- moran.test(Var, mat2listw(dists.inv))
    mt2 <- moran.test(Var, mat2listw(dists.inv, style = "W"))
    
    str(mi)
    List of 4
     $ observed: num -0.0184
     $ expected: num -0.00503
     $ sd      : num 0.0106
     $ p.value : num 0.896
    
    
    tidy(mt1)
    
    # A tibble: 1 x 7
      estimate1 estimate2 estimate3 statistic p.value method                           alternative
          <dbl>     <dbl>     <dbl>     <dbl>   <dbl> <chr>                            <chr>      
    1   -0.0167  -0.00503  0.000199    -0.829   0.796 Moran I test under randomisation greater    
    
    tidy(mt2)
    
    # A tibble: 1 x 7
      estimate1 estimate2 estimate3 statistic p.value method                           alternative
          <dbl>     <dbl>     <dbl>     <dbl>   <dbl> <chr>                            <chr>      
    1   -0.0184  -0.00503  0.000112     -1.26   0.896 Moran I test under randomisation greater