I tried to create a graph in R to describe a quadratic term with a random effect.
I used the function lme() with a random effect which included random slope and intercept:
diversity = c(0.69, 1.54, 0.84, 1.48, 1.71, 1.80, 2.09, 1.63, 2.40, 2.20,
2.56, 2.30, 2.67, 1.98, 1.65, 2.33, 2.17, 1.98, 1.96, 1.33, 2.55, 2.49, 2.39, 2.47, 2.42, 2.44, 2.35, 2.33, 2.01, 2.39)
Plot_age = c(7, 7, 9, 12, 17, 19, 22, 32, 31, 35, 35, 36, 36, 36, 37, 37,
37, 38, 38, 38, 110, 111, 112, 113, 115, 116, 117, 118, 120, 121)
subject = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C")
this data frame gives you only an overview of my actual data. I hope it will work anyways.
age <- Plot_age
div <- diversity
age2 <- Plot_age^2
qm1r <- lme( fixed = div ~ age+age2,
random = ~ age |subject)
To get a graph I used the plot() function as usually. With the lm() it worked, now I also used the predict() function and lme() with the following terms:
plot(age, div, pch = 16)
X <- seq(0, 200, 0.1)
#X has a range of 0 to 200 which include all of my x-axis data
NewData <- data.frame(age= Plot_age,
age2 = Plot_age^2,
subject = subject)
Y <- predict(qm1r, NewData)
points(Y ~ X, type ="l", lwd=3)
I got a graphical representation of my points() function. Unfortunately it didnt came out right (I wanted a quadratic parabola, no nonlinear line) -->see image below
xmax <- X[Y == max(Y)]
#The reason why I used this function was to get the xmax depending on the peak of the quadratic parabola graph (this worked when I only used the lm() function, lme() did not work) Is there any way to create a graphical representation of my lme() function with random effects? I would be so glad if someone could solve this problem :) I got a graph but no quadratic regression out of the points() function enter image description here