我正在解决与 mgcv 包中的示例类似的问题。
我创建了一个类似的模型,并希望预测到网格级别,而不是训练数据中确定的区域。这可能吗?如果是这样,怎么做?
我创建了一个预测数据集作为示例。
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
谢谢!