title: Looping all pairwise comparisons from a list in R
body: <p>In my R code below, suppose I want to compare all unique 2 <code>m</code> objects using a similar R routine. For example, to compare <code>m1</code> and <code>m2</code>, my routine is:</p>
  
  <p><code>pchisq(2 * (logLik(m2) - logLik(m1)), df = abs(m1$df.residual - m2$df.residual), lower = F)</code></p>
  
  <p><strong>Question:</strong></p>
  
  <p>I was wondering how I could make a function to make all unique pairwise comparisons for all <code>m</code> objects using my routine?</p>
  
  <p>Here is what I've tried without success:</p>
  
  <pre><code>## Suppose we have 4 `m` objects: `m1...m4` (6 unique pairwise comparisons possible)
  
  m1 &lt;- lm(hp ~ vs, mtcars)
  m2 &lt;- lm(hp ~ vs*wt, mtcars)
  m3 &lt;- lm(hp ~., mtcars)
  m4 &lt;- lm(hp ~ vs * gear * wt, mtcars)
  
  
   compare &lt;- function(...){
  
     m &lt;- list(...)
     L &lt;- length(m) - 1
  
     lapply(1:L, function(i) pchisq(2 * (logLik(m[[i+1]]) - logLik(m[[i]])), df = abs(m[[i]]$df.residual - m[[i+1]]$df.residual), lower.tail = FALSE) )
  
    }
  # Example of use:
  compare(m1, m2, m3, m4)
  </code></pre>