尝试使用mgcv :: gam评估Markov随机字段时发生错误"nb/polys提供的区域名称与数据区域名称之间不匹配". [英] Error when trying to evaluate Markov Random Fields using mgcv::gam "mismatch between nb/polys supplied area names and data area names"

查看:450
本文介绍了尝试使用mgcv :: gam评估Markov随机字段时发生错误"nb/polys提供的区域名称与数据区域名称之间不匹配".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实施此很棒的博客加文·辛普森(Gavin Simpson)发布的文章使用通过cancensus软件包下载的数据,但在尝试评估gam时出现以下错误:

I tried to implement this great blog post by Gavin Simpson using data downloaded using the cancensus package, but I get the following error when trying to evaluate the 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

我已经在此处发布了我的最小工作示例.任何提示将不胜感激.

I have posted my minimal working example here. Any tip would be greatly appreciated.

最好

推荐答案

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

I know you already found your answer, however I had the same error and a different problem, so I'll post my solution here for posterity.

(注意:我使用的是sf软件包,而不是rgdalspdep)

(Note: I used the sf package instead of rgdal and spdep)

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)

您附近的对象结构应如下所示:

Your neighboring object structure should look like:

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)

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

It's all in the factors. FSA in your main data object of your gam must be factor, and your neighboring object structure should be a list of factors with as many levels as the TOTAL number of levels in your main data.

这篇关于尝试使用mgcv :: gam评估Markov随机字段时发生错误"nb/polys提供的区域名称与数据区域名称之间不匹配".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆