problem fitting rw2 covariates: logical subscript too long

9 views
Skip to first unread message

Virginia Morera Pujol

unread,
Sep 5, 2025, 9:43:31 AM (4 days ago) Sep 5
to R-inla discussion group
Hi, 

I've encountered a problem trying to fit covariates as random walks that I can't really interpret. I've managed to reproduce it with the gorilla's dataset. 

rm(list = ls())
library(inlabru)
library(spatstat)
library(terra)
library(INLA)

# get gorillas data
data(gorillas_sf, package = "inlabru")

mesh <- gorillas_sf$mesh
boundary <- gorillas_sf$boundary
nests <- gorillas_sf$nests

# get covariates
data("gorillas")

elevation <- rast(gorillas.extra$elevation)
crs(elevation) <- "epsg:32632"
elevation <- project(elevation, crs(nests))

# set up 1 covariate for RW2
mesh1D_elev <- inla.mesh.1d(seq(min(elevation[], na.rm = T)-1,
                                max(elevation[], na.rm = T)+1,
                                length.out = 20),
                            degree = 2)
mapper_elev <- bru_mapper(mesh1D_elev, indexed = FALSE)

# set up spatial random field
matern <- INLA::inla.spde2.pcmatern(
  mesh,
  prior.sigma = c(0.1, 0.01),
  prior.range = c(0.1, 0.01))

# fit model
cmp <- geometry ~ Intercept(1) +
  Eff.elevation(elevation, model = "rw2", mapper = mapper_elev) +
  field(geometry, model = matern)
 
fit <- lgcp(cmp, nests,
            samplers = boundary,
            domain = list(geometry = mesh))

This produces the following error that doesn't appear when fitting the covariate as linear

Warning in input_eval.bru_input(component[[part]]$input, data, env = component$env,  :
  Model input 'elevation' for 'Eff.elevation' returned some NA values.
Attempting to fill in spatially by nearest available value.
To avoid this basic covariate imputation, supply complete data.
Error in .subscript.2ary(x, , j, drop = drop) :
  logical subscript too long

I'm aware of the warning for the covariate imputation, that doesn't bother me at this point, my issue is with the cryptic error. Any ideas?

Many thanks! 

Finn Lindgren

unread,
Sep 5, 2025, 10:26:30 AM (4 days ago) Sep 5
to Virginia Morera Pujol, R-inla discussion group
You seem to be using the original covariate transformed to a CRS that may or may not be the same as the one used by the data stored in inlabru.
For compatible covariates for that data, see the help text for gorillas_sf() that already has a properly transformed raster for the covariates, that also includes infill of missing values (required near the domain boundary).
gorillas_sf_gcov() I think is the function that returns the needed rasters.
Finn

On 5 Sep 2025, at 14:43, Virginia Morera Pujol <morera....@gmail.com> wrote:


--
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, visit https://groups.google.com/d/msgid/r-inla-discussion-group/e3d346a0-14ba-4610-bc3a-dcdcb29aed3an%40googlegroups.com.
Message has been deleted

Virginia Morera Pujol

unread,
Sep 6, 2025, 8:59:50 AM (3 days ago) Sep 6
to R-inla discussion group
Oh, I didn't realise the covariates were available from there. In any case, that was not the problem, but whatever it was got solved by updating inlabru from 2.12 to 2.13. 

Apologies for the unnecessary question. 

El dia divendres, 5 de setembre del 2025 a les 15:26:30 UTC+1, Finn Lindgren va escriure:
Reply all
Reply to author
Forward
0 new messages