Spatial data in shapefile grid: ID problems

105 views
Skip to first unread message

Michael Smith

unread,
May 5, 2021, 4:00:33 AM5/5/21
to R-inla discussion group

Dear all,

I'm following a Coding Club tutorial (https://ourcodingclub.github.io/tutorials/spatial-modelling-inla/) and adapting my own data.

My problems may be coming from the ID column which is a list of 1834 unique IDs, not starting at 1 (i.e. 2, 3, 4, 8 ....2053). The lattice has 1834 squares. My code is:

lattice <- readOGR("grid_with_points.shp")
data <- lattice@data # first variable is 'ID' = chr
Lattice_Temp <- poly2nb(lattice, row.names = data$ID)
nb2INLA("Lattice.graph", Lattice_Temp)
Lattice.adj <- "Lattice.graph"
data$ID <- as.numeric(data$ID)

formula <- NUMPOINTS ~ NGcost_mea + elev_mean + elev_min + elev_max +
  X1_mean + X2_mean + X3_mean + X4_mean + X5_mean + X6_mean + X7_mean +
  X8_mean + X9_mean + X10_mean + X11_mean + X12_mean + X13_mean + X14_mean +   X15_mean + X16_mean + X17_mean + X18_mean + X19_mean + X21_mean + X22_mean +   X23_mean + X24_mean + X25_mean + X26_mean + X27_mean + X28_mean + X29_mean +   X30_mean + X31_mean + X32_mean + X33_mean + X34_mean + X35_mean + X36_mean +   X37_mean + X38_mean + X39_mean + X40_mean + X41_mean + X42_mean + X43_mean +   X44_mean + X45_mean + X46_mean + f(ID, model = "bym", graph = Lattice.adj)

Mod_Lattice <- inla(formula,     
                    family = "poisson",
                    data = data,
                    control.compute = list(cpo = T, dic = T, waic = T))

I get " f(ID). Covariate does not match 'values' 177 times. Indexes for mismatch:929 930 [...etc...] 1834. This is not what you want. Use NA values in the covariate"

Yet if I leave ID as a 'chr' field I get this instead: "In f(ID): 'covariate' must match 'values',  and both must either be 'numeric', or 'factor'/'character'." Scylla and Charybdis....

Looking at the helpfile for poly2nb, it seems a character vector is expected for the region.id. I tried setting it as numeric: no immediate problem, but I still get the 'covariate does not match' error.  I tried not setting row.names at all - this gives me a region.id of 1:1834 but the same error.

Does anyone have any advice? And what's that about NA values?

Finn Lindgren

unread,
May 5, 2021, 7:02:09 AM5/5/21
to R-inla discussion group
In the numerical case, the ID values need to go from 1 to 1834 as well, so something like data$ID <- as.numeric(as.factor(data$ID)) from the chr IDs might accomplish that; this treats the character IDs as labels, without using that they might contain strings of numbers.

Finn

--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/e6c2a5b0-2500-4a22-a84c-94510eb5f28en%40googlegroups.com.


--
Finn Lindgren
email: finn.l...@gmail.com

Michael Smith

unread,
May 6, 2021, 3:20:16 AM5/6/21
to R-inla discussion group
That does sound like it ought to work. I actually went back to QGIS and added another ID field so the numeric ID values were sequential as you say.

Many thanks,
Michael

Michael Smith

unread,
May 6, 2021, 5:57:10 AM5/6/21
to R-inla discussion group
Having said that, using data$ID <- as.numeric(as.factor(data$ID)) I get the error:

Error in if (n.from.graph <= 0) { : missing value where TRUE/FALSE needed
In addition: warning message:
In inla.read.graph.ascii.internal(..., size.only = size.only) :
  NAs introduced by coercion

Michael Smith

unread,
May 6, 2021, 6:49:20 AM5/6/21
to R-inla discussion group
I'm now working on another dataset of 1020 squares. The ID column runs from 1 to 1020 and when imported, is class 'chr'. I have tried using this, also:
data$ID <- as.numeric(data$ID)
data$ID <- as.numeric(as.factor(data$ID))

with the resulting region.id field in the neighbours list, respectively:
- attr(*, "region.id")= chr [1:1020] "1" "2" "3" "4" ...
- attr(*, "region.id")= num [1:1020] 1 2 3 4 5 6 7 8 9 10 ..
- attr(*, "region.id")= num [1:1020] 1 2 3 4 5 6 7 8 9 10 ...

....all producing the same error message as above.

Helpdesk

unread,
May 6, 2021, 6:56:12 AM5/6/21
to Michael Smith, R-inla discussion group

as Finn said, 'ID' have to be integers 1...n, where n is the size of the
graph. 'Lattice.adj' also have to be a valid object from which the graph
can be read from; see the entry in the FAQ in the vignettes.

the error

Error in if (n.from.graph <= 0) { : missing value where TRUE/FALSE
needed
In addition: warning message:
In inla.read.graph.ascii.internal(..., size.only = size.only) :
NAs introduced by coercion

is because of the an error in 'Lattice.adj'.

you may try

inla.debug.graph(Lattice.adj)





On Wed, 2021-05-05 at 01:00 -0700, 'Michael Smith' via R-inla discussion
group wrote:
> X42_mean + X43_mean +   X44_mean + X45_mean + X46_mean + f(ID, model =
> "bym", graph = Lattice.adj)

--
Håvard Rue
he...@r-inla.org

Reply all
Reply to author
Forward
0 new messages