[R-meta] Plotting confidence intervals around regression slope using rma.mv

Bronwen Stanford bstanfor at ucsc.edu
Mon Mar 26 20:54:11 CEST 2018


Thank you Wolfgang. That worked. I think my main problem was feeding
predict a dataframe instead of a matrix.

Bronwen Stanford
Ph.D. Candidate
Environmental Studies Department
University of California, Santa Cruz

On Sat, Mar 24, 2018 at 8:22 AM, Viechtbauer Wolfgang (SP) <
wolfgang.viechtbauer at maastrichtuniversity.nl> wrote:

> The 'newmods' argument should be a matrix. So use this:
>
> sav <- predict(m1, newmods = as.matrix(df1))
>
> (I'll consider making predict.rma() also work with data frames)
>
> 'd1f$preds<-' won't work either, since predict.rma() returns not just a
> vector, but a list with elements of various length (unclass(sav) will show
> you the contents). So just put things in another object and then you can
> use it.
>
> Also, predict.rma() does not have an 'interval' argument, but it always
> returns the CI anyway.
>
> Finally, I know this is just a toy example, but the struct="DIAG" has no
> relevance when random=(~1|ID). The struct argument only matters when using
> things like 'random = ~ inner | outer'.
>
> So, to create the plot:
>
> plot(df$x, df$y, pch=19)
> lines(df1$x, sav$pred)
> lines(df1$x, sav$ci.lb, lty="dotted")
> lines(df1$x, sav$ci.ub, lty="dotted")
>
> For a more complete example, see:
>
> http://www.metafor-project.org/doku.php/plots:meta_analytic_scatterplot
>
> Best,
> Wolfgang
>
> -----Original Message-----
> From: R-sig-meta-analysis [mailto:r-sig-meta-analysis-
> bounces at r-project.org] On Behalf Of Bronwen Stanford
> Sent: Monday, 12 March, 2018 20:03
> To: r-sig-meta-analysis at r-project.org
> Subject: [R-meta] Plotting confidence intervals around regression slope
> using rma.mv
>
> I am trying to plot the regression line estimated using metafor and 95%
> confidence intervals on the regression line, and I'm not sure how to do
> this. Here is an example of what I'm hoping to create using lm from base R:
> https://stat.ethz.ch/pipermail/r-help/2007-November/146285.html
>
> The model has random effects, but for the purposes of this plot, I'm
> interested in just plotting the slope of one predictor and the CI for that
> predictor, ignoring the random effects.
>
> Do I need to use a model matrix (along these lines
> http://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#lme4) ? And if so,
> does anyone have any example syntax that I could follow? I tried adapting
> the linked example, and ran into a lot of problems.
>
> Here is a dummy example (that isn't working). I'm interested in plotting
> values of y against x while holding "a" constant:
>
> #create example data
> df<-data.frame(expand.grid(
>   a=seq(0,1, length=2), #covariate
>   x=seq(0,1, length=5),  #predictor I want to plot
>   y=seq(0,5,  length=5), #response
>   vi=seq(.1,.5, length=2),  #variance
>   ID=c("A","B")   #random effect
> ))
>
> #run the model
> m1<-rma.mv(y, V=vi,
>            mods= ~ a + x,
>            random=(~1|ID), struct="DIAG",
>            data=df)
>
> #create new df to use for predictions, only allowing x to vary
> df1<-data.frame(expand.grid(
>   a=mean(df$a),
>   x=seq(0,1, length=5)
> ))
>
> d1f$preds<-predict(m1, newmods=df1, c=c("confidence"))
>
> I get the message "Error in Xi.new %*% x$beta : requires numeric/complex
> matrix/vector arguments".
>

	[[alternative HTML version deleted]]



More information about the R-sig-meta-analysis mailing list