Hewitt, Charlotte
2013-Mar-19 18:17 UTC
[R] Error when adding lines to a plot using the mixed-effect model and metafor package
Hi, I am a student using R for my final year project, with the metafor package being particularly helpful. I have been following the steps laid out in the manual 'Conducting Meta-analysis in R with the Metafor package' (Viechtbauer, 2010) and applying it to my own data of infected Anopheles mosquitoes across Africa. In particular, I am attempting to apply a mixed-effect model to my data with absolute latitude as a moderator as per the example in the manual. I am able to generate a plot of Relative Risk against absolute latitude without issue, but when I try to add lines in from the predicted values generated I continually get an error: Error in xy.coords(x, y) : 'x' and 'y' lengths differ However, I have used the length function on both my x and y co-ordinates and found no difference in lengths. If anyone could shed some light on this I'd really appreciate it. The code I used can be found below:> mixed<-read.delim("regressiontest.txt") > mixedstudy year ablat mosq1pos mosq1neg mosq2pos mosq2neg 1 Cano et al. 2006 2 42 237 148 629 2 Kerah - Hinzoumbe et al. 2009 10 103 7260 3 215 3 Konate et al. 1994 13 83 5714 48 3609 4 Mwanzia et al. 2011 4 2 8971 0 100> dat<-escalc(measure="RR", ai = mosq1pos, bi = mosq1neg, ci = mosq2pos, di = mosq2neg, data = mixed, append = TRUE) > res<-rma(yi, vi, mods = ablat, data=dat) > predict(res,transf = exp, addx = TRUE) > preds<-predict(res, transf = exp) > wi<-1/sqrt(dat$vi) > size<-0.5 + 3 *(wi-min(wi))/(max(wi)) - min(wi) > plot(dat$ablat, exp(dat$yi), pch = 19, cex = size, xlab = "Absolute Latitude", ylab = "Relative Risk", las=1, bty = "l", log = "y") > lines(0:15, preds$pred)Many thanks, Charlotte Hewitt [[alternative HTML version deleted]]
Viechtbauer Wolfgang (STAT)
2013-Mar-20 09:02 UTC
[R] Error when adding lines to a plot using the mixed-effect model and metafor package
See comments below. Best, Wolfgang -- Wolfgang Viechtbauer, Ph.D., Statistician Department of Psychiatry and Psychology School for Mental Health and Neuroscience Faculty of Health, Medicine, and Life Sciences Maastricht University, P.O. Box 616 (VIJV1) 6200 MD Maastricht, The Netherlands +31 (43) 388-4170 | http://www.wvbauer.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Hewitt, Charlotte > Sent: Tuesday, March 19, 2013 19:17 > To: r-help at R-project.org > Subject: [R] Error when adding lines to a plot using the mixed-effect > model and metafor package > > Hi, > > I am a student using R for my final year project, with the metafor package > being particularly helpful. I have been following the steps laid out in > the manual 'Conducting Meta-analysis in R with the Metafor package' > (Viechtbauer, 2010) and applying it to my own data of infected Anopheles > mosquitoes across Africa. > > In particular, I am attempting to apply a mixed-effect model to my data > with absolute latitude as a moderator as per the example in the manual. I > am able to generate a plot of Relative Risk against absolute latitude > without issue, but when I try to add lines in from the predicted values > generated I continually get an error: > > Error in xy.coords(x, y) : 'x' and 'y' lengths differ > > However, I have used the length function on both my x and y co-ordinates > and found no difference in lengths. If anyone could shed some light on > this I'd really appreciate it. The code I used can be found below: > > > mixed<-read.delim("regressiontest.txt") > > mixed > study year ablat mosq1pos mosq1neg mosq2pos mosq2neg > 1 Cano et al. 2006 2 42 237 148 629 > 2 Kerah - Hinzoumbe et al. 2009 10 103 7260 3 215 > 3 Konate et al. 1994 13 83 5714 48 3609 > 4 Mwanzia et al. 2011 4 2 8971 0 100So there are 4 studies in your dataset.> > dat<-escalc(measure="RR", ai = mosq1pos, bi = mosq1neg, ci = mosq2pos, > di = mosq2neg, data = mixed, append = TRUE) > > res<-rma(yi, vi, mods = ablat, data=dat) > > predict(res,transf = exp, addx = TRUE) > > preds<-predict(res, transf = exp)This will save the predicted values *for those 4 studies* in "preds".> > wi<-1/sqrt(dat$vi) > > size<-0.5 + 3 *(wi-min(wi))/(max(wi)) - min(wi) > > plot(dat$ablat, exp(dat$yi), pch = 19, cex = size, xlab = "Absolute > Latitude", ylab = "Relative Risk", las=1, bty = "l", log = "y") > > lines(0:15, preds$pred)So you are trying to plot a line based on 16 x-values (0:15) and the 4 y-values in preds. That can't work. If you want to obtain the predicted values for ablat values equal to 0:15, then you need to do: preds <- predict(res, newmods=0:15, transf=exp) And then the code should work.> Many thanks, > Charlotte Hewitt