title: Loop multiple 'multiple linear regressions' in R
body: <p>I have a database where I want to do several multiple regressions. They all look like this:</p>
  
  <pre><code>fit &lt;- lm(Variable1 ~ Age + Speed + Gender + Mass, data=Data)
  </code></pre>
  
  <p>The only variable changing is variable1. Now I want to loop or use something from the apply family to loop several variables at the place of variable1. These variables are columns in my datafile. Can someone help me to solve this problem? Many thanks! </p>
  
  <p><strong>what I tried so far:</strong></p>
  
  <p>When I extract one of the column names with the names() function I do get a the name of the column:      </p>
  
  <pre><code>varname  = as.name(names(Data[14])) 
  </code></pre>
  
  <p>But when I fill this in (and I used the <code>attach()</code> function):</p>
  
  <pre><code>fit &lt;- lm(Varname ~ Age + Speed + Gender + Mass, data=Data) 
  </code></pre>
  
  <p>I get the following error: </p>
  
  <blockquote>
    <p>Error in model.frame.default(formula = Varname ~ Age + Speed + Gender
    +  :   object is not a matrix          </p>
  </blockquote>
  
  <p>I suppose that the lm() function does not recognize Varname as Variable1.</p>