enw_formula: Define a model using a formula interface

enw_formulaR Documentation

Define a model using a formula interface

Description

This function allows models to be defined using a flexible formula interface that supports fixed effects, random effects (using lme4 syntax). Note that the returned fixed effects design matrix is sparse and so the index supplied is required to link observations to the appropriate design matrix row.

Usage

enw_formula(formula, data, sparse = TRUE)

Arguments

formula

A model formula that may use standard fixed effects, random effects using lme4 syntax (see re()), and random walks defined using the rw() helper function.

data

A data.frame of observations. It must include all variables used in the supplied formula.

sparse

Logical, defaults to TRUE. Should the fixed effects design matrix be sparely defined.

Value

A list containing the following:

  • formula: The user supplied formula

  • parsed_formula: The formula as parsed by parse_formula()

  • extended_formula: The flattened version of the formula with both user supplied terms and terms added for the user supplied complex model components.

  • fixed: A list containing the fixed effect formula, sparse design matrix, and the index linking the design matrix with observations.

  • random: A list containing the random effect formula, sparse design matrix, and the index linking the design matrix with random effects.

See Also

Functions used to help convert formulas into model designs as_string_formula(), construct_re(), construct_rw(), enw_manual_formula(), parse_formula(), re(), remove_rw_terms(), rw(), rw_terms(), split_formula_to_terms()

Examples

# Use meta data for references dates from the Germany COVID-19
# hospitalisation data.
obs <- enw_filter_report_dates(
  germany_covid19_hosp[location == "DE"],
  remove_days = 40
)
obs <- enw_filter_reference_dates(obs, include_days = 40)
pobs <- enw_preprocess_data(
  obs, by = c("age_group", "location"), max_delay = 20
  )
data <- pobs$metareference[[1]]

# Model with fixed effects for age group
enw_formula(~ 1 + age_group, data)

# Model with random effects for age group
enw_formula(~ 1 + (1 | age_group), data)

# Model with a random effect for age group and a random walk
enw_formula(~ 1 + (1 | age_group) + rw(week), data)

# Model defined without a sparse fixed effects design matrix
enw_formula(~1, data[1:20, ])

# Model using an interaction in the right hand side of a random effect
# to specify an independent random effect per strata.
enw_formula(~ (1 + day | week:month), data = data)

seabbs/epinowcast documentation built on July 27, 2024, 1:33 p.m.