我尝试使用通过cancensus软件包下载的数据来实现Gavin Simpson的great blog帖子,但是尝试评估gam时出现以下错误:

Error in smooth.construct.mrf.smooth.spec(object, dk$data, dk$knots) :
  mismatch between nb/polys supplied area names and data area names
In addition: Warning message:
In if (all.equal(sort(a.name), sort(levels(k))) != TRUE) stop("mismatch
between nb/polys supplied area names and data area names") :
  the condition has length > 1 and only the first element will be used

我已经发布了我的最小工作示例here。任何提示将不胜感激。

最好,

最佳答案

我知道您已经找到了答案,但是我有相同的错误和不同的问题,因此我将在后文中发布我的解决方案。

(注意:我使用了sf包而不是rgdalspdep)

library(sf)
sh_terr <- st_read("your_shp.shp", stringsAsFactors = T)

neighb <- st_touches(sh_terr, sparse = T) %>%
  lapply(function(xx) sh_terr$FSA[xx] %>% factor(levels = levels(sh_terr$FSA))) %>%
  set_names(sh_terr$FSA)

您的相邻对象结构应如下所示:
str(neighb[1:5])
List of 5
 $ G0A: Factor w/ 419 levels "G0A","G0C","G0E",..: 14 15 16 17 21 22 39 49 50 51 ...
 $ G0C: Factor w/ 419 levels "G0A","G0C","G0E",..: 3 6 67
 $ G0E: Factor w/ 419 levels "G0A","G0C","G0E",..: 2 6 65 67
 $ G0G: Factor w/ 419 levels "G0A","G0C","G0E",..: 5 16 62 70 271
 $ G0H: Factor w/ 419 levels "G0A","G0C","G0E",..: 4 14 16 68 70 71

和你的样条公式:
Effect ~ s(FSA, bs = "mrf", xt = list(nb = neighb), k = 41, fx = TRUE)

都是因素。 FSA的主数据对象中的gam必须为factor,并且相邻对象的结构应该是一个因素列表,其级别与主数据中的TOTAL总数一样多。

关于r - 尝试使用mgcv::gam “mismatch between nb/polys supplied area names and data area names”评估Markov随机字段时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46945652/

10-14 16:00