mi: Predictors with Missing Values in 'brms' Models

Description Usage Arguments Details See Also Examples

View source: R/formula-sp.R

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.

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## Not run: 
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)

## End(Not run)

brms documentation built on Aug. 23, 2021, 5:08 p.m.