### copy data into 'dat' and examine data
dat <- dat.knapp2017
dat[-c(1:2)]
if (FALSE) {
### load metafor package
library(metafor)
### fit a standard random-effects model ignoring the issues described above
res <- rma(yi, vi, data=dat)
res
### fit a multilevel model with random effects for studies and comparisons within studies
### (but this ignored the correlation in the sampling errors)
res <- rma.mv(yi, vi, random = ~ 1 | study/comp, data=dat)
res
### create variable that indicates the task and difficulty combination as increasing integers
dat$task.diff <- unlist(lapply(split(dat, dat$study), function(x) {
task.int <- as.integer(factor(x$task))
diff.int <- as.integer(factor(x$difficulty))
diff.int[is.na(diff.int)] <- 1
paste0(task.int, ".", diff.int)}))
### construct correlation matrix for two tasks with four different difficulties where the
### correlation is 0.4 for different difficulties of the same task, 0.7 for the same
### difficulty of different tasks, and 0.28 for different difficulties of different tasks
R <- matrix(0.4, nrow=8, ncol=8)
R[5:8,1:4] <- R[1:4,5:8] <- 0.28
diag(R[1:4,5:8]) <- 0.7
diag(R[5:8,1:4]) <- 0.7
diag(R) <- 1
rownames(R) <- colnames(R) <- paste0(rep(1:2, each=4), ".", 1:4)
R
### construct an approximate V matrix accounting for the use of shared groups and
### for correlations among tasks/difficulties as specified in the R matrix above
V <- vcalc(vi, cluster=study, grp1=group1, grp2=group2, w1=n_sz, w2=n_hc,
obs=task.diff, rho=R, data=dat)
### correlation matrix for study 3 with four patient groups and a single control group
round(cov2cor(V[dat$study == 3, dat$study == 3]), 2)
### correlation matrix for study 6 with two tasks with four difficulties
cov2cor(V[dat$study == 6, dat$study == 6])
### correlation matrix for study 24 with two independent groups
cov2cor(V[dat$study == 24, dat$study == 24])
### correlation matrix for study 29 with three independent groups
cov2cor(V[dat$study == 29, dat$study == 29])
### fit multilevel model as above, but now use this V matrix in the model
res <- rma.mv(yi, V, random = ~ 1 | study/comp, data=dat)
res
predict(res, digits=2)
### use cluster-robust inference methods based on this model
robust(res, cluster=study)
### use methods from the clubSandwich package
robust(res, cluster=study, clubSandwich=TRUE)
### examine if task difficulty is a potential moderator of the effect
res <- rma.mv(yi, V, mods = ~ difficulty, random = ~ 1 | study/comp, data=dat)
res
sav <- robust(res, cluster=study)
sav
sav <- robust(res, cluster=study, clubSandwich=TRUE)
sav
### draw bubble plot
regplot(sav, xlab="Task Difficulty", ylab="Standardized Mean Difference", las=1, digits=1, bty="l")
}
Run the code above in your browser using DataLab