Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

like

Likelihood construction for usage with bru()


Description

Likelihood construction for usage with bru()

Usage

like(
  formula = . ~ .,
  family = "gaussian",
  data = NULL,
  mesh = NULL,
  E = NULL,
  Ntrials = NULL,
  samplers = NULL,
  ips = NULL,
  domain = NULL,
  include = NULL,
  exclude = NULL,
  allow_latent = FALSE,
  allow_combine = FALSE,
  options = list()
)

like_list(...)

## S3 method for class 'list'
like_list(object, envir = NULL, ...)

## S3 method for class 'bru_like'
like_list(..., envir = NULL)

## S3 method for class 'bru_like_list'
x[i]

Arguments

formula

a formula where the right hand side is a general R expression defines the predictor used in the model.

family

A string identifying a valid INLA::inla likelihood family. The default is gaussian with identity link. In addition to the likelihoods provided by inla (see names(INLA::inla.models()$likelihood)) inlabru supports fitting latent Gaussian Cox processes via family = "cp". As an alternative to bru(), the lgcp() function provides a convenient interface to fitting Cox processes.

data

Likelihood-specific data, as a data.frame or SpatialPoints[DataFrame] object.

mesh

An inla.mesh object.

E

Exposure parameter for family = 'poisson' passed on to INLA::inla. Special case if family is 'cp': rescale all integration weights by E. Default taken from options$E.

Ntrials

A vector containing the number of trials for the 'binomial' likelihood. Default value is rep(1, n.data). Default taken from options$Ntrials.

samplers

Integration domain for 'cp' family.

ips

Integration points for 'cp' family. Overrides samplers.

domain

Named list of domain definitions.

include

Character vector of component labels that are needed by the predictor expression; Default: NULL (include all components that are not explicitly excluded)

exclude

Character vector of component labels that are not used by the predictor expression. The exclusion list is applied to the list as determined by the include parameter; Default: NULL (do not remove any components from the inclusion list)

allow_latent

logical. If TRUE, the latent state of each component is directly available to the predictor expression, with a _latent suffix. This also makes evaluator functions with suffix _eval available, taking parameters main, group, and replicate, taking values for where to evaluate the component effect that are different than those defined in the component definition itself.

allow_combine

logical; If TRUE, the predictor expression may involve several rows of the input data to influence the same row. (TODO: review what's needed to allow the result to also be of a different size)

options

A bru_options options object or a list of options passed on to bru_options()

...

For like_list.bru_like, one or more bru_like objects

object

A list of bru_like objects

envir

An optional environment for the new bru_like_list object

x

bru_like_list object from which to extract element(s)

i

indices specifying elements to extract

Details

  • like_list: Combine a bru_like likelihoods into a bru_like_list object

  • like_list.list: Combine a list of bru_like likelihoods into a bru_like_list object

  • like_list.bru_like: Combine several bru_like likelihoods into a bru_like_list object

Value

A likelihood configuration which can be used to parameterize bru().

Author(s)

Fabian E. Bachl bachlfab@gmail.com

Examples

if (bru_safe_inla()) {

  # The like function's main purpose is to set up models with multiple likelihoods.
  # The following example generates some random covariates which are observed through
  # two different random effect models with different likelihoods

  # Generate the data

  set.seed(123)

  n1 <- 200
  n2 <- 10

  x1 <- runif(n1)
  x2 <- runif(n2)
  z2 <- runif(n2)

  y1 <- rnorm(n1, mean = 2 * x1 + 3)
  y2 <- rpois(n2, lambda = exp(2 * x2 + z2 + 3))

  df1 <- data.frame(y = y1, x = x1)
  df2 <- data.frame(y = y2, x = x2, z = z2)

  # Single likelihood models and inference using bru are done via

  cmp1 <- y ~ -1 + Intercept(1) + x
  fit1 <- bru(cmp1, family = "gaussian", data = df1)
  summary(fit1)

  cmp2 <- y ~ -1 + Intercept(1) + x + z
  fit2 <- bru(cmp2, family = "poisson", data = df2)
  summary(fit2)

  # A joint model has two likelihoods, which are set up using the like function

  lik1 <- like("gaussian", formula = y ~ x + Intercept, data = df1)
  lik2 <- like("poisson", formula = y ~ x + z + Intercept, data = df2)

  # The union of effects of both models gives the components needed to run bru

  jcmp <- ~ x + z + Intercept
  jfit <- bru(jcmp, lik1, lik2)

  # Compare the estimates

  p1 <- ggplot() +
    gg(fit1$summary.fixed, bar = TRUE) +
    ylim(0, 4) +
    ggtitle("Model 1")
  p2 <- ggplot() +
    gg(fit2$summary.fixed, bar = TRUE) +
    ylim(0, 4) +
    ggtitle("Model 2")
  pj <- ggplot() +
    gg(jfit$summary.fixed, bar = TRUE) +
    ylim(0, 4) +
    ggtitle("Joint model")

  multiplot(p1, p2, pj)
}

inlabru

Bayesian Latent Gaussian Modelling using INLA and Extensions

v2.3.1
GPL (>= 2)
Authors
Finn Lindgren [aut, cre, cph] (<https://orcid.org/0000-0002-5833-2011>, Finn Lindgren continued development of the main code), Fabian E. Bachl [aut, cph] (Fabian Bachl wrote the main code), David L. Borchers [ctb, dtc, cph] (David Borchers wrote code for Gorilla data import and sampling, multiplot tool), Daniel Simpson [ctb, cph] (Daniel Simpson wrote the basic LGCP sampling method), Lindesay Scott-Howard [ctb, dtc, cph] (Lindesay Scott-Howard provided MRSea data import code), Seaton Andy [ctb] (Andy Seaton provided testing and bugfixes)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.