[R-sig-Geo] Different values from raster::Moran() and spdep::moran.test()

Roger Bivand Roger.Bivand at nhh.no
Fri Nov 20 20:51:57 CET 2015


On Fri, 20 Nov 2015, Agustin Lobo wrote:

> Using the testGrid example provided by
> https://stat.ethz.ch/pipermail/r-sig-geo/2014-December/022106.html
>
> lng <- rep(seq(0, 7, by=1), 8)
> counter = 1
> subCounter = 0
> startNum = 0
> lat = NULL
> while (counter < 65) {
>  if (subCounter == 8) {
>    startNum = startNum + 1
>    subCounter = 0
>  }
>  lat = c(lat, startNum)
>  subCounter = subCounter + 1
>  counter = counter + 1
> }
> # now add the black/ white chessboard pattern
> chess <- rep(c(0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0), 4)
> gridNum <- seq(1:64)
> testGrid <- data.frame(gridNum, lat, lng, chess)
>
> I define
> require(raster)
> rtestGrid <- rasterFromXYZ(testGrid[,2:4])
> plot(rtestGrid)
>
> and run
>
> Moran(rtestGrid)
> which results on:
>
> [1] 0.07438017

The raster::Moran function does:

Moran(rtestGrid, w = matrix(1, 3, 3))

which makes i its own neighbour. The spdep::moran.test result can be 
reproduced by using:

Moran(rtestGrid, w=matrix(c(1,1,1,1,0,1,1,1,1), 3, 3))

that is, omitting i from the set of its neighbours.

Equivalently,

moran.test(testGrid$chess, nb2listw(include.self(nb8q), style="B"),
   alternative="two.sided")

in spdep::moran.test gives the default raster::Moran result.

The rook neighbours definition on ?raster::Moran is defined without the 
self-neighbour relationship (arguably correctly). I would suggest that the 
defaults for raster::Moran, Geary, MoranLocal and GearyLocal should be 
changed from w=matrix(1, 3, 3) to omit the self-neighbour on the central 
cell.

Hope this clarifies,

Roger

>
> while
>
> require(spdep)
> nb8q <- cell2nb(8, 8, type="queen", torus=FALSE)
> moran.test(testGrid$chess, nb2listw(nb8q, style="B"),alternative="two.sided")
>
> which results on:
> Moran's I test under randomisation
>
> data:  testGrid$chess
> weights: nb2listw(nb8q, style = "B")
>
> Moran I statistic standard deviate = -0.7714, p-value = 0.4404
> alternative hypothesis: two.sided
> sample estimates:
> Moran I statistic       Expectation          Variance
>     -0.066666667      -0.015873016       0.004335237
>
> Considering the grid is regular, a negative I value makes sense.
>
> Why is Moran() so different?
>
> Thanks
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list