title: How to use PCA on test set (code)
body: <p>I'm trying to use PCA to select some K principal components to work with.</p>
  
  <p>I understand that one should NOT re-run PCA on the testing set, but use the eigenvectors \ PC's found when modeling the training set.</p>
  
  <p><strong>I Have 2 CSV's - One is training set,</strong></p>
  
  <p><strong>The other a test set (without the label per record)</strong></p>
  
  <p>PCA process on the <strong>training set</strong> is done with the following code:</p>
  
  <pre><code># Load CSV file
  train_set.init_data &lt;- read.csv("D:\\train.csv", header = TRUE)
  
  # Remove identifier and respone variables (ID, and SalePrice):
  train_set.vars &lt;- subset(train_set.init_data, select = -c(Id, SalePrice))
  
  # Convert catergorical variables into numerical using dummy variables:
  library(dummies)
  train_set.vars_dummy &lt;- dummy.data.frame(train_set.vars, sep = ".")
  
  # Principal Component Analysis:
  train_set.prin_comp &lt;- prcomp(train_set.vars_dummy, scale. = T)
  
  # Choose some K components
  ????
  
  # Run linear regression model based on PC's
  &lt;.....&gt;
  </code></pre>
  
  <p>After I'm done building a model using the training set, I would need to load the testing set and run my prediction model on it.</p>
  
  <p>The difficulties I'm having, in terms of 'How to code it?':</p>
  
  <ol>
  <li><p>How to extract K (Will be chosen based on scree plot) PC's after running PCA (on the training set), so the modeling for the training set will be based on those? (planning on linear regression)</p></li>
  <li><p>How to use K extracted PC's when wanting to run the model built on an actual testing set?</p></li>
  <li><p>Should I zero-mean the features in the testing set first, or scale STD of them?
  For the training set, I understand <code>prcomp</code> method already does that for me, so I'm not sure if I should do it manually on the testing set.</p></li>
  <li><p>Should I transform categorical variables of test set into numerical using dummy variables, as I've done with the training set?</p></li>
  </ol>
  
  <p>I <strong>DO</strong> understand the basic - those same operations applied to the training set, should be applied to the testing set as well.</p>
  
  <p>But - I'm not sure exactly what that means in terms of code.</p>
  
  <p>Thanks</p>