0

我正在解决与 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

谢谢!

4

1 回答 1

0

至少据我所知,MRF 不可能做到这一点。在您显示的模型中,您只有 MRF 平滑,因此响应的条件分布被建模为每个district在附近地区彼此相似的约束下的估计值。要使用该模型,您需要预测地区,而不是其他一些数据。

我假设在这一行

pred_b <- predict(b, pred_data)

你的意思是

pred_b <- predict(b, pred_grid)

如果您的真正问题是网格和区域级别的数据混合,您可以在网格上进行预测,但您需要将每个网格位置分配给一个区域,以便您可以传入一个列,例如Districtwhich is what输入到 MRF 项中。

于 2019-11-26T19:59:47.520 回答