I am working on a similar problem as the example in the mgcv package.
I have created a similar model and would like to predict to grid level instead of the districts identified in the training data. Is this possible? If so, how?
I have created a prediction dataset as an example.
library(mgcv)
data(columb)
data(columb.polys)
b <- gam(crime ~ s(district,bs="mrf",xt=xt),data=columb,method="REML")
plot(b,scheme=1)
#Create prediction dataset
df <- data.frame(district=numeric(0),x=numeric(0),y= numeric(0)) #Create empty df to store x, y and IDs for each polygon
# Extract x and y coordinates from each polygon and assign district ID
for (i in 1:length(columb.polys)) {
district <- i-1
x <- columb.polys[[i]][,1]
y <- columb.polys[[i]][,2]
df <- rbind(df,cbind(district,x,y))
}
sp <- df %>%
group_by(district) %>%
do(poly=dplyr::select(., x, y) %>%Polygon()) %>%
rowwise() %>%
do(polys=Polygons(list(.$poly),.$district)) %>%
{SpatialPolygons(.$polys)}
#Convert sp to sf object
sp_sf <- st_as_sf(sp, proj4string='+proj=utm +zone=48N +ellps=WGS84 +units=m')
sp_sf <- st_set_crs(sp_sf, '+proj=utm +zone=48N +ellps=WGS84 +units=m')
#Make grid
grid <- st_make_grid(sp_sf,
cellsize = 0.05,
what = "centers", crs = '+proj=utm +zone=48N +ellps=WGS84 +units=m')
#intersection of grid and Columbus, Ohio
pred_grid <- st_intersection(grid, st_buffer(sp_sf, dist=0))
pred_b <- predict(b, pred_data)
Error in data[[txt]] : subscript out of bounds
Thank you!