Error in spatialreg::predict.sarlm: unknown mismatch. please report this bug

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Error in spatialreg::predict.sarlm: unknown mismatch. please report this bug

amoroso
Dear All,

I am trying to do a out-of-sample prediction using predict.sarlm but I am
getting the 'unknown mismatch' error from predict.sarlm.

I have a training and a testing spatial points dataframe (train_spdf and
test_spdf) and I know that I would require a listw object that contains the
neighbours of the training set for fitting the model and a listw object
that contains the neighbours of training + testing set for predicting.

Here is how I have created the listw object for predicting:
all_coords <- rbind(train_spdf@coords, test_spdf@coords)
col.rel.nb <- graph2nb(relativeneigh(all_coords), sym=TRUE)
test.listw <- nb2listw(col.rel.nb)

It seems like there are at least 2 ways one can create the listw object for
model fitting.

First Way:
nb <- graph2nb(relativeneigh(train_spdf@coords), sym=TRUE)
train.listw <- nb2listw(nb)

Second Way:
train_index <- rep(1:214) # the first 214 observations should be my
training set since that was how I rbind-ed it in all_coords object
train.listw <- subset(test.listw, 1:nrow(all_coords) %in% train_index)

Eventually, the train.listw and test.listw object will be used as follows:

model <- spatialreg::lagsarlm(myformula, data=train_spdf@data,
                        train.listw, type="mixed",zero.policy = T)

pred <- spatialreg::predict.sarlm(model, test_spdf@data,
test.listw,zero.policy = T)

When I did it the first way, I obtained an error and warning messages from
the predict.sarlm function:
Error in spatialreg::predict.sarlm(model, final_test_spdf@data, test.listw,
 :
  unknown mismatch. please report this bug
In addition: Warning messages:
1: In spatialreg::predict.sarlm(model, final_test_spdf@data, test.listw,  :
  some region.id are both in data and newdata
2: In colnames(Xs.not.lagged) != colnames(Xo) :
  longer object length is not a multiple of shorter object length

When I did it the second way,  I obtained warning messages from the
lagsarlm function:
Warning message:
In spatialreg::lagsarlm(as.formula(rest_formula), data =
final_train_spdf@data,  :
  Aliased variables found:
A lag.B lag.C lag.D lag. E lag.F lag.G lag.H lag.I lag.J lag.K lag.L lag.M
lag.N lag.O lag.P  [... truncated]

And then the same error and warning messages as the first way when using
the predict.sarlm function:
Error in spatialreg::predict.sarlm(model2, final_test_spdf@data,
test.listw,  :
  unknown mismatch. please report this bug
In addition: Warning messages:
1: In spatialreg::predict.sarlm(model2, final_test_spdf@data, test.listw,  :
  some region.id are both in data and newdata
2: In colnames(Xs.not.lagged) != colnames(Xo) :
  longer object length is not a multiple of shorter object length

As you can see, either method would have me run into the 'unknown mismatch'
error in predict.sarlm.

Here I have 2 questions:
1. Which way should be the right way of creating the listw object for
fitting the model?
2. How can I solve the 'unknown mismatch' error?

Would appreciate any advice! Thank you!

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo