r-spatial / spatialreg

spatialreg: spatial models estimation and testing
https://r-spatial.github.io/spatialreg/
41 stars 12 forks source link

The new Durbin= argument possible issue #10

Closed rsbivand closed 4 years ago

rsbivand commented 4 years ago

See https://github.com/r-spatial/spdep/issues/37

rsbivand commented 4 years ago

Without a fully reproducible example preferably using a built-in data set, nobody can offer any advice.

rsbivand commented 4 years ago

A possibility is that handling of na.action is untested and that a value in the selected covariate is missing (so fx has more rows than X), but this should be covered by passing na.action. Try to manipulate say columbus to give the same error, and post the reprex.

rsbivand commented 4 years ago

My reprex:

data(oldcol, package="spdep")
listw <- spdep::nb2listw(COL.nb, style="W")
ev <- eigenw(listw)
COL.mixed.D1a <- lagsarlm(CRIME ~ INC + HOVAL, data=COL.OLD, listw, Durbin= ~ INC, control=list(pre_eig=ev))
summary(COL.mixed.D1a)
anyNA(COL.OLD$INC)
COL.OLD1 <- COL.OLD
is.na(COL.OLD1$INC) <- 10L
anyNA(COL.OLD1$INC)
COL.mixed.D1a1 <- lagsarlm(CRIME ~ INC + HOVAL, data=COL.OLD1, listw, Durbin= ~ INC, control=list(pre_eig=ev))
# Error in eigen_pre_setup(env, pre_eig = pre_eig, which = which) : 
#  length(pre_eig) == get("n", envir = env) is not TRUE

If this is your error message, it is nothing to do with Durbin=, but with control=list(pre_eig=ev), as the pre-computed eigenvalues should not be used if on-the-fly NA removal has occurred (opening https://github.com/r-spatial/spatialreg/issues/11):

COL.mixed.D1a1 <- lagsarlm(CRIME ~ INC + HOVAL, data=COL.OLD1, listw, Durbin= ~ INC)
summary(COL.mixed.D1a1)

So a reprex showing the code really in play is essential, please.

omm718 commented 4 years ago

Thanks for the info. Will try and see where it goes.

rsbivand commented 4 years ago

Please at least post the complete command causing the error, the error message, and the output of traceback() entered immediately after the error. Otherwise this cannot be resolved, and the lack of resolution will be your lack of attention to software you use freely and someone else maintains. Do try to help, not "will try to see", do see, or don't expect any resolution.

omm718 commented 4 years ago

I'm sorry I wasn't clear before. I'm using a non-built-in dataset. I meant I'll try to manipulate a built-in dataset and produce the same error. I did and couldn't. Based on the info you provided, it has to be something with my data (and not the code) that I have not been able to figure out yet. I think you can close this issue then.

SEM <- errorsarlm(log(G2016)-log(G2006) ~ log(G2006) + H2006, data=shape, listw=contiguity_q, na.action = na.exclude, Durbin = ~ H2006)

Error in create_WX(fx, listw, zero.policy = zero.policy, prefix = prefix) : Input data and neighbourhood list have different dimensions

traceback() 3: stop("Input data and neighbourhood list have different dimensions") 2: create_WX(fx, listw, zero.policy = zero.policy, prefix = prefix) 1: errorsarlm(log(G2016) - log(G2006) ~ log(G2006) + H2006, data = shape, listw = contiguity_q, na.action = na.exclude, Durbin = ~H2006)

Trying to debug my code, I tried the following and this raised the aforementioned questions for me. NROW(shape) [1] 88

length(contiguity_q$neighbours) [1] 88

Thanks for your contribution. It's truly appreciated.

rsbivand commented 4 years ago

If need be zip your input objects, or save() to RDA format, and either zip the data and enough script to reproduce the problem and upload here, provide a link here, or send the link or zipfile to me by email if this is too public.

rsbivand commented 4 years ago

The NA was in the included variables not in the Durbin formula:

library(spatialreg)
data(oldcol, package="spdep")
listw <- spdep::nb2listw(COL.nb, style="W")
COL.OLD1 <- COL.OLD
is.na(COL.OLD1$HOVAL) <- 10L
res <- lagsarlm(CRIME ~ INC + HOVAL, data=COL.OLD1, listw, Durbin= ~ INC)
# Error in create_WX(fx, listw, zero.policy = zero.policy, prefix = prefix) : 
#  Input data and neighbourhood list have different dimensions