Please also provide the following information in addition to your question:
- Operating System: R 4.0.0
- brms Version: 2.12.0
Dear all,
I am conducting a multilevel meta-analysis in brms. Some of my predictor variables I was unable to obtain in the studies and in requests to authors. So, somehow I have to impute this data. I used the “mice” package according to the brms vignette. However, the model releases the following message: "Error: Argument 'data' must be coercible to a data.frame"
My question is whether there is a way to impute this data in this model of analysis. Below is a reproducible example of my analysis.
Thank you for your attention!
Best
#covariance matrix of outcomes
cor_matrix <- function(x, r, v = rep(1, length(x)), na.rm = FALSE) {
mat <- diag(v)
se <- sqrt(v)
se[is.na(se)] <- 0
if (length(x) > 1L) {
for (i in 2:nrow(mat)) {
for (j in 1:(i-1)) {
if (x[i] == x[j]) {
mat[i, j] <- mat[j, i] <- r * se[i] * se[j]
}
}
}
}
dimnames(mat) <- list(1:nrow(mat), 1:ncol(mat))
if (na.rm) {
keep <- !is.na(diag(mat))
mat <- mat[keep, keep]
}
mat
}
#data
dat <- data.frame(
yi = rnorm(20),
level = factor(c(1, 1, "NA", "NA", "NA", 1, "NA", "NA", 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, "NA", 4)),
log_time = rnorm(20),
exer = rnorm(20),
study_ID = 1:20,
vi = 0.25
)
#data imputation
library(mice)
imp <- mice(dat, m = 5, print = FALSE)
#prior
prior <-c(prior(normal(0, 100), class = Intercept),
prior(normal(0, 10), class = b),
prior(cauchy(0, 10), class = sd))
#matrix
v.m1 <- cor_matrix(dat$study_ID, r = 0.9, v = dat$vi)
cor_obs <- cor_matrix(dat$study_ID, r = 0.9)
#model
m1 <- brm(bf(
yi ~ 1 + log_time + level + exer + (1|study_ID) + fcor(v.m1),
sigma = 1
),
family = gaussian(),
prior = prior, chains = 1,
data2 = list(v.m1 = v.m1),
control = list(adapt_delta = 0.99), data = imp)