How to create a graph file for INLA using region names

72 Views Asked by At

i.e. use the region.id of class nb from the spdep package rather than ignoring it as spdep::nb2INLA does?

I've been trying to link a column in my data containing these regions as a factor, to an INLA model with a graph describing their spatial arrangement.

#something like this
f(rgn16cd,
      model = "bym2",
      graph = inla_graphs$gb_regions)

It works if I coerce rgn16cd from factor to numeric. Is there a way to get the region names into the graph file?

1

There are 1 best solutions below

0
On

Where nbs is a list of class nb, made using an spatial polygons object with row.names given values from a column of the @data slot of the spatial polygons object.

This code should return a graph with named element as shown.

enter image description here

inla_graphs <- purrr::imap(nbs, ~ {
  spdep::nb2INLA(file = glue::glue("{.y}.graph"), nb = .x$nb)
  x <- INLA::inla.read.graph(glue::glue("{.y}.graph"))
  x$nbs <- lapply(x$nbs, FUN = function(X) {
    row.names(.x$mat)[X]
  })
  names(x$nbs) <- row.names(.x$mat)
  unlink(glue::glue("{.y}.graph"))
  x
})