Skip to contents

Fit a basic MMRM model using brms.

Usage

brm_model(
  data,
  formula,
  ...,
  prior = NULL,
  family = brms::brmsfamily(family = "gaussian")
)

Arguments

data

A tidy data frame with one row per patient per discrete time point.

formula

An object of class "brmsformula" from brm_formula() or brms::brmsformula(). Should include the full parameterization of the model, including fixed effects, residual correlation, and heterogeneity in the discrete-time-specific residual variance components.

...

Arguments to brms::brm() other than data, formula, and prior.

prior

Either NULL for default priors or a "brmsprior" object from brms::prior().

family

A brms family object generated by brms::brmsfamily(). Must fit a continuous outcome variable and have the identity link.

Value

A fitted model object from brms.

Parameterization

The formula is not the only factor that determines the fixed effect parameterization. The ordering of the categorical variables in the data, as well as the contrast option in R, affect the construction of the model matrix. To see the model matrix that will ultimately be used in brm_model(), run brms::make_standata() and examine the X element of the returned list. See the examples below for a demonstration.

See also

Other models: brm_formula()

Examples

if (identical(Sys.getenv("BRM_EXAMPLES", unset = ""), "true")) {
set.seed(0L)
data <- brm_data(
  data = brm_simulate_simple()$data,
  outcome = "response",
  role = "response",
  group = "group",
  time = "time",
  patient = "patient",
  reference_group = "group_1",
  reference_time = "time_1"
)
formula <- brm_formula(
  data = data,
  baseline = FALSE,
  baseline_time = FALSE
)
# Optional: set the contrast option, which determines the model matrix.
options(contrasts = c(unordered = "contr.SAS", ordered = "contr.poly"))
# See the fixed effect parameterization you get from the data:
head(brms::make_standata(formula = formula, data = data)$X)
# Specify a different contrast method to use an alternative
# parameterization when fitting the model with brm_model():
options(
  contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")
)
# different model matrix than before:
head(brms::make_standata(formula = formula, data = data)$X)
tmp <- utils::capture.output(
  suppressMessages(
    suppressWarnings(
      model <- brm_model(
        data = data,
        formula = formula,
        chains = 1,
        iter = 100,
        refresh = 0
      )
    )
  )
)
# The output model is a brms model fit object.
model
# The `prior_summary()` function shows the full prior specification
# which reflects the fully realized fixed effects parameterization.
brms::prior_summary(model)
}