[R-sig-Geo] Seeking help implementing nb2listwdist() to find inverse distance matrix.

Jeffrey Boggs jbogg@ @end|ng |rom brocku@c@
Wed May 19 23:33:01 CEST 2021


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")

## calculate Moran's I assuming Normality.
moran.test(z, lwW2, randomisation = FALSE, alternative = "two.sided")

# 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)

## 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]]



More information about the R-sig-Geo mailing list