[R-sig-Geo] Seeking help implementing nb2listwdist() to find inverse distance matrix.
Roger Bivand
Roger@B|v@nd @end|ng |rom nhh@no
Thu May 20 11:09:24 CEST 2021
On Wed, 19 May 2021, Jeffrey Boggs wrote:
> Hello,
>
> I am attempting to write R code for the spdep package to reproduce the Moran's I for point data examples found in David Ebdon (1985) Statistics In Geography, 2nd Edition on pages 161-162, Table 7.11
>
> I am unable to implement the nb2listdist() operation to make an inverse distance weighted matrix. Some of what follows is a solution posted by Dr. Bivand using a slightly different approach ( https://r.789695.n4.nabble.com/troubles-performing-Moran-I-test-td878799.html ). While I have a solution, I still want to also figure out how to use nb2listdistw, too. The relevant part of my code is:
>
> ############################
> id <- c("A","B","C","D","E")
> x <- c(2,1,3,4,5)
> y <- c(5,4,2,1,1)
> z <- c(8,5,10,42,20)
>
> ## Create inverse distance matrix using boring math.
> dij.matrix <- as.matrix(dist(cbind(x, y)))
> inv.dij.matrix <- 1/dij.matrix
> diag(inv.dij.matrix) <- 0
>
> install.packages( "spdep",
> "sp", dependencies = TRUE )
> library(spdep)
> library(sp)
>
> ## create sparse matrix for spdep::Moran.test
> lw <- mat2listw(inv.dij.matrix)
> lwW2 <- nb2listw(lw$neighbours, glist=lw$weights, style="raw")
This cannot work as there is no style "raw". I guess you mean "B".
>
> ## calculate Moran's I assuming Normality.
> moran.test(z, lwW2, randomisation = FALSE, alternative = "two.sided")
With
lwW2 <- nb2listw(lw$neighbours, glist=lw$weights, style="B")
> moran.test(z, lwW2, randomisation = FALSE, alternative = "two.sided")
Moran I test under normality
data: z
weights: lwW2
Moran I statistic standard deviate = 0.91961, p-value = 0.3578
alternative hypothesis: two.sided
sample estimates:
Moran I statistic Expectation Variance
-0.08248781 -0.25000000 0.03318058
>
> # Everything works! It also mirrors Ebdon's result.
>
> ## Create inverse distance matrix using spdep::nb2listwdist
> arf <- mat2listw(dij.matrix)
> arf2 <- nb2listwdist( arf$neighbours, z, type = "idw", style = "raw", longlat = NULL)
>
If you name the arguments, you see that you are missing x (an 'sp' 'sf',
or 'sfc' object) and passing the variable under analysis z, not the
points.
> arf2 <- nb2listwdist( arf$neighbours, x=SpatialPoints(cbind(x, y)),
type = "idw", style = "raw")
> moran.test(z, arf2, randomisation = FALSE, alternative = "two.sided")
Moran I test under normality
data: z
weights: arf2
Moran I statistic standard deviate = 0.91961, p-value = 0.3578
alternative hypothesis: two.sided
sample estimates:
Moran I statistic Expectation Variance
-0.08248781 -0.25000000 0.03318058
which looks the same to me.
Hope this helps,
Roger
> ## Calculate Moran's I again.
> moran.test(z, arf, randomisation = FALSE, alternative = "two.sided")
>
> ######################
>
> Sadly, when I run arf2 <- nb2listwdist( arf$neighbours, z, type = "idw", style = "raw", longlat = NULL), I get this error message:
>
> Error in UseMethod("st_coordinates") :
> no applicable method for 'st_coordinates' applied to an object of class "c('double', 'numeric')"
>
> I have found nothing on Google, Stack Exchange, or R-Help to help. So far, manually digging through this newsgroup has turned nothing up. Any help is greatly appreciated.
>
> Regards,
> Jeff Boggs
>
>
> Jeff Boggs | Associate Professor
> Department of Geography & Tourism Studies
> NCO Research Associate & Associate Director | Centre for Canadian Studies
> Brock University
> St. Catharines | Ontario | Canada
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Roger Bivand
Emeritus Professor
Department of Economics, Norwegian School of Economics,
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
e-mail: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
More information about the R-sig-Geo
mailing list