title: How to use substitute() to loop lme functions from nlme package?
body: <p>I am trying to use <a href="https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/lme.html" rel="nofollow noreferrer"><code>lme</code></a> function from <code>nlme</code> package inside a <code>lapply</code> loop. This works for <code>lmer</code> function from <a href="http://cran.r-project.org/web/packages/lme4/lme4.pdf" rel="nofollow noreferrer"><code>lme4</code></a> package, but produces an error message for <code>lme</code>. How can I loop <code>lme</code> functions similarly to the <code>lmer</code> function in the example below?</p>
  
  <pre><code>library("nlme")
  library("lme4")
  
  set.seed(1)
  dt &lt;- data.frame(Resp1 = rnorm(100, 50, 23), Resp2 = rnorm(100, 80, 15), Pred = rnorm(100,10,2), group = factor(rep(LETTERS[1:10], each = 10)))
  
  ## Syntax:
  lmer(Resp1 ~ Pred + (1 |group), data = dt)
  lme(Resp1 ~ Pred, random = ~1 | group, data = dt)
  
  ## Works for lme4
  lapply(c("Resp1", "Resp2"), function(k) {
    lmer(substitute(j ~ Pred + (1 | group), list(j = as.name(k))), data = dt)})
  
  ## Does not work for nlme
  lapply(c("Resp1", "Resp2"), function(k) {
  lme(substitute(j ~ Pred, list(j = as.name(k))), random = ~1 | group, data = dt)})
  
  # Error in UseMethod("lme") : 
  # no applicable method for 'lme' applied to an object of class "call"
  </code></pre>
  
  <p>PS. I am aware that <a href="https://stackoverflow.com/questions/24748724/looping-in-nlme">this solution</a> exists, but I would like to use a method substituting response variable directly in the model function instead of subsetting data using an additional function.</p>