Skip to contents

Performs a computation of each of imputed datasets in data.

Usage

# S3 method for class 'mids'
with(data, expr, ...)

Arguments

data

An object of type mids, which stands for 'multiply imputed data set', typically created by a call to function mice().

expr

An expression to evaluate for each imputed data set. A formula with a dot (notation for "all other variables"), such as glm(hyp ~ ., family = binomial), and a formula stored in a variable, such as lm(f), work if they are arguments of the top-level call, the call has no data argument, and the called function has one. See Details.

...

Not used

Value

An object of S3 class mira

Details

with() evaluates expr with the columns of each completed data set as variables, so a model like lm(bmi ~ age) finds its variables without a data argument. This fails in two cases. A dot in a formula can only be expanded from a data argument. And a formula stored in a variable, such as f <- bmi ~ age in lm(f), looks up its variables in the environment where it was created, not in the completed data. If the top-level call in expr has such a formula as an argument, no data argument, and calls a function with a data argument, with() adds data = .mice.data, where .mice.data is the completed data set. A variable counts as a stored formula if it is not a column of the data and evaluates to a formula outside with(). The fitted call then shows data = .mice.data. That object only exists during with(), so update() of such a fit outside with() needs a new data argument.

A formula in a nested call, such as summary(lm(bmi ~ .)) or summary(lm(f)), is not detected. Write data = mget(names(nhanes)) (with the name of your data) to pass the completed data explicitly in such cases.

Note

Version 3.11.10 changed to tidy evaluation on a quosure. This change should not affect any code that worked on previous versions. It turned out that the latter statement was not true (#292). Version 3.12.2 reverts to the old with() function.

References

van Buuren S and Groothuis-Oudshoorn K (2011). mice: Multivariate Imputation by Chained Equations in R. Journal of Statistical Software, 45(3), 1-67. doi:10.18637/jss.v045.i03

See also

Author

Karin Oudshoorn, Stef van Buuren 2009, 2012, 2020

Examples

imp <- mice(nhanes2, m = 2, print = FALSE, seed = 14221)

# descriptive statistics
getfit(with(imp, table(hyp, age)))
#> Component 1 :
#>      age
#> hyp   20-39 40-59 60-99
#>   no     12     4     3
#>   yes     0     3     3
#> 
#> Component 2 :
#>      age
#> hyp   20-39 40-59 60-99
#>   no     11     4     3
#>   yes     1     3     3
#> 

# model fitting and testing
fit1 <- with(imp, lm(bmi ~ age + hyp + chl))
fit2 <- with(imp, glm(hyp ~ age + chl, family = binomial))
fit3 <- with(imp, anova(lm(bmi ~ age + chl)))

# a dot formula: all other variables as predictors
fit4 <- with(imp, lm(bmi ~ .))

# a formula stored in a variable
f <- bmi ~ age + chl
fit5 <- with(imp, lm(f))