LMER - Plotting of a quadratic effect interacting with time
Thank you, John. Worked like a charm. Also, good catch on exclusion of quadratic FTE as a random effect. Made no sense. Matt
On Thu, Jan 31, 2019 at 11:56 AM Fox, John <jfox at mcmaster.ca> wrote:
Dear Mathew,
-----Original Message----- From: R-sig-mixed-models [mailto:r-sig-mixed-models-bounces at r- project.org] On Behalf Of Boden, Matthew T. via R-sig-mixed-models Sent: Wednesday, January 30, 2019 4:27 PM To: r-sig-mixed-models at r-project.org Subject: [R-sig-ME] LMER - Plotting of a quadratic effect interacting
with time
Hello, I have a question related to fitting and plotting a longitudinal linear
mixed
model that includes an interaction between a quadratic effect and time.
Data
attached. I fit the following: Q1 <- lmer(Patients ~ Time*FTE + Time*I(FTE^2) + (FTE | ID), data =
SHARE)
#Yes, the variables are on very different scales - will take care of
that later
I find a sizeable quadratic effect.
Fixed effects:
Estimate Std. Error t value
(Intercept) 6.760e+03 5.347e+02 12.642
Time 2.033e+01 1.011e+01 2.011
FTE 9.728e+01 8.583e+00 11.335
I(FTE^2) -5.155e-01 4.000e-02 -12.890
Time:FTE -5.560e-01 2.254e-01 -2.467
Time:I(FTE^2) 7.371e-03 1.052e-03 7.006
To plot the quadratic interaction, I attempt to use the effects package.
However, effects are displayed for Time x FTE, not time by FTE^2. Time x
FTE
is clearly not the plot that I want (I think...). e1 <- effect(term="Time:I(FTE^2)", mod=Q1) ed1<-as.data.frame(e1) ed1 Time FTE fit se lower upper 1 1 17 8277.635 464.3995 7366.770 9188.500 2 4 17 8316.650 463.3763 7407.792 9225.508 ...... I tried a workaround, by fitting a model that included FTE^2 as a second, calculated variable in the data set. Using the effects package, I do
indeed
obtain Time * FTE_sq. Q2 <- lmer(Patients ~ Time*FTE + Time*FTE_sq + (FTE | ID), data =
SHARE)
e2 <- effect(term="Time*FTE_sq", mod=Q2) ed2<-as.data.frame(e2) ed2 Time FTE_sq fit se lower upper 1 1 300 14678.1413 564.5423 13570.8582 15785.4243 2 4 300 14606.9253 563.3827 13501.9166 15711.9340 ...... But the plot does not at all look like what I would expect. All lines representing FTE_sq over time are straight.
Try, plot(Effect(c("Time", "FTE"), Q1)) .
More generally, why not fit the model as Q1 <- lmer(Patients ~
Time*poly(FTE, 2) + (FTE | ID), data = SHARE) or Q1 <- lmer(Patients ~
Time*poly(FTE, 2, raw=TRUE) + (FTE | ID), data = SHARE) ? Also, do you
really want the linear term in FTE to be random and the quadratic term only
fixed?
I hope this helps,
John
ggplot(ed2, aes(x=Time, y=fit, color=FTE_sq,group=FTE_sq)) +
geom_point() +
geom_line(size=1.2) +
labs(title = "Time x FTE^2", x= "Time",
y="Patients", color="FTE^2", fill="FTE^2") + theme_classic() +
theme(text=element_text(size=10))
Does my problem (obtaining effects for FTE^2*Time and accurately plotting
them) relate to my use of the effects package, ggplot, both?
Thank you for the feedback.
Matthew Boden, Ph.D.
Senior Evaluator
Program Evaluation & Resource Center
Office of Mental Health & Suicide Prevention Veterans Health
Administration
_______________________________________________ R-sig-mixed-models at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
_______________________________________________ R-sig-mixed-models at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models