I would like to plot meta-regression with ggplot (or similar packages) using metafor
.
I found this website ( https://ecologyforacrowdedplanet.wordpress.com/2013/05/30/using-metafor-and-ggplot-togetherpart-2/ ) that explains how to plot reported values and regressoin lines of fitted models on a scatterplot with ggplot2
and metafor
.
However, the codes that author put on the site did not run properly by itself. It seems like I have to calculate prediction someway, but since I am a novice R user, I am not sure how I can do this. I cannot treat results of rma()
in the same way as results of lm()
.
Model2<-rma.uni(yi,vi,mods=~Intensity+Method,method="ML",data=ROM)
summary(Model2)
in.plot<-ggplot(data=ROM,aes(x=Intensity,y=yi,size=1/vi,colour=Method))+geom_point(data=ROM,shape=16)
in.plot2<-in.plot+geom_line(data=ROM,aes(x=Intensity,y=preds,size=1))
in.plot3<-in.plot2+ylab("log response ratio")+xlab("Logging intensity (m-3 ha-1)")
in.plot3+theme(legend.position="none")
Please visit the website to see the produced plot (sorry I am not sure if I can just copy and paste the plot here, so I am just citing the website).
When I tried the code with my data that is similar to the website's example, I got an error saying that there are no “preds”.
It seems like I need to calculate the prediction, but that's actually the thing that I would like to know, ie, how to plot fitted regression lines. I learned that I can use predict()
or confint()
functions, but not being able to figure out how to apply those functions to rma()
's objects.
I would really appreciate it if you could teach me how to reproduce this type of plot, using metafor
and ggplot
. Thank you.
Edited
I thought it would be nice to provide the data and codes that can be used for this questions. Following is my approach (I know this is not a good model, the results wise, but this is the research design I am dealing with).
require(metafor)
require(ggplot2)
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
res <- rma(yi, vi, mods = ~ alloc*ablat, data=dat)
preds <- predict.rma (res)
ggplot(dat, aes(x = ablat, y = yi, size = 1/vi, col = alloc))+
geom_point(data = dat, shape = 16) + geom_line(data = dat, y = preds, size = 1)
I wanna plot the observations and fitted regression lines for each allocation method on a scatter plot.
And this is the error I got:
Error: Aesthetics must be either length 1 or the same as the data (13): y, size
In the documentation for rma.uni
of the package metafor
it says:
"Predicted/fitted values can be obtained with predict.rma and fitted.rma. For best linear unbiased predictions, see blup.rma.uni."
I assume with these functions you can then create a vector preds
and plot the fitted lines.
Edit as response to your edit:
ggplot(dat, aes(x = ablat, y = yi, size = 1/vi, col = alloc))+
geom_point(data = dat, shape = 16) +
geom_line(data = dat,aes(x = ablat,y = preds$pred, size = 1))
Is this what you were looking for?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.