Learn R Programming

brms (version 2.19.0)

mi: Predictors with Missing Values in brms Models

Description

Specify predictor term with missing values in brms. The function does not evaluate its arguments -- it exists purely to help set up a model. For documentation on how to specify missing values in response variables, see resp_mi.

Usage

mi(x, idx = NA)

Arguments

x

The variable containing missing values.

idx

An optional variable containing indices of observations in `x` that are to be used in the model. This is mostly relevant in partially subsetted models (via resp_subset) but may also have other applications that I haven't thought of.

Details

For detailed documentation see help(brmsformula).

See Also

brmsformula

Examples

Run this code
if (FALSE) {
data("nhanes", package = "mice")
N <- nrow(nhanes)

# simple model with missing data
bform1 <- bf(bmi | mi() ~ age * mi(chl)) +
  bf(chl | mi() ~ age) +
  set_rescor(FALSE)

fit1 <- brm(bform1, data = nhanes)

summary(fit1)
plot(conditional_effects(fit1, resp = "bmi"), ask = FALSE)
loo(fit1, newdata = na.omit(fit1$data))

# simulate some measurement noise
nhanes$se <- rexp(N, 2)

# measurement noise can be handled within 'mi' terms
# with or without the presence of missing values
bform2 <- bf(bmi | mi() ~ age * mi(chl)) +
  bf(chl | mi(se) ~ age) +
  set_rescor(FALSE)

fit2 <- brm(bform2, data = nhanes)

summary(fit2)
plot(conditional_effects(fit2, resp = "bmi"), ask = FALSE)

# 'mi' terms can also be used when some responses are subsetted
nhanes$sub <- TRUE
nhanes$sub[1:2] <- FALSE
nhanes$id <- 1:N
nhanes$idx <- sample(3:N, N, TRUE)

# this requires the addition term 'index' being specified
# in the subsetted part of the model
bform3 <- bf(bmi | mi() ~ age * mi(chl, idx)) +
  bf(chl | mi(se) + subset(sub) + index(id) ~ age) +
  set_rescor(FALSE)

fit3 <- brm(bform3, data = nhanes)

summary(fit3)
plot(conditional_effects(fit3, resp = "bmi"), ask = FALSE)
}

Run the code above in your browser using DataLab