tips:multiple_imputation_with_mice_and_metafor
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tips:multiple_imputation_with_mice_and_metafor [2020/12/13 11:48] – Wolfgang Viechtbauer | tips:multiple_imputation_with_mice_and_metafor [2021/10/22 14:45] – Wolfgang Viechtbauer | ||
---|---|---|---|
Line 10: | Line 10: | ||
dat <- dat.bangertdrowns2004 | dat <- dat.bangertdrowns2004 | ||
</ | </ | ||
- | (I copy the dataset into ' | + | (I copy the dataset into ''dat'', which is a bit shorter and therefore easier to type further below). We can look at the first 10 and the last 10 rows of the dataset with: |
<code rsplus> | <code rsplus> | ||
rbind(head(dat, | rbind(head(dat, | ||
Line 137: | Line 137: | ||
install.packages(" | install.packages(" | ||
library(mice) | library(mice) | ||
- | </ | ||
- | |||
- | Also, due to a recent change in the mice package (that currently breaks compatibility with the metafor package), we need to create a little helper function to make things work again. | ||
- | |||
- | <code rsplus> | ||
- | withold <- function (data, expr) { | ||
- | call <- match.call() | ||
- | analyses <- as.list(seq_len(data$m)) | ||
- | for (i in seq_along(analyses)) { | ||
- | data.i <- complete(data, | ||
- | analyses[[i]] <- eval(expr = substitute(expr), | ||
- | if (is.expression(analyses[[i]])) | ||
- | analyses[[i]] <- eval(expr = analyses[[i]], | ||
- | } | ||
- | object <- list(call = call, call1 = data$call, nmis = data$nmis, analyses = analyses) | ||
- | oldClass(object) <- c(" | ||
- | object | ||
- | } | ||
</ | </ | ||
Line 223: | Line 205: | ||
Next, we can fit the model of interest to each of the 20 imputed datasets with: | Next, we can fit the model of interest to each of the 20 imputed datasets with: | ||
<code rsplus> | <code rsplus> | ||
- | fit <- withold(imp, rma(yi, vi, mods = ~ length + wic + feedback + info + pers + imag + meta)) | + | fit <- with(imp, rma(yi, vi, mods = ~ length + wic + feedback + info + pers + imag + meta)) |
</ | </ | ||
tips/multiple_imputation_with_mice_and_metafor.txt · Last modified: 2022/08/03 11:35 by Wolfgang Viechtbauer