Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zero.policy= in listw2dgCMatrix #17

Open
rsbivand opened this issue Oct 28, 2023 · 6 comments
Open

zero.policy= in listw2dgCMatrix #17

rsbivand opened this issue Oct 28, 2023 · 6 comments

Comments

@rsbivand
Copy link
Contributor

@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
Copy link
Owner

gpiras commented Nov 7, 2023

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
Copy link
Contributor Author

rsbivand commented Nov 7, 2023

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
Copy link
Owner

gpiras commented Nov 7, 2023

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

@gpiras
Copy link
Owner

gpiras commented Nov 7, 2023

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

@rsbivand
Copy link
Contributor Author

rsbivand commented Nov 7, 2023

Yes, please wait until upstream stabilises.

@JosiahParry
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants