title: Why does summary() output label factor levels differently for models, depending on previous commands?
body: <p>I am working on a large R project to perform different analyses of a common dataset.  I have built up several individual scripts for each analysis, as well as high-level scripts to call each one in sequence.  Each script starts by calling an <code>init.R</code> script that wipes the memory ( <code>rm(list=ls(all=TRUE))</code> ).</p>
  
  <p>I have recently discovered that <code>summary()</code> (and, I think <code>coef()</code>) produces different output, depending on the order of the scripts.  In scripts that fit models using <code>lm()</code> or <code>gam()</code> (<code>mgcv</code> package), if these are run first, in a "fresh" R session, the <code>summary()</code> output lists factors with the full labels.</p>
  
  <p>However, if I run other scripts first, which use simple nested <code>aov()</code> functions and produce some graphs and other output using some other packages, then re-run the previously-mentioned scripts, <code>summary()</code> instead produces output with factor levels labeled using numbers (the 'coded' values, not the actual factor level labels).</p>
  
  <p>This is not something I can easily "reproduce" using a minimal working example, unfortunately, because I haven't quite pinpointed where in my scripts this behaviour changes.  I have confirmed a few things in quick tests:</p>
  
  <ul>
  <li>memory is cleared in between scripts using <code>rm(list=ls())</code>, so there shouldn't be anything in memory causing this change.</li>
  <li><code>summary()</code> itself does not change: the model-fitting functions actually produce slightly different output (as confirmed with <code>all.equal()</code> ), which is even more disturbing.  Saved objects produced when running the scripts in a different order reliably produce the same output whenever they are loaded, but that output differs depending on the order of scripts used to generate the fitted model objects (even though memory is cleared in between each script).
  
  <ul>
  <li>Depending on the order of scripts, <code>summary( lm(...) )</code> also outputs different estimates for model terms, but the same Residuals summary, R^2, and overall F-test.  Very bizarre.</li>
  </ul></li>
  <li>I can not recover the default (desired) behaviour by removing packages loaded in prior scripts.  Does the order of loading packages matter?</li>
  <li>default behaviour is restored after quitting and re-starting R
  
  <ul>
  <li>nothing in this answer seemed to fix the problem:  <a href="https://stackoverflow.com/questions/3716764/reset-r-instance">Reset R instance</a></li>
  </ul></li>
  </ul>
  
  <p>Ideally, I would like my project to be able to reproduce all results and output by simply <code>source()</code>ing each script in turn, but this strange 'bug' (in my code - I'm not blaming this on R) means that the output is not consistent and depends on the order :(</p>
  
  <p>Is there anything other than objects or packages that stays in memory that could alter the way model-fitting functions work, or store factor levels in data-frames that are passed in?</p>
  
  <h2>EDIT</h2>
  
  <p>I realized the answer to the above question was the contrasts option (see below).  New question:</p>
  
  <p>How can you reset options() to the default settings, i.e. to the values used when R starts up?  The 'factory default' is
  <code>options(contrasts=c("contr.treatment","contr.poly")))</code> but I'm wondering if there is a way to restart to the internal defaults (in case they aren't 'factory fresh'.</p>