I want to fit a linear-plateau model with random effects. I found a way to fit the function with nls()
, but I don't know how to include random effects. Here is what i have so far:
#create data
x=c(1:6,1:6)
y=c(10,21,27,35,33,35,9,20,28,32,30,31)
z=c("A","A","A","A","A","A","B","B","B","B","B","B")
df<-data.frame(x,y,z)
#create linear-plateau function
lp=function(x, a, b, c){
ifelse(x > c, a + b * c, a + b * x)
}
#fit the model without random effects
p10=nls(y ~ lp(x, a, b, c), data = df, start = list(a = 0, b = 15, c = 4))
plot(y~x)
lines(x=c(0, coef(p10)["c"],max(df$x)),
y=c(coef(p10)["a"],
(coef(p10)["a"] + coef(p10)["b"] * coef(p10)["c"]),
(coef(p10)["a"] + coef(p10)["b"] * coef(p10)["c"])),lty=2)
What I want to do is to include z
as a random effect, since all data collected from the same z
level are not independent.
I know how to model mixed effects with nlmer
function from lme4
package, but I don't know how to fit the linear-plateau model with it.