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,
allow_combine = NULL,
aggregate = NULL,
aggregate_input = NULL,
control.family = NULL,
control.gcpo = NULL,
tag = NULL,
options = list(),
.envir = parent.frame(),
include = deprecated(),
exclude = deprecated(),
include_latent = 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. In addition to the likelihoods provided by inla (seenames(INLA::inla.models()$likelihood)) inlabru supports fitting latent Gaussian Cox processes viafamily = "cp". As an alternative tobru(), thelgcp()function provides a convenient interface to fitting Cox processes.- 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.
- 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.- allow_combine
logical; If
TRUE, the predictor expression may involve several rows of the input data to influence the same row. WhenNULL, defaults toFALSE, unlessresponse_datais non-NULL, ordatais alist, or the likelihood construction requires it.- 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
NULLor an optional input list to the mapper defined by non-NULLaggregate, overriding the default,list(block = .data.[[".block"]], weights = .data.[["weight"]], n_block = bru_response_size(.response_data.), block_response = ".block"), available from version
2.12.0.9013.From
2.13.0.9016, it will look for ablock_responseelement in the list, which should be the name of a response variable inresponse_datato match theblockinformation against, allowing character or factor aggregation block information to be used by replacingblockwithmatch(block, block_response). If not supplied, the name".block"is 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 noblock_responsevariable 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)) {
# 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")
multiplot(p1, p2, pj)
}
# }