title: How to fit a nls model with mixed effects
body: <p>I want to fit a linear-plateau model with random effects. I found a way to fit the function with <code>nls()</code>, but I don't know how to include random effects. Here is what i have so far:</p>
  
  <pre><code>#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&lt;-data.frame(x,y,z)
  
  #create linear-plateau function
  lp=function(x, a, b, c){
    ifelse(x &gt; 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)
  </code></pre>
  
  <p>What I want to do is to include <code>z</code> as a random effect, since all data collected from the same <code>z</code> level are not independent.
  I know how to model mixed effects with <code>nlmer</code> function from <code>lme4</code> package, but I don't know how to fit the linear-plateau model with it.</p>