gpiras / sphet

sphet was previously hosted by Rforge. This R library deals with cross sectional spatial econometrics models estimated by IV and GM.
8 stars 2 forks source link

zero.policy= in listw2dgCMatrix #17

Open rsbivand opened 1 year ago

rsbivand commented 1 year ago

@gpiras listw2dgCMatrix in sphet and splm seems to do the same as as(, "CsparseMatrix") in spatialreg. The apparent difference is that listw2dgCMatrix takes a zero.policy argument, but:

library(spdep)
columbus <- st_read(system.file("shapes/columbus.shp", package="spData")[1], quiet=TRUE)
(nb0 <- poly2nb(columbus))
lw0 <- nb2listw(nb0)
library(spatialreg)
m0 <- as(lw0, "CsparseMatrix")
library(sphet)
M0 <- listw2dgCMatrix(lw0)
all.equal(mo, M0, check.attributes=FALSE)
col_geoms <- st_geometry(columbus)
col_geoms[1] <- st_buffer(col_geoms[1], dist=-0.05)
st_geometry(columbus) <- col_geoms
(nb1 <- poly2nb(columbus))
lw1 <- nb2listw(nb1, zero.policy=TRUE)
m1 <- as(lw1, "CsparseMatrix")
M1b <- listw2dgCMatrix(lw1, zero.policy=TRUE)
all.equal(m1, M1b, check.attributes=FALSE)

M1a <- listw2dgCMatrix(lw1, zero.policy=FALSE) fails with:

  invalid class "dgCMatrix" object: 'i' slot has elements not in {0,...,Dim[1]-1}

The only other difference is in listw2dgCMatrix not providing row or column names.

I'm thinking of adding an attribute recording whether a listw object was created with zero.policy TRUE or FALSE, but in this case I don't see what the zero.policy argument does in listw2dgCMatrix over and above the coercion - which implicitly assumes zero.policy is TRUE because the alternative is full rows and columns of NAs for observations without neighbours. This is part of trying to tidy up interfaces between the SE packages.

gpiras commented 1 year ago

Hi Roger: sorry for the delay in my response. From what I recall, when I wrote listw2dgCMatrix() I was having issues with Na's. In fact, the help of listw2dgCMatrix() make reference to lagsarlm() in spatialreg pakage. From the help in lagsarlm():

zero.policy | default NULL, use global option value; if TRUE assign zero to the lagged value of zones without neighbours, if FALSE (default) assign NA - causing lagsarlm() to terminate with an error

I think I was applying the same logic in listw2dgCMatrix(). Does this make sense? One easy fix would be to to check if there are Na's in listw. If there aren't any then coerce zero.policy to TRUE, and eliminate the argument zero.policy from the function. What do you think?

rsbivand commented 1 year ago

I also posted to R-sig-geo: https://stat.ethz.ch/pipermail/r-sig-geo/2023-November/029353.html and subsequent, and https://stat.ethz.ch/pipermail/r-sig-geo/2023-November/029356.html. I'm working through spdep now to try to detect and structure policy, so Josiah @JosiahParry and Connor's @ConnorDonegan comments are helpful. The changes upstream in spdep add an attribute to listw objects storing the zero.policy status when it was created, with further branches when converting matrices to listw, and when subsetting listw objects (this affects predict methods for out-of-sample predictions). @JosiahParry @ConnorDonegan - should I start a discussion on https://github.com/r-spatial/discuss and invite any further responders from the mailing list posting?

gpiras commented 1 year ago

Yes, I would probably try to involve more people from r-spatial in the discussion.

gpiras commented 1 year ago

Also, should I wait before making any changes to listw2dgCMatrix()?

rsbivand commented 1 year ago

Yes, please wait until upstream stabilises.

JosiahParry commented 1 year ago

Thanks for including me! Quick heads up: there's a typo in the reprex: The line should compare m0 to M0 rather than mo. all.equal(m0, M0, check.attributes=FALSE)