log_mlik_all: Fit models and compute the log marginal likelihood for all...

View source: R/model-within.R

log_mlik_allR Documentation

Fit models and compute the log marginal likelihood for all clusters

Description

Fit the specified INLA model to each cluster and compute the log marginal likelihood for each cluster specified in the membership vector.

Usage

log_mlik_all(
  membership,
  stdata,
  stnames = c("geometry", "time"),
  correction = TRUE,
  detailed = FALSE,
  ...
)

Arguments

membership

Integer, character or factor vector indicating the cluster membership for each spatial unit.

stdata

A stars object with spatial-temporal dimensions defined in stnames, and including predictors and response variables.

stnames

The names of the spatial and temporal dimensions of the stdata object.

correction

Logical value indicating whether a correction for dispersion.

detailed

Logical value indicating whether to return the INLA model instead of the log marginal likelihood. The argument correction is not applied in this case.

...

Arguments passed to the inla function (eg. family, formula and E).

Value

A numeric vector containing the log marginal likelihood for each cluster or the the fitted INLA model for each cluster when detailed = TRUE.

Examples




library(sfclust)
library(stars)

dims <- st_dimensions(
  geometry = st_sfc(lapply(1:5, function(i) st_point(c(i, i)))),
  time = seq(as.Date("2024-01-01"), by = "1 day", length.out = 3)
)
stdata <- st_as_stars(
  cases = array(rpois(15, 100 * exp(1)), dim = c(5, 3)),
  temperature = array(runif(15, 15, 20), dim = c(5, 3)),
  expected = array(100, dim = c(5, 3)),
  dimensions = dims
)

log_mlik_all(c(1, 1, 1, 2, 2), stdata,
  formula = cases ~ temperature, family = "poisson", E = expected)

models = log_mlik_all(c(1, 1, 1, 2, 2), stdata, detailed = TRUE,
  formula = cases ~ temperature, family = "poisson", E = expected)
lapply(models, summary)



sfclust documentation built on June 8, 2025, 10:11 a.m.