[R-sig-Geo] Error in predict.sarlm (spatial reg package): Not yet able to subset general weights lists

Jiawen Ng |ove|y||tt|ed@|@|e@ @end|ng |rom gm@||@com
Tue Jun 25 16:28:16 CEST 2019


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


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



More information about the R-sig-Geo mailing list