title: How do you remove an insignificant factor level from a regression using the lm() function in R?
body: <p>When I perform a regression in R and use type factor it helps me avoid setting up the categorical variables in the data. But how do I remove a factor that is not significant from the regression to just show significant variables?</p>
  
  <p>For example:</p>
  
  <pre><code>dependent &lt;- c(1:10)
  independent1 &lt;- as.factor(c('d','a','a','a','a','a','a','b','b','c'))
  independent2 &lt;- c(-0.71,0.30,1.32,0.30,2.78,0.85,-0.25,-1.08,-0.94,1.33)
  output &lt;- lm(dependent ~ independent1+independent2)
  summary(output)
  </code></pre>
  
  <p>Which results in the following regression model:</p>
  
  <pre><code>Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)   
  (Intercept)     4.6180     1.0398   4.441  0.00676 **
  independent1b   3.7471     2.1477   1.745  0.14148   
  independent1c   5.5597     2.0736   2.681  0.04376 * 
  independent1d  -3.7129     2.3984  -1.548  0.18230   
  independent2   -0.1336     0.7880  -0.170  0.87203   
  </code></pre>
  
  <p>If I want to pull out the independent1 levels that are insignificant (b,d) is there a way that I can do that? </p>
  
  <p>In this case setting up the data to have categorical variables is easy but when I'm including week numbers or another factor with a lot of levels it becomes inconvenient. </p>
  
  <p>Here is the way to build the model using categorial variables. As you can see it ends up being more of a pain to structure the data but also gives me more control.</p>
  
  <pre><code>regressionData &lt;- data.frame(cbind(1:10,c(-0.71,0.30,1.32,0.30,2.78,0.85,-0.25,-1.08,-0.94,1.33),c(0,1,1,1,1,1,1,0,0,0),c(0,0,0,0,0,0,0,1,1,0),c(0,0,0,0,0,0,0,0,0,1),c(1,0,0,0,0,0,0,0,0,0)))
  
  names(output) = c('dependent','independent2','independenta', 'independentb','independentc','independentd')
  
  attach(regressionData)
  
  result &lt;- lm(dependent~independent2+independentb+independentc+independentd)
  summary(result)
  </code></pre>
  
  <p>Now I can remove independent2 since it's insignificant </p>
  
  <pre><code>result &lt;- lm(dependent~independentb+independentc+independentd)
  summary(result)
  </code></pre>
  
  <p>I'll remove independentd since it's not significant</p>
  
  <pre><code>result &lt;- lm(dependent~independentb+independentc)
  summary(result)
  </code></pre>
  
  <p>But in this case the Adjusted R Squared drops (I'm not even going to do the partial F-test) since it would be significant, but in many cases this is not true and I need to remove the categorical from the regression because it's eating up degrees of freedom which are important in this case and potential masking the value of other variables that are significant. </p>