Fixed-effects model selection
Description
Performs a two-stage fixed effect model selection:
- Best configuration: Fits, ranks and selects univariable fixed effect models by covariate (rolling averages, lags, non-linear) according to goodness of fit metrics.
- Best combination: Generates, fits, and ranks multivariable combinations of the selected univariable fixed 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.
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_fe(
data,
outcome,
covariates,
time,
unit = NULL,
re_mod,
re_id = NULL,
roll_mean = NULL,
roll_sum = NULL,
lag = NULL,
nl = NULL,
family = "nbinomial",
offset_var = NULL,
metric = c("waic", "lms", "re_meanchange"),
n_select_uni = 1,
n_select_multi = 3,
standardize = "all",
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.covariates: Names of the covariates in the data to consider.time: Character specifying the name of the column ofdatathat 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_mod: Either the output ofselect_re(i.e. a list with a re_multi_gof component), or alternatively a single character with a random effects formula.re_id: Only used ifre_modis the output ofselect_re. ID of the selected random effects models to use as inselect_re$re_multi_gof$model_id. If NULL (default), it will use the best model according to the GoF statistics.roll_mean: A named list specifying the variables incovariatesto compute rolling means for with averaging timest>= 1. Note that t=1 is equivalent to the non-transformed covariate denoted as ‘noroll’. See example to see format. If a variable incovariatesis included in neitherroll_meannorroll_sum(default), only the non-transformed covariate (i.e. ‘noroll’) is included.roll_sum: Same asroll_meanbut with a sum. Defaults to NULL. The rolling windows requested inroll_meanandroll_sumare combined.lag: A named list specifying the variables incovariatesto compute lags with lag timest>= 0. See example to see format. Lag times are applied after rolling statistics. Note that t=0 (i.e. ‘lag0’) is equivalent to the non-transformed covariate. If a variable incovariatesis not included in the list (default), only the non-transformed covariate (i.e. ‘lag0’) is included.nl: A named list specifying the variables incovariatesto test in non-linear form using n >= 3 cut points. See example to see format. Non-linear effects are applied after rolling statistics and lags. If a variable incovariatesis not included in the list, only the linear effect will be computed. Set to NULL (default) to only include linear effects.family: Character string specifying the likelihood family (default is"nbinomial"). See fit_models to check the available families.offset_var: A character 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: Character vector specifying the ranking metric used to rank models. Options include ‘waic’, ‘lms’, ‘re_meanchange’ (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 covariate group.n_select_multi: Integer. Number of best-fitting multivariable models to display as final candidates. Note that only combinations with at least length(covariates) - 1) are considered.standardize: Character. Specifies which variables should be standardised, which can help model convergence. Standardization is applied after rolling statistics and before lagging, so that the mean of all non-lagged variables is equal to 0 and standard deviation is 1 (these may vary slightly for lagged versions due to dropped rows). Set to NULL for no standardization, set to ‘all’ (default) to apply to all covariates or use specific covariate names.nthreads: An integer specifying the maximum number of threads for parallel computation. Default is1.
Returns
A list of class GHRselect_fe containing:
params: Selection of input parameters used in the reporting of the results.data: Data used for all the model runs after transformations.fe_uni_mod: Univariable fixed effect models.fe_uni_gof: GoF results for univariable fixed effect models.fe_uni_selected: Selected univariable fixed effect models.fe_multi_gof: GoF results for multivariable fixed effect models.fe_multi_mod: Multivariable fixed effect models.fe_multi_selected: Selected multivariable fixed effects.best: Best-fitting multivariable model formula.
See Also
select_report select_re
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"))
# Run FE model selection
dengue_fe <- select_fe(
data = dengue_MS,
outcome = "dengue_cases",
covariates = c("tmax", "pdsi"),
roll_mean = list(tmax = c(1, 6), pdsi = c(1, 6)),
lag = list(tmax = c(1, 3), pdsi = c(1, 3)),
nl = list(tmax = c(4, 8), pdsi = 8),
offset_var = "pop100000",
time = "date",
unit = "micro_name",
re_mod = dengue_re
)
# Generate report
select_report(dengue_fe, file.path(tempdir(), "fixed_effects.html"))
## End(Not run)