[R-sig-Geo] Error in predict.sarlm (spatial reg package): Not yet able to subset general weights lists
Roger Bivand
Roger@B|v@nd @end|ng |rom nhh@no
Tue Jun 25 17:31:30 CEST 2019
On Tue, 25 Jun 2019, Jiawen Ng wrote:
> Dear Roger,
>
> I have an inverse distance matrix that has been converted to listw object
> via mat2listw function.
>
> To predict out-of-sample data, I made 2 listw objects -- one that is based
> upon the training data -- this is used as the listw input for training the
> model (i called this train_listw) and the other is based on all data (both
> train and test) that is used as the listw input for predicting new data (i
> called this all_listw).
>
> Here is the code:
>
> model <- spatialreg::lagsarlm(rest_formula, data=train, train_listw)
> model_pred <- spatialreg::predict.sarlm(model, test, all_listw)
>
> Here is the function that I used to create the inverse distance matrix and
> the listw object:
>
> get_inv_dist_nb <- function(full_df){
> dist_matrix <- as.matrix(dist(cbind(full_df$Longitude,full_df$Latitude)))
> dist_matrix_inv <- 1/dist_matrix
> diag(dist_matrix_inv) <- 0
> mylistw <- mat2listw(dist_matrix_inv, style="W")
> return (mylistw)
> }
>
>
>
> The error i receive is:
>
> Error in subset.listw(listw, (attr(listw, "region.id") %in% region.id), :
> Not yet able to subset general weights lists
>
That seems self-explanatory. You and anyone else interested are very
welcome to contribute. There may be ways of flagging general weights as
subsettable under a given weights scheme, but some are not subsettable,
hence the absence of support.
It might also be possible to re-write all the internals as sparse matrices
(IDW are often in practice dense, which is a poor weighting scheme for
an SAR/CAR/MRF approach anyway). But that needs coding.
Ideas welcome. Until then, don't use IDW or other general weights if you
want to use prediction methods.
Roger
>
> From a previous post that you have answered, you mentioned that this is
> because some observations are not complete. May I just clarify what do you
> mean by observations here? the listw object? I have checked both my
> all_listw and train_listw objects -- they do not have missing neighbours.
> The function above that I wrote to calculate the inverse distance matrix
> does not allow for missing values too. There is also no missing values upon
> checking my training and testing datasets.
>
>
> Please advise, thank you so much!
>
> [[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
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; 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