inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
76 stars 21 forks source link

Error in INLA::f(xxx, ..., group = group, model = model) : object 'period' not found #73

Closed smar-git closed 3 years ago

smar-git commented 4 years ago

Hi, I made a minimal example where i get the error Error in INLA::f(xxx, ..., group = group, model = model) : object 'period' not found even though 'period' is present in dataframe

ndata = 50
xy = matrix(runif(ndata*3*2),ncol = 2)
df_sp = SpatialPointsDataFrame(coords = xy,
  data  = data.frame(period = rep(1:3, each = ndata)))

loc.bnd = matrix(c(0,0, 1,0, 1,1, 0,1), 4, 2, byrow=TRUE)

boundary = inla.mesh.segment(loc.bnd)
max.edge = 0.05
bound.outer = 0.5
mesh = inla.mesh.2d(boundary = boundary,
                     #loc=coord_sightings,
                     max.edge = c(1,5)*max.edge,
                     # - use 5 times max.edge in the outer extension/offset/boundary
                     offset = c(max.edge, bound.outer))
plot(mesh)
points(xy)

spde_model = inla.spde2.pcmatern(mesh,
                                 prior.sigma = c(1, 0.1),
                                 prior.range = c(0.1, 0.1))
mod0 = coordinates+period ~ Intercept + 
  field(map = coordinates,
        model = spde_model, 
        group = period, 
        ngroup = 3, 
        control.group=list(model = "iid"))

res =  lgcp(mod0,
            df_sp, 
            samplers = boundary,
            options = list(verbose = T)) 
finnlindgren commented 4 years ago

Note: May be related to #45

finnlindgren commented 4 years ago

The issue seems to have been already solved in the development version. There I instead got a different error message:

Error in ipoints(samplers, domain$coordinates, project = TRUE, group = samp.dim) : 
  object 'ips' not found

I traced this error to samplers not supporting inla.mesh.segment objects, partly because they are more similar to SpatialLines than SpatialPolygons (it does support mesh objects). The workaround is to supply a SpatialPolygons object instead. Replacing the boundary definition with

boundary <- SpatialPolygons(list(Polygons(list(Polygon(loc.bnd)),
                                             ID = "boundary")))

solved the problem.

I recommend using the current development version, 2.1.13.999 until the new backend code is ready for use.

Also, inlabru/INLA should either add conversion from inla.mesh.segment to Spatial* objects (the other way is supported) or add a check that the samplers information is of a supported type, so it can give a more informative error message.