Skip to content
Prev 5405 / 5636 Next

[R-meta] Metafor - regplot() for a categorical (and interaction) variable

This is one issue. However, there are more issues at hand here, since regplot(res, mod = ~ Continent) isn't gong to work either, as this will trigger the error "Can only specify a single variable via argument 'mod'." The reason is that the 'mod' argument is not meant to take a formula as input. However, when you do, then the length of 'mod' will be 2 (length(~ blah)), which triggers the error.

@Emanuel: While I don't know what you really intend to do here, I suspect you would like to show all levels of 'Continent' on the x-axis. This is not so easily accomplished with the regplot() function, which is designed for visualizing quantitative/continuous moderators. For example:

dat <- escalc(measure="RR", ai=tpos, bi=tneg,
                            ci=cpos, di=cneg, data=dat.bcg)

res <- rma(yi, vi, mods = ~ ablat, data=dat)
res

regplot(res, mod="ablat")

A dichotomous moderator is also easily handled:

dat$random <- ifelse(dat$alloc == "random", 1, 0)

res <- rma(yi, vi, mods = ~ random, data=dat)
res

regplot(res, mod="random")

We might want to make this look a bit nicer, maybe like this:

regplot(res, mod="random", xlab="Method of Treatment Allocation", xaxt="n")
axis(side=1, at=c(0,1), labels=c("Non-Random", "Random"))

However, things become more tricky with a factor variable that has 3 or more levels:

res <- rma(yi, vi, mods = ~ alloc, data=dat)
res

regplot(res, mod="alloc")

This will not work, since there are two 'alloc' dummy variables, but regplot() is designed to place a single variable on the x-axis. One could do:

par(mfrow=c(2,1))
regplot(res, mod="allocrandom")
regplot(res, mod="allocsystematic")
par(mfrow=c(1,1))

but this is showing the difference between random and not random (consisting of 'systematic' and 'alternate') in the first plot and the difference between systematic and not systematic (consisting of 'random' and 'alternate') in the second plot. Probably not how most people would want to visualize this. Instead, I suspect most would want to show three 'columns' of points, corresponding to the three levels, with lines connecting the fitted/predicted values for these levels.

I had not quite considered that some may want to do something like this with this function. I am not sure how easy it would be to add this kind of functionality directly to regplot() given some of the internal intricacies. However, with a small trick, we can still accomplish this. A model with a categorical moderator with p levels can be represented as a polynomial regression model to the degree p-1 where the first term is the linear one (which we want to place on the x-axis). This, combined with the possibility to pass predicted values to regplot() via the 'pred' argument, we can place all levels on the x-axis as follows:

dat$anum <- as.numeric(factor(dat$alloc))
res <- rma(yi, vi, mods = ~ poly(anum, degree=2, raw=TRUE), data=dat)
res

pred <- predict(res, newmods=unname(poly(1:3, degree=2, raw=TRUE)))
pred

regplot(res, mod=2, pred=pred, xvals=c(1:3), xlim=c(1,3), xlab="Allocation Method", xaxt="n")
axis(side=1, at=1:3, labels=levels(factor(dat$alloc)))

Best,
Wolfgang