Random-effects model selection
Description
Performs a two-stage random effect model selection:
- Best configuration: Fits, ranks and select univariable random effect models by effect type (spatial, seasonal, interannual) according to goodness of fit metrics.
- Best combination: Generates, fits, and ranks multivariable combinations of the selected univariable random effects.
The function returns all intermediate and final results in a structured output list, including generated formulas, goodness-of-fit (GoF) metrics, ranked models, and selected model combinations. See details for requirements.
WARNING: This function is experimental. Model selection should combine goodness-of-fit and cross-validation metrics, which will be included in future versions of the functions.
select_re(
data,
outcome,
prior,
time,
unit = NULL,
re_spatial = "bym2",
re_seasonal = c("rw1", "rw2"),
re_interannual = c("iid", "rw1", "rw2"),
family = "nbinomial",
offset_var = NULL,
metric = c("waic", "lms"),
n_select_uni = 1,
n_select_multi = 3,
nthreads = 1
)Arguments
data: A data.frame containing the outcome and covariates used for model fitting. Should contain columns named “spatial_id”, “seasonal_id” and “year_id” if spatial, seasonal, or interannual effects are considered.outcome: Character specifying the outcome variable name.prior: Character vector specifying the prior specifications to try for each random effect. A prior with the same name must be present in the working environment (see example).time: Character specifying the name of the column ofdataof class ‘Date’ that contains the dates of the observations. Regular daily, weekly, or monthly time series are allowed.unit: Character specifying the name of the column ofdatathat contains the spatial unit identifiers for cases where several time series are available. Set to NULL for a single time series analysis.re_spatial: Character specifying the spatial random effect models to include. Options include “bym2” (default) and NULL (no spatial RE). If not NULL, an objectgwith the graph of the spatial units must be available in the working environment and the spatial identifierspatial_idmust be present in the data (type integer). Ifunitis NULL, it will be forced to be NULL too (no spatial component).re_seasonal: Character vector specifying the seasonal random effect models to include. Options include “rw1”, “rw2” and NULL (no seasonal RE). If not NULL, the seasonal identifierseasonal_idmust be present in the data (type integer).re_interannual: Character vector specifying the interannual random effect models to include. Options include “iid”, “rw1” and “rw2” and NULL (no interannual RE). If not NULL, the interannual identifieryear_idmust be present in the data (type integer).family: Character string specifying the likelihood family (default is"nbinomial").offset_var: A character string specifying the name of the offset variable indata. IfNULL(default), no offset is applied. Internally,offset_varis transformed according to the link function of the corresponding family: identity for"gaussian"and log for the rest of families.metric: Optional character string specifying the ranking metric used in.rank_gof(). Options include ‘waic’, ‘lms’ (the two defaults), ‘dic’, ‘rmse’ and ‘mae’. See fit_models for more details on the metrics.n_select_uni: Integer. Number of best-fitting univariable models to select per random effect group.n_select_multi: Integer. Number of best-fitting multivariable models to display as final candidates in the report.nthreads: An integer specifying the maximum number of threads for parallel computation. Default is1.
Returns
A named list of class GHRselect_re containing:
params: Selection of input parameters used in the reporting of the results.data: Data used for all the model runs.re_uni_mod: Univariable random effect models.re_uni_gof: GoF results for univariable random effect models.re_uni_selected: Selected univariable random effects.re_multi_mod: Multivariable random effect models.re_multi_gof: GoF results for multivariable random effect models.re_multi_selected: Selected multivariable random effects.best: Best-fitting multivariable model formula.
See Also
select_report select_fe
Examples
## Not run:
# Read data
library(sf)
data(dengue_MS)
dengue_MS <- dengue_MS[complete.cases(dengue_MS), ]
# Compute graph
data(map_MS)
nb <- spdep::poly2nb(map_MS)
g <- spdep::nb2mat(nb, style = "B")
# Prepare indices
dengue_MS$spatial_id <- as.numeric(as.factor(dengue_MS$micro_code))
dengue_MS$seasonal_id <- dengue_MS$month
dengue_MS$year_id <- dengue_MS$year
# Prepare precision priors
prec1 <- list(prec = list(prior = "pc.prec", param = c(0.5, 0.01)))
prec2 <- list(prec = list(prior = "pc.prec", param = c(0.25, 0.01)))
# Run RE model selection
dengue_re <- select_re(
data = dengue_MS,
outcome = "dengue_cases",
prior = c("prec1", "prec2"),
offset_var = "pop100000",
time = "date",
unit = "micro_name"
)
# Generate report
select_report(dengue_re, file.path(tempdir(), "random_effects.html"))
## End(Not run)