[R-sig-ME] LMER - Plotting of a quadratic effect interacting with time
Boden, Matthew T.
M@tthew@Boden @end|ng |rom v@@gov
Wed Jan 30 22:27:20 CET 2019
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.
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
More information about the R-sig-mixed-models
mailing list