Out-of-Sample predictions with spdep::sarlm

I am trying to make an out of-sample-prediction for spatial data using the spdep package. The documentation claims that this should be possible. However, I do not understand how it works. In particular I do not know how the spatial weights should be handled.

In the example below I generated data which I split into a training and a test set. How can I fit the model using the training data and predict the outcome of the test data?

``````library(spdep)
set.seed(1)
coords    <- data.frame(x = runif(n=20, min =0, max = 10),
y = runif(n=20, min = 0, max = 10))
sp_data   <- SpatialPointsDataFrame(coords, data = data.frame(a = rnorm(20), b = rnorm(20) ) )
weights   <- nb2listw(knn2nb(knearneigh(sp_data, k = 2),  row.names = 1:20 ))

#Using all data to fit the model
sp_model  <- lagsarlm(a ~ b, data = sp_data, listw = weights)
in_sample <- predict(sp_model)

#Using train set to fit the model
train_set <- sp_data[1:15, ]

#Out-of-sample prediction
test_set <- sp_data[16:20, ]
``````

SAC models are not suitable for prediction problems. You can do it, but you're going to be violating all kind of statistical assumptions. In this case, you need to generate a weigths matrix (lw) for the training set and another matrix for the test set. Note that you are training the model with one structure given by the dimmension of that matrix, and testing it in another dimmension. Both, variance and bias are huge.

• Agreed, the vairance-bias trade off is uncontrollable due to the unknown structural characteristics of the withheld data. Assuming that data outside the fit model shares the same spatial structure is an ecological fallacy. In fact, one could make an argument that a data withhold is not warranted because it plausible changes the spatial structure of the data used in the model. In this case one should use a Monte Carlo or Bootstrap/permutation approach to evaluate model structure, variance and parameter stability and not prediction performance. – Jeffrey Evans Feb 1 at 17:17