gpt4 book ai didi

r - 尝试使用 mgcv::gam "mismatch between nb/polys supplied area names and data area names"评估马尔可夫随机场时出错

转载 作者:行者123 更新时间:2023-12-02 02:59:43 27 4
gpt4 key购买 nike

我试图实现这个 great blog Gavin Simpson 使用从 cancensus 包下载的数据发布,但在尝试评估 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)

一切都在因素中。 gam 的主要数据对象中的 FSA 必须是 factor,并且您的相邻对象结构应该是具有多达主数据中的总级别数。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com