0

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)
user1988
  • 29
  • 1
  • 7
  • If you look at the documentation for [`Moran.I`](https://www.rdocumentation.org/packages/ape/versions/5.4-1/topics/Moran.I) and [`moran.test`](https://www.rdocumentation.org/packages/spdep/versions/1.1-5/topics/moran.test) you'll see that they have different defaults for alternative. – Ian Campbell Jan 15 '21 at 02:46
  • @IanCampbell thank you, even when I change the alternative of Moran.I to greater as moran.test I still get different results (see my updated post). – user1988 Jan 15 '21 at 02:51

1 Answers1

1

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    
neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Thank you very much for this information. Sorry for my stupid question but what is the difference between style "M" and "W"? – user1988 Jan 15 '21 at 16:17
  • Not my area, but it's to do with how weights are calculated. See [the documentation for nb2listw](https://www.rdocumentation.org/packages/spdep/versions/1.1-5/topics/nb2listw). Please vote for and/or accept the answer if it solves your issue. – neilfws Jan 15 '21 at 20:34