title: How to save mapping of data.frame-to-model.matrix and apply to new observations?
body: <p>Some modeling functions, e.g. <code>glmnet()</code>, require (or just allow for) the data to be passed in as a predictor matrix and a response matrix (or vector) as apposed to using a formula. In these cases, it's typically the case that the <code>predict()</code> method, e.g. <code>predict.glmnet()</code>, requires that the <code>newdata</code> argument provides a predictor matrix with the same features as was used to train the model.</p>
  
  <p>A convenient way to create a predictor matrix when your dataframe has <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/factor.html" rel="noreferrer">factors</a> (R's categorical data type) is to use the <a href="https://stat.ethz.ch/R-manual/R-devel/library/stats/html/model.matrix.html" rel="noreferrer"><code>model.matrix()</code></a> function, which automatically creates dummy features for your categorical variables:</p>
  
  <pre><code># this is the dataframe and matrix I want to use to train the model
  set.seed(1)
  df &lt;- data.frame(x1 = factor(sample(LETTERS[1:5], replace = T, 20)),
                   x2 = rnorm(20, 100, 5),
                   x3 = factor(sample(c("U","L"), replace = T, 20)),
                   y = rnorm(20, 10, 2))
  
  mm &lt;- model.matrix(y~., data = df)
  </code></pre>
  
  <p>But when I introduce a dataframe with new observations that contain only a subset of the levels of the factors from the original dataframe, <code>model.matrix()</code> (predictably) returns a matrix with different dummy features. This new matrix cannot be used in <code>predict.glm()</code> because it doesn't have the same features that the model is expecting:</p>
  
  <pre><code># this is the dataframe and matrix I want to predict on
  set.seed(1)
  df_new &lt;- data.frame(x1 = c("B", "C"),
                       x2 = rnorm(2, 100, 5),
                       x3 = c("L","U"))
  
  mm_new &lt;- model.matrix(~., data = df_new)
  </code></pre>
  
  <p><strong>Is there a way to save the transformation (creating all necessary dummy features) from a dataframe to a model matrix so that I can re-apply this transformation to future observations?</strong> In my above example, this would ideally result in <code>mm_new</code> having identical feature names as <code>mm</code> so that <code>predict()</code> can accept <code>mm_new</code>.</p>
  
  <p>I want to add that I'm aware of <a href="https://stackoverflow.com/questions/41335718/keep-same-dummy-variable-in-training-and-testing-data">this approach</a>, which essentially suggests to include the observations from <code>df_new</code> in <code>df</code> before calling <code>model.matrix()</code>. This work fine if I have all the observations to begin with, and I'm just training and testing models. However, the new observations will only be accessible in the future (in a production prediction pipeline), and I want to avoid the overhead of re-loading the entire training dataframe for new predictions.</p>