The metafor Package

A Meta-Analysis Package for R

User Tools

Site Tools


tips:multiple_imputation_with_mice_and_metafor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
tips:multiple_imputation_with_mice_and_metafor [2020/12/13 11:48] Wolfgang Viechtbauertips:multiple_imputation_with_mice_and_metafor [2022/01/27 20:29] Wolfgang Viechtbauer
Line 1: Line 1:
 ===== Multiple Imputation with the mice and metafor Packages ===== ===== Multiple Imputation with the mice and metafor Packages =====
  
-Meta-analytic data often looks like Swiss cheese -- there are lots of holes in it! For example, due to missing information, it may not be possible to compute the effect size estimates (or the corresponding sampling variances) for some of the studies. Similarly, it may not be possible to code certain moderator variables for some studies. Below, I illustrate how to use multiple imputation as a possible way to deal with the latter issue, using the [[https://cran.r-project.org/package=mice|mice]] package in combination with metafor.+A meta-analytic dataset often looks like Swiss cheese -- there are lots of holes in it! For example, due to missing information, it may not be possible to compute the effect size estimates (or the corresponding sampling variances) for some of the studies. Similarly, it may not be possible to code certain moderator variables for some studies. Below, I illustrate how to use multiple imputation as a possible way to deal with the latter issue, using the [[https://cran.r-project.org/package=mice|mice]] package in combination with metafor.
  
 ==== Data Preparation ==== ==== Data Preparation ====
Line 10: Line 10:
 dat <- dat.bangertdrowns2004 dat <- dat.bangertdrowns2004
 </code> </code>
-(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:+(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, 10), tail(dat, 10)) rbind(head(dat, 10), tail(dat, 10))
Line 137: Line 137:
 install.packages("mice") install.packages("mice")
 library(mice) library(mice)
-</code> 
- 
-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, i) 
-    analyses[[i]] <- eval(expr = substitute(expr), envir = data.i, enclos = parent.frame()) 
-    if (is.expression(analyses[[i]])) 
-      analyses[[i]] <- eval(expr = analyses[[i]], envir = data.i, enclos = parent.frame()) 
-  } 
-  object <- list(call = call, call1 = data$call, nmis = data$nmis, analyses = analyses) 
-  oldClass(object) <- c("mira", "matrix") 
-  object 
-} 
 </code> </code>
  
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))
 </code> </code>
  
tips/multiple_imputation_with_mice_and_metafor.txt · Last modified: 2022/08/03 11:35 by Wolfgang Viechtbauer