Unknown error with LGCP distance sampling model in inlabru

90 views
Skip to first unread message

Andrew

unread,
Jun 20, 2022, 11:00:04 AM6/20/22
to R-inla discussion group
Hello,

I am trying to follow the following vignette example: https://inlabru-org.github.io/inlabru/articles/web/2d_lgcp_distancesampling.html with my own data but i am encountering the following error message:

Error in rep(k, sum(nonzero)) : invalid 'times' argument

I have no idea how to fix this? I have ensured my data is formatted the same as the "mex.dolphin" data from the vignette, but this hasn't helped? I should also note that the vignette example is running fine on the same computer I am using for this. 

Here is my code for importing my data and running the model:

# Bru settings

bru_options_set(inla.mode = "experimental")

# Importing data and setting/transforming CRS's

site <- st_read(dsn="~/Documents/MSc Conservation and Biodiversity/Research Project/Final project/GIS files/Shapefiles/Borders", layer="BMP_Study_Area_UTM")
site <- as(site, "Spatial")
site <- rebuild_CRS(site)
coordnames(site) <- c("x", "y")

projectCRS <- fm_sp_get_crs(site)

data <- as.data.frame(read_excel("~/Documents/MSc Conservation and Biodiversity/Research Project/Final project/Transect data/BHMP Year 2 – transects/Observations BHMP Year 2/BHMP_S2_All_observations_English.xlsx",
                                 sheet = "Campbell direct observations_S2", col_types = c("skip", "skip", "skip", "text", "skip", "skip", "date", "text", "numeric", "numeric", "skip", "skip", "numeric", "skip", "numeric", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "skip")))

points <- st_as_sf(data, coords = c("longitude", "latitude"))
st_crs(points) <- 4326
points <- st_transform(points, projectCRS)
points <- as(points, "Spatial")
coordnames(points) <- c("x", "y")

samplers <- st_read(dsn="~/Documents/MSc Conservation and Biodiversity/Research Project/Final project/GIS files/Shapefiles/Transects", layer="Transects_Darwin_Y2_UTM")
samplers <- as(samplers, "Spatial")
samplers <- rebuild_CRS(samplers)
coordnames(samplers) <- c("x", "y")

# Mesh 

simpsite <- gSimplify(site, tol = 1600)

bndry <- inla.sp2segment(simpsite, crs = projectCRS)

mesh <- inla.mesh.2d(boundary = bndry,
                     max.edge=c(3300, 6600),
                     min.angle=c(30, 21),
                     max.n=c(48000, 16000),
                     max.n.strict=c(128000, 128000),
                     cutoff=60,
                     offset=c(1400, 7600))
mesh$crs <- projectCRS

# Model

W <- 100

hn <- function(distance, lsig) {
  exp(-0.5 * (distance / exp(lsig))^2)
}

matern <- inla.spde2.pcmatern(mesh,
                              prior.sigma = c(2, 0.01),
                              prior.range = c(50, 0.01) 
) # Priors used from vignette as I am currently unsure how to select appropriate priors here. 

cmp <- ~ mySPDE(main = coordinates, model = matern) +
  lsig(1) + Intercept(1)

form <- coordinates + distance ~ mySPDE +
  log(hn(distance, lsig)) +
  Intercept + log(2)

fit <- lgcp(
  components = cmp,
  points,
  samplers = samplers,
  domain = list(
    coordinates = mesh,
    distance = INLA::inla.mesh.1d(seq(0, 100, length.out = 20))
  ),
  formula = form
)

traceback() # Shown below

13: bru_compute_linearisation.component(model[["effects"]][[label]],
        model = model, lhood_expr = lhood_expr, data = data, state = state,
        A = A[[label]], effects = effects, pred0 = pred0, allow_latent = lhood[["allow_latent"]],
        allow_combine = lhood[["allow_combine"]], eps = eps, ...)
12: bru_compute_linearisation(model[["effects"]][[label]], model = model,
        lhood_expr = lhood_expr, data = data, state = state, A = A[[label]],
        effects = effects, pred0 = pred0, allow_latent = lhood[["allow_latent"]],
        allow_combine = lhood[["allow_combine"]], eps = eps, ...)
11: bru_compute_linearisation.bru_like(x, model = model, data = x[["data"]],
        state = state, A = A[[idx]], eps = eps, ...)
10: bru_compute_linearisation(x, model = model, data = x[["data"]],
        state = state, A = A[[idx]], eps = eps, ...)
9: FUN(X[[i]], ...)
8: lapply(seq_along(lhoods), function(idx) {
       x <- lhoods[[idx]]
       bru_compute_linearisation(x, model = model, data = x[["data"]],
           state = state, A = A[[idx]], eps = eps, ...)
   })
7: bru_compute_linearisation.bru_like_list(lhoods, model = model,
       state = state, A = A, ...)
6: bru_compute_linearisation(lhoods, model = model, state = state,
       A = A, ...)
5: bru_compute_linearisation.bru_model(model, lhoods = lhoods, state = states[[length(states)]],
       A = A)
4: bru_compute_linearisation(model, lhoods = lhoods, state = states[[length(states)]],
       A = A)
3: iinla(model = info[["model"]], lhoods = info[["lhoods"]], options = info[["options"]])
2: bru(components, lik, options = options)
1: lgcp(components = cmp, campbells$points, samplers = campbells$samplers,
       domain = list(coordinates = campbells$mesh, distance = INLA::inla.mesh.1d(seq(0,
           100, length.out = 20))), formula = form)


Apologies for the lengthy post, thank you very much for any help,

Andrew

Finn Lindgren

unread,
Jun 21, 2022, 11:14:23 AM6/21/22
to Andrew, R-inla discussion group
Debugging of the issue showed that the default starting values lead to
-Inf in the linear predictor.
See solution here:
https://github.com/inlabru-org/inlabru/discussions/148#discussioncomment-2995009

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/f6de5d32-eae0-4b0d-a395-7eaf73cb6a9fn%40googlegroups.com.



--
Finn Lindgren
email: finn.l...@gmail.com
Reply all
Reply to author
Forward
0 new messages