Skip to content

Un expected behavior when predicting spatial regression models #47

@MGousseff

Description

@MGousseff

Thank you for the great spatial tools you provide to the community.

Maybe I'm doing something wrong as I'm rather new to spatial regressions, but here is a behavior I find strange.

if I fit a spatial regression model the way it is suggested in the man page, it works and I get some fitted values in the object I get. For instance :
formula1<-paste0( "to_predict ~ predictor_1 + predictor_2") fit.sem1<-lagsarlm(formula1, data=df_b, listw = neighbs_weights_b)

will work. Then my fit.sem object has a $fitted.values and it seems everything works. df_b is an sf object, and neghbs_weights_b have been produced as intended.

But when I try to use this model to predict my dependent variable on another location, using something like :
predict.sem2.arras<-predict(object=fit.sem2, newdata = df_a, listw = neighbs_weights_a, pred.type = "TS", legacy.mixed=TRUE, power=TRUE)

I get an error message :

Error in x$terms (explo_spat_cor_models.R#210): $ operator is invalid for atomic vectors

It is a bit confusing, as I thought my data was wrong, like missing one of the predictors. I checked the way the weights are provided, including how the row.names need to be specified so that they are different in the training area and the testing area.

I found, thanks to the code of an article by Thibault Laurent (thank him for making it available), that the proper use seems to be:

  1. Fit the non-spatial linear model on the "training data".
  2. Feed the resulting linear model to lagsarlm in lieu of just the formula
  3. Use the resulting spatial model to predict on the new area (providing that all the weights are properly computed and fed to the function, o course).

All in all, as it is working, maybe it is just that I'm not aware of practice that may seem obvious to the community, and if so, sorry for the inconvenience, but as an R user, it is rather puzzling that feeding the formula "almost" works and feeding the linear object "fully" works. Maybe just modifying the error message in predict.sarlm would be useful ?

If the issue is considered useful and the behavior can not be reproduced, I'll try and provide a small data set to reproduce it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions