with | R Documentation |
with()
runs a model on the n
multiply imputed datasets of the supplied mimids
or wimids
object. The typical sequence of steps to do a matching or weighting procedure on multiply imputed datasets are:
Multiply impute the missing values using the mice()
function (from the mice package) or the amelia()
function (from the Amelia package), resulting in a multiply imputed dataset (an object of the mids
or amelia
class);
Match or weight each multiply imputed dataset using matchthem()
or weightthem()
, resulting in an object of the mimids
or wimids
class;
Check the extent of balance of covariates in the datasets (using functions from the cobalt package);
Fit the statistical model of interest on each dataset by the with()
function, resulting in an object of the mimira
class; and
Pool the estimates from each model into a single set of estimates and standard errors, resulting in an object of the mimipo
class.
## S3 method for class 'mimids'
with(data, expr, cluster, ...)
## S3 method for class 'wimids'
with(data, expr, ...)
data |
A |
expr |
An expression (usually a call to a modeling function like |
cluster |
When a function from survey (e.g., |
... |
Additional arguments to be passed to |
with()
applies the supplied model in expr
to the (matched or weighted) multiply imputed datasets, automatically incorporating the (matching) weights when possible. The argument to expr
should be of the form glm(y ~ z, family = quasibinomial)
, for example, excluding the data or weights argument, which are automatically supplied.
Functions from the survey package, such as svyglm()
, are treated a bit differently. No svydesign
object needs to be supplied because with()
automatically constructs and supplies it with the imputed dataset and estimated weights. When cluster = TRUE
(or with()
detects that pairs should be clustered; see the cluster
argument above), pair membership is supplied to the ids
argument of svydesign()
.
After weighting using weightthem()
, glm_weightit()
should be used as the modeling function to fit generalized lienar models. It correctly produces robust standard errors that account for estimation of the weights, if possible. See WeightIt::glm_weightit()
for details. Otherwise, svyglm()
should be used rather than glm()
in order to correctly compute standard errors. For Cox models, coxph()
will produce approximately correct standard errors when used with weighting but svycoxph()
will produce more accurate standard errors when matching is used.
An object from the mimira
class containing the output of the analyses.
Farhad Pishgar and Noah Greifer
Stef van Buuren and Karin Groothuis-Oudshoorn (2011). mice
: Multivariate Imputation by Chained Equations in R
. Journal of Statistical Software, 45(3): 1-67. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v045.i03")}
matchthem()
weightthem()
mice::with.mids()
#Loading libraries
library(survey)
#Loading the dataset
data(osteoarthritis)
#Multiply imputing the missing values
imputed.datasets <- mice::mice(osteoarthritis, m = 5)
#Matching in the multiply imputed datasets
matched.datasets <- matchthem(OSP ~ AGE + SEX + BMI + RAC + SMK,
imputed.datasets,
approach = 'within',
method = 'nearest')
#Analyzing the matched datasets
models <- with(matched.datasets,
svyglm(KOA ~ OSP, family = binomial),
cluster = TRUE)
#Weghting in the multiply imputed datasets
weighted.datasets <- weightthem(OSP ~ AGE + SEX + BMI + RAC + SMK,
imputed.datasets,
approach = 'within',
method = 'glm')
#Analyzing the matched datasets
models <- with(weighted.datasets,
WeightIt::glm_weightit(KOA ~ OSP,
family = binomial))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.