尝试使用mgcv :: gam评估马尔可夫随机字段时出错" nb /多边形提供的区域名称与数据区域名称不匹配"

时间:2017-10-26 04:01:45

标签: r gam mgcv markov-random-fields

我尝试使用癌症包下载的数据来实现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。任何提示都将非常感激。

最佳,

2 个答案:

答案 0 :(得分:0)

找到它 - 你必须确保你没有丢失Y的任何多边形: shp< - shp [!is.na(shp @ data $ Y),]

答案 1 :(得分:0)

我知道你已经找到了答案,但我有同样的错误和不同的问题,所以我会在这里发布我的解决方案以供后代使用。

(注意:我使用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级别数一样多