title: Cross-validating lm model and returning an lm class in R
body: <p>Suppose I'm trying to do some cross validation and have a training set and a validation set. Maybe something like this:</p>
  <pre><code>full_data = read.csv(&quot;data/my_data.csv&quot;)
  in_training_data = sample(1:nrow(full_data), size=.67*nrow(full_data))
  training_set = full_data[in_training_data,]
  validation_set = full_data[!(1:nrow(full_data) %in% in_training_data),]
  </code></pre>
  <p>What I want to do is estimate the fit with <code>training_set</code>, then pass <code>validation_set</code> into a new model <em>without refitting the data</em>. Something like this would be ideal:</p>
  <pre><code>initial_model = lm(y~x+z, data=training_set)
  validation_model = lm(y~x+z, data=validation_set, model=initial_model, refit=FALSE)
  </code></pre>
  <p>If this were the case, I could use all the same functions on <code>validation_model</code> that I could on <code>initial_model</code> (<code>coef</code>, <code>summary</code>, etc.). Alas, the documentation doesn't have &quot;model&quot; or &quot;refit&quot; options.</p>
  <p>I could, of course, use <code>predict</code> to estimate the new fitted values:</p>
  <pre><code>new_fits = predict(initial_model, newdata = validation_model)
  </code></pre>
  <p>And that's fine, but it only returns the fitted values. With some R ninja moves, I could create a function that basically recreates the <code>lm</code> class, but that seems quite cumbersome.</p>
  <p>I could also use <code>offset</code> to fix the parameters of the model, but that doesn't return r squared, the original coefficients, or many of the other objects contained within a traditional <code>lm</code>.</p>
  <p>So, <strong>is there a way to refit an old model to a new dataset and return an <code>lm</code> class?</strong> Is my only option to basically recreate the <code>lm</code> class using the output of the <code>predict</code> function?</p>
  <p>And, to preempt potential answers, I know about k-fold cross-validation, but that's not what I want. I don't want an algorithm that does the split/estimation for me. I already have the split and just want to feed it into a new model.</p>