问题
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.
Best,
回答1:
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.
(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
And your spline formula:
Effect ~ s(FSA, bs = "mrf", xt = list(nb = neighb), k = 41, fx = TRUE)
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.
回答2:
Found it -- You must make sure that you don't have any polygons with missing Y: shp <- shp[!is.na(shp@data$Y), ]
来源:https://stackoverflow.com/questions/46945652/error-when-trying-to-evaluate-markov-random-fields-using-mgcvgam-mismatch-bet