Skip to content
Prev 27314 / 29559 Next

Error while using predict.sarlm

On Fri, 24 May 2019, Amitha Puranik wrote:

            
The predict method has to identify the weights applying to the newdata. So 
it uses the region.id attribute of the neighbour object, and the row.names 
of the newdata object. If they do not match, it error-exits. If shp below 
was read in the typical way, the default region.id may be the FID of the 
input file (0, ..., (n-1)), but the default row.names of newdata may be 1, 
..., n.

For example:
Linking to GEOS 3.7.2, GDAL 3.0.0, PROJ 6.1.0
+                                   "shapes/boston_tracts.shp",
+                                   package="spData")[1])
Reading layer `boston_tracts' from data source 
`/home/rsb/lib/r_libs/spData/shapes/boston_tracts.shp' using driver `ESRI 
Shapefile'
Simple feature collection with 506 features and 36 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -71.52311 ymin: 42.00305 xmax: -70.63823 ymax: 
42.67307
epsg (SRID):    4267
proj4string:    +proj=longlat +datum=NAD27 +no_defs
+                 I((NOX*10)^2) + I(RM^2) + AGE + log(DIS) +
+                 log(RAD) + TAX + PTRATIO + I(BB/100) +
+                 log(I(LSTAT/100)))
+           listw=lw_q_489, zero.policy=TRUE,
+           control=list(pre_eig=eigs_489))
+                   pred.type="TS", zero.policy=TRUE))
chr [1:506] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" 
"15" "16" ...
chr [1:17] "13" "14" "15" "17" "43" "50" "312" "313" "314" "317" "337" 
"346" "355" ...
[1] TRUE
# introduce a wrong row.name
[1] FALSE
+                   pred.type="TS", zero.policy=TRUE))
Error in predict.sarlm(SLM_489, newdata = nd, listw = lw_q,
   pred.type = "TS",  :
   mismatch between newdata and spatial weights. newdata should have
   region.id as row.names

In this case, the row.names of the input object to spdep::poly2nb() and 
the region.id matched, as the newdata were subsetted from the same object. 
We don't know the values for your data, but you should be able to check 
them. It is important that they align the data with the weights correctly 
for obvious reasons.

Hope this helps,

Roger