title: difference between sparse.model.matrix and as.matrix
body: <p>I am using xgboost in R to build a classification model on a training dataset <code>train</code>. <code>train</code> has following attributes.</p>
  
  <ol>
  <li>it has all numeric features</li>
  <li>no missing values.</li>
  <li>no categorical/text/factor variables present.</li>
  <li><code>TARGET</code> is the target variable taking 1/0 values.</li>
  </ol>
  
  <p>xgb.train requires data to be passed in a specific format, so I tried following 2 ways to get the format required by xgb.train.</p>
  
  <p>1.</p>
  
  <pre><code>y &lt;- train$TARGET
  train &lt;- sparse.model.matrix(TARGET ~ ., data = train) ##has 76000 rows and 307 columns
  xgtrain &lt;- xgb.DMatrix(data=train, label=y)
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>y &lt;- train$TARGET
  train$TARGET &lt;- NULL
  x = as.matrix(train)
  x = matrix(as.numeric(x),nrow(x),ncol(x)) ##has 76000 rows and 307 columns
  xgtrain = xgb.DMatrix(x, label = y)
  </code></pre>
  
  <p><strong>Now, when I score the test set using the model built by these 2 <code>xgtrain</code> creating methods, I get different scores/AUC. I have verified that it is not the problem with randomization since I have been able to reproduce the scores if I run 1st method or 2nd method twice/thrice. Something is happening differently in the 2 methods which I have not been able to figure out. Can somebody please help me identify that difference?</strong> </p>
  
  <p>Question in another words.
  <strong><em>What does <code>sparse.model.matrix</code> do differently from <code>as.matrix</code> when the data provided to them has only numeric features with no missing values and no categorical variables?</em></strong></p>