Observation model construction for usage with bru().
Note: Prior to version 2.12.0, this function was called like(), and that
alias will remain for a while until examples etc have been updated and users
made aware of the change. The name change is to avoid issues with namespace
clashes, e.g. with data.table::like(), and also to signal that the function
defines observation models, not just likelihood functions.
Usage
bru_obs(
formula = . ~ .,
family = "gaussian",
data = NULL,
response_data = NULL,
data_extra = NULL,
E = NULL,
Ntrials = NULL,
weights = NULL,
scale = NULL,
domain = NULL,
samplers = NULL,
ips = NULL,
used = NULL,
is_rowwise = NULL,
aggregate = NULL,
aggregate_input = NULL,
response_block = NULL,
control.family = NULL,
control.gcpo = NULL,
tag = NULL,
options = list(),
.envir = parent.frame(),
include = deprecated(),
exclude = deprecated(),
include_latent = deprecated(),
allow_combine = deprecated()
)
like(
...,
E = NULL,
Ntrials = NULL,
weights = NULL,
scale = NULL,
options = list(),
.envir = parent.frame()
)
bru_obs_list(..., .tag = NULL)
# S3 method for class 'list'
bru_obs_list(object, ..., .tag = NULL)
# S3 method for class 'bru_obs'
bru_obs_list(..., .tag = NULL)
# S3 method for class 'bru_obs_list'
bru_obs_list(..., .tag = NULL)
# S3 method for class 'bru_obs'
c(...)
# S3 method for class 'bru_obs_list'
c(...)
# S3 method for class 'bru_obs_list'
x[i]
like_list(...)
bru_like_list(...)Arguments
- formula
a
formulawhere the right hand side is a general R expression defines the predictor used in the model.- family
A string identifying a valid
INLA::inlalikelihood family. The default isgaussianwith identity link. Apart from the likelihoods provided by INLA (seenames(INLA::inla.models()$likelihood)) inlabru supportscpCox process likelihood, for fitting point process models. The
lgcp()function is a shortcut tobru(..., family = "cp").nzbinomial,nzbetabinomial,nznbinomial,nzcenpoissonNon-zero versions of "binomial", "betabinomial", "nbinomial", and "cenpoisson", respectively, implemented by setting the zero probability parameter close to zero in the corresponding "zeroinflated*0" models. It will check if these models have native INLA implementations, and use those instead if available. In INLA
26.06.07, only "nzpoisson" has a native implementation.
- data
Predictor expression-specific data, as a
data.frame,tibble, orsf. Since2.12.0.9023, deprecated support forSpatialPoints[DataFrame]objects.- response_data
Observation/response-specific data for models that need different size/format for inputs and response variables, as a
data.frame,tibble, orsf. Since2.12.0.9023, deprecated support forSpatialPoints[DataFrame]objects.- data_extra
object convertible with
as.list()with additional variables to be made available in predictor evaluations. Variables with the same names as the data object will be ignored, unless accessed via.data_extra.[["name"]]or.data_extra.$namein the formula.- E
Exposure/effort parameter for family = 'poisson' passed on to
INLA::inla. Special case if family is 'cp': rescale all integration weights by a scalarE. For sampler specific reweighting/effort, use aweightcolumn in thesamplersobject instead, seefmesher::fm_int(). Default taken fromoptions$E, normally1.- Ntrials
A vector containing the number of trials for the 'binomial' likelihood. Default taken from
options$Ntrials, normally1.- weights
Fixed (optional) weights parameters of the likelihood, so the log-likelihood
[i]is changed intoweights[i] * log_likelihood[i]. Default value is1. WARNING: The normalizing constant for the likelihood is NOT recomputed, so ALL marginals (and the marginal likelihood) must be interpreted with great care.For
family = "cp", the weights are applied assum(weights * eta)in the point location contribution part of the log-likelihood, whereetais the linear predictor, and do not affect the integration part of the likelihood. This can be used to implement approximative methods for point location uncertainty.- scale
Fixed (optional) scale parameters of the precision for several models, such as Gaussian and student-t response models.
- domain, samplers, ips
Arguments used for
family="cp"andaggregate=.domainNamed list of domain definitions, see
fmesher::fm_int().samplersIntegration domain for
family="cp"or subdomains foraggregate=, seefmesher::fm_int().ipsIntegration points. Defaults to fmesher::fm_int
(domain, samplers). If explicitly given, overridesdomainandsamplers.fmesher::new_fm_int()(from fmesher0.5.0.9013) can be used for manually constructed integration schemes.
- used
Either
NULL(default) or abru_used()object. When,NULL, the information about what effects and latent vectors are made available to the predictor evaluation is defined bybru_used(formula), which will include all effects and latent vectors used by the predictor expression.- is_rowwise, allow_combine
logical; If
is_rowwiseisFALSE, the predictor expression may involve several rows of the input data to influence the same row. WhenNULL, it defaults toTRUE, unlessresponse_datais non-NULL, ordatais alist, or the likelihood construction requires it. Theallow_combineargument is a deprecated and inverted version, such thatis_rowwise = !allow_combine.- aggregate
character ("none" or a valid name for the
typeargument ofbm_aggregate()) or an aggregationbru_mapperobject (bm_aggregate(),bm_logsumexp(), orbm_logitaverage()). DefaultNULL, interpreted as "none"., available from version
2.12.0.9013.- aggregate_input, response_block
NULLor an optional input list to the mapper defined by non-NULLaggregate, overriding the default,aggregate_input = list( block = .data.[[".block"]], weights = .data.[["weight"]], n_block = bru_response_size(.response_data.) ), response_block = .response_data.[[".block"]], available from version
2.12.0.9013.From
2.13.0.9016to2.14.1.9007, it would look for ablock_responsecharacter element in the list, but from2.14.1.9008, the separate argumentreponse_blockshould be used instead, with an expression to be evaluated in the input data context.response_blockshould evaluate to a vector of the same length as the response data, with values that can be used to index into the rows of the response variable to determine which rows of the predictor expression to aggregate, allowing character or factor aggregation block information to be used by replacingblockwithmatch(block, response_block). If not supplied, the variable.blockin theresponse_datais tried. If that isn't available, theblockinformation must be supplied directly as a numeric or integer vector, indexing into the rows of the response variable. Having noresponse_blockvariable is equivalent to havingresponse_data$.block = seq_len(bru_response_size(response_data)).- control.family
A optional
listofINLA::control.familyoptions- control.gcpo
A optional
listofINLA::control.gcpooptions- tag
character; Name that can be used to identify the relevant parts of INLA predictor vector output, via
bru_index().- options
A bru_options options object or a list of options passed on to
bru_options()- .envir
The evaluation environment to use for special arguments (
E,Ntrials,weights, andscale) if not found inresponse_dataordata. Defaults to the calling environment.- include, exclude, include_latent
- ...
For
bru_obs_list.bru_obs, one or morebru_obsobjects- .tag
Optional name to assign to a single
bru_obsobject. Reserved for internal use.- object
A list of
bru_obsand/orbru_obs_listobjects- x
bru_obs_listobject from which to extract element(s)- i
indices specifying elements to extract
Value
A likelihood configuration which can be used to parameterise bru().
Details
The E, Ntrials, weights, and scale arguments are evaluated in the
data context, with values from response_data taking precedence over data.
Methods (by generic)
bru_obs_list(bru_obs): Combine one or more lists ofbru_obsobservation model objects into abru_obs_listobjectc(bru_obs): Combine severalbru_obsobjects into abru_obs_listobject
Functions
like():Legacy
like()method forinlabruprior to version2.12.0. Usebru_obs()instead.bru_obs_list(): Combinebru_obsobservation model object into abru_obs_listobjectbru_obs_list(list): Combine one or more lists ofbru_obsobservation model objects into abru_obs_listobjectbru_obs_list(bru_obs_list): Combine one or morebru_obs_listobjects into abru_obs_listobjectc(bru_obs_list): Combine severalbru_obs_listobjects into abru_obs_listobjectlike_list():Backwards compatibility for versions
<= 2.12.0. For later versions, useas_bru_obs_list(),bru_obs_list(), orc().bru_like_list():Backwards compatibility for versions
<= 2.12.0.9017. For later versions, useas_bru_obs_list(),bru_obs_list()orc().
Examples
# \donttest{
if (bru_safe_inla() &&
require(ggplot2, quietly = TRUE) &&
require(patchwork, quietly = TRUE)) {
# The 'bru_obs()' (previously 'like()') function's main purpose is to set
# up observation models, both for single- and multi-likelihood models.
# 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 bru_obs
# function
lik1 <- bru_obs(
"gaussian",
formula = y ~ x + Intercept,
data = df1,
tag = "norm"
)
lik2 <- bru_obs(
"poisson",
formula = y ~ x + z + Intercept,
data = df2,
tag = "pois"
)
# The union of effects of both models gives the components needed to run
# bru
jcmp <- ~ x + z + Intercept(1)
jfit <- bru(jcmp, lik1, lik2)
bru_index(jfit, "norm")
bru_index(jfit, "pois")
# 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")
(p1 / p2 / pj)
# Non-zero binomial example:
nzdata <- data.frame(ntrials = rep(2:6, 10))
nzdata$x <- rnorm(nrow(nzdata))
nzdata$count <- rbinom(
nrow(nzdata),
size = nzdata$ntrials,
prob = plogis(nzdata$x)
)
nzdata <- nzdata[nzdata$count > 0, ]
truncated_binomial_obs <-
bru_obs(
formula = count ~ x,
family = "nzbinomial",
data = nzdata,
Ntrials = ntrials
)
fit <- bru(~ 0 + x, truncated_binomial_obs)
}
# }