Hello Andrew,
Take a look at the following:
predict(model1, addx=T)
predict(model2, addx=T)
predict(model3, addx=T)
As you can see, the factor was turned into dummy variables. However, the
predict.rma() function does not expand a factor passed via newmods into the
corresponding dummy variables. At the moment, you will have to do this yourself.
Note: the intercept is added automatically, so it should not be added to the
newmods vector(s):
For model2:
predict(model2, newmods=c(0,0), addx=T)
predict(model2, newmods=c(1,0), addx=T)
predict(model2, newmods=c(0,1), addx=T)
or use the model.matrix() function:
newdat <- model.matrix(~factor(c(1,2,3)))
predict(model2, newmods=newdat[,-1], addx=T)
For model3:
predict(model3, newmods=cbind(1:5,0,0), addx=T)
predict(model3, newmods=cbind(1:5,1,0), addx=T)
predict(model3, newmods=cbind(1:5,0,1), addx=T)
or using model.matrix():
newdat <- expand.grid(mid=c(1,2,3,4,5), mod=factor(c(1,2,3)))
newdat <- model.matrix(~ mid + mod, data=newdat)
predict(model3, newmods=newdat[,-1], addx=T)
Yes, I realize this takes some extra work and is not as convenient as having
that factor expanded automatically. Something to put on the to-do list ...
Best,
Wolfgang
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org]
> On Behalf Of Andrew Beckerman
> Sent: Thursday, September 08, 2011 14:22
> To: r-help at r-project.org
> Subject: [R] predict.rma (metafor package)
>
> Hi
>
> (R 2.13.1, OSX 10.6.8)
>
> I am trying to use predict.rma with continuous and categorical variables.
> The argument newmods in predict.rma seems to handle coviariates, but
> appears to falter on factors. While I realise that the coefficients for
> factors provide the answers, the goal is to eventually use predict.rma
> with ANCOVA type model with an interaction.
>
> Here is a self contained example (poached in part from the MAd package):
>
> id<-c(1:20)
> n.1<-c(10,20,13,22,28,12,12,36,19,12,36,75,33,121,37,14,40,16,14,20)
> n.2 <- c(11,22,10,20,25,12,12,36,19,11,34,75,33,120,37,14,40,16,10,21)
> g <- c(.68,.56,.23,.64,.49,-.04,1.49,1.33,.58,1.18,-
> .11,1.27,.26,.40,.49,.51,.40,.34,.42,1.16)
> var.g <-
> c(.08,.06,.03,.04,.09,.04,.009,.033,.0058,.018,.011,.027,.026,.0040,.049,.
> 0051,.040,.034,.0042,.016)
> mod<-factor(c(rep(c(1,1,2,3),5))) # factor
> mid<-c(rep(1:5,4)) # covariate
> df<-data.frame(id, n.1,n.2, g, var.g,mod, mid)
>
> # Examples
> # Random Effects
>
> model1<-rma(g,var.g,mods=~mid,method="REML") # covariate model
> model2<-rma(g,var.g,mods=~mod,method="REML") # factor model
> model3<-rma(g,var.g,mods=~mid+mod,method="REML") # multiple
>
> # example matrix for predicting against model3
>
> newdat<-expand.grid(c(1,2,3,4,5),c(1,2,3))
>
> predict(model1,newmods=c(1,2,3,4,5))
> predict(model2,newmods=c(1,2,3))
> predict(model3,newmods=newdat)
>
> --
> Andrew Beckerman
> Sent with Sparrow (http://bit.ly/sigsprw)