Skip to content

Plotting models with quadratic trends

5 messages · Adam Roebuck, Philippi, Tom, Guillaume Adeux +1 more

#
Hello,

I apologize in advance if this is not the correct venue for this question.
I have been searching for a way to plot models with quadratic interactions
in R for months now. Each time I search, I typically end up falling back on
calculating point estimates in Excel. If anyone has any suggestions, I
would greatly appreciate it.

My model specification is as follows:

mod<-lme(DV~1+linear+quad+cov1+var1+(linear*var1)+(quad*var1),

random=~1+lvl1|lvl2,

data=dat,method="ML",

control=list(opt="optim"),correlation=corAR1())

Both interactions with the time variables are significant, and so I would
like to find a way to plot them in R instead of Excel.

Thanks,
Adam
#
I'll jump in because I think this is an easy question not requiring the experts.

My general approach is to create a skeleton dataset with the combination of values I want to graph the fit over (range of linear, quad, cov1, var 1), then use nlme::predict() on my lme object and that skeleton.  The hard part is then what to actually graph: surfaces of predicted DV as functions of linear & var1 at average or slices of cov1, separate surfaces for different values of lvl1, or something else that shows informative aspects of the fit.  [I'm assuming quad is literally linear^2, perhaps with some centering, so graphing predicted against linear would show the curve due to linear and quad in the model.  For such a model you can be pretty coarse in the grid of values in skeleton, as the surface or slice lines can use smoothing to interpolate in the graphing stage.]

lme4, mgcv, and most other packages I use to fit models have predict functions for their model object types.

Tom


-----Original Message-----
From: R-sig-mixed-models <r-sig-mixed-models-bounces at r-project.org> On Behalf Of Adam Roebuck
Sent: Friday, January 19, 2024 6:36 AM
To: r-sig-mixed-models at r-project.org
Subject: [EXTERNAL] [R-sig-ME] Plotting models with quadratic trends



 This email has been received from outside of DOI - Use caution before clicking on links, opening attachments, or responding.



Hello,

I apologize in advance if this is not the correct venue for this question.
I have been searching for a way to plot models with quadratic interactions in R for months now. Each time I search, I typically end up falling back on calculating point estimates in Excel. If anyone has any suggestions, I would greatly appreciate it.

My model specification is as follows:

mod<-lme(DV~1+linear+quad+cov1+var1+(linear*var1)+(quad*var1),

random=~1+lvl1|lvl2,

data=dat,method="ML",

control=list(opt="optim"),correlation=corAR1())

Both interactions with the time variables are significant, and so I would like to find a way to plot them in R instead of Excel.

Thanks,
Adam


_______________________________________________
R-sig-mixed-models at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
#
Hi,
An alternative could be to code your linear and quadratic effect as
poly(Var,2) (this ensures orthogonality) and use ggemmeans() or ggeffects()
(the difference between the two will depend on whether you have non focal
factors or not) . Something along these lines :
require("ggeffects")
require("emmeans")
*plot(ggemmeans(mod,terms="Var[all]"))*
If you want to look at interactions with factors, you can add :
*plot(ggemmeans(mod,terms=c("Var[all]","YourFactor")))*
You can also condition on specific values of other covariates.
This is well detailed/illustrated if you type "difference between ggpredict
and ggeffects" in your browser.
To go further, these linear and quadratic effects can be compared using
emtrends():
*emtrends(mod, ~ YourFactor | degree, "Var", max.degree = 2)*
You can wrap that in a pairs() call for pairwise comparisons.
Have a good weekend,
GA2



<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Le ven. 19 janv. 2024 ? 15:36, Adam Roebuck <adam.a.roebuck at gmail.com> a
?crit :

  
  
2 days later
#
Just want to send along a quick thank you to Guillaume Adeux and Tom
Philippi. Both of your recommendations worked beautifully.

Have a great week,
Adam

On Fri, Jan 19, 2024 at 1:30?PM Guillaume Adeux <guillaumesimon.a2 at gmail.com>
wrote:

  
  
#
Hi Adam,
just a short follow-up, meanwhile you can also calculate contrasts and pairwise comparisons using ggeffects. See following examples / vignettes, starting with this one (the other two vignettes belonging to this series are references/linked in the document).

https://strengejacke.github.io/ggeffects/articles/introduction_comparisons_1.html

Best
Daniel

-----Urspr?ngliche Nachricht-----
Von: R-sig-mixed-models <r-sig-mixed-models-bounces at r-project.org> Im Auftrag von Adam Roebuck
Gesendet: Montag, 22. Januar 2024 18:10
An: Guillaume Adeux <guillaumesimon.a2 at gmail.com>
Cc: r-sig-mixed-models at r-project.org
Betreff: [EXT] Re: [R-sig-ME] Plotting models with quadratic trends

Just want to send along a quick thank you to Guillaume Adeux and Tom Philippi. Both of your recommendations worked beautifully.

Have a great week,
Adam

On Fri, Jan 19, 2024 at 1:30?PM Guillaume Adeux <guillaumesimon.a2 at gmail.com>
wrote:
_______________________________________________
R-sig-mixed-models at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models

--

_____________________________________________________________________

Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Christian Gerloff (Vorsitzender), Joachim Pr?l?, Prof. Dr. Blanche Schwappach-Pignataro, Matthias Waldmann (komm.)
_____________________________________________________________________

SAVE PAPER - THINK BEFORE PRINTING