title: Different results: “xgboost” vs. “caret” in R
body: <p>I try to setup a xgboost classification model in R</p>
  
  <ol>
  <li>with the xgboost library  </li>
  <li>with caret</li>
  </ol>
  
  <p>but I get different results (e.g. different predictions for the test dataset) despite I use the same settings(?).
  I use the Ionosphere dataset from the mlbench library and try to keep it as simple as possible for this example (no cross validation, parameter tuning etc.):</p>
  
  <p>Has someone an idea why I get different results (see below). Since caret is just a wrapper of xgboost ("it just calls the same xgboost package") the results should be exactly the same.</p>
  
  <pre><code>library(caret)
  library(xgboost)
  library(mlbench)
  
  #####
  ###Load &amp; Prepare Data
  #####
  data(Ionosphere)
  dataset &lt;- Ionosphere
  dataset &lt;- dataset[,-c(2)] #remove (Constant everywhere)
  dataset$V1 &lt;- as.numeric(as.character(dataset$V1)) #factor to numeric
  dataset$Class&lt;-ifelse(dataset$Class=="good",dataset$Class&lt;-1,dataset$Class&lt;-0) #convert good-&gt;1 and bad-&gt;0
  dataset$Class&lt;-as.factor(dataset$Class) #convert to factor
  
  #####
  ###Create Train &amp; Test Dataset
  #####
  set.seed(1992)
  validation_index&lt;-createDataPartition(dataset$Class, p=0.8, list=FALSE)
  testSet&lt;-dataset[-validation_index,]
  trainSet&lt;-dataset[validation_index,]
  
  #xgb.DMatrix for xgb.train() ("trainSet[c(34)]=trainSet$Class")
  xgb.trainData&lt;-xgb.DMatrix(data = data.matrix(trainSet[,-c(34)]), label = data.matrix(trainSet$Class))
  xgb.testData&lt;-xgb.DMatrix(data = as.matrix(testSet[,-c(34)]), label = as.matrix(data.matrix(testSet$Class)))
  
  #####
  ###Set parameters &amp; create models
  #####
  #params
  param &lt;-  data.frame(nrounds=c(100), max_depth = c(2),eta =c(0.3),gamma=c(0),
                      colsample_bytree=c(0.8),min_child_weight=c(1),subsample=c(1))                           
  
  #xgboost
  set.seed(1992)
  fit.xgb &lt;- xgb.train(
          params = list(eta = param$eta, max_depth = param$max_depth, 
              gamma = param$gamma, colsample_bytree = param$colsample_bytree, 
              min_child_weight = param$min_child_weight, subsample = param$subsample),    
          data = xgb.trainData, nrounds = param$nrounds, objective = "binary:logistic")
  
  #caret
  set.seed(1992)
  fit.xgbTree &lt;- train(Class~., data=trainSet, method="xgbTree",
                      metric="Accuracy", trControl=trainControl(method="none"),tuneGrid=param)
  
  #####
  ###Print results (predictions)
  #####
  print("xgboost")
  predictionxgb &lt;- as.numeric(predict(fit.xgb,  xgb.testData) &gt;= 0.5)
  confusionMatrix(predictionxgb,testSet$Class)
  #Confusion Matrix and Statistics
  #
  #          Reference
  #Prediction  0  1
  #         0 18  0
  #         1  7 45
  # ...
  
  
  print("caret")
  predictionsxgbTree &lt;- predict(fit.xgbTree, testSet)
  confusionMatrix(predictionsxgbTree, testSet$Class)
  #Confusion Matrix and Statistics
  #
  #          Reference
  #Prediction  0  1
  #         0 17  0
  #         1  8 45
  # ...
  </code></pre>
  
  <p>This information about the models may help too (I dont see an important difference):</p>
  
  <pre><code>#xgboost:
  fit.xgb
  ##### xgb.Booster
  #raw: 35.9 Kb 
  #call:
  #  xgb.train(params = list(eta = param$eta, max_depth = param$max_depth, 
  #    gamma = param$gamma, colsample_bytree = param$colsample_bytree, 
  #    min_child_weight = param$min_child_weight, subsample = param$subsample), 
  #    data = xgb.trainData, nrounds = param$nrounds, objective = "binary:logistic")
  #params (as set within xgb.train):
  #  eta = "0.3", max_depth = "2", gamma = "0", colsample_bytree = "0.8", min_child_weight = "1", subsample = "1", objective = "binary:logistic", silent = "1"
  #xgb.attributes:
  #  niter
  #callbacks:
  #  cb.print.evaluation(period = print_every_n)
  #niter: 100
  
  
  #caret:
  fit.xgbTree$finalModel
  ##### xgb.Booster
  #raw: 36 Kb 
  #call:
  #  xgboost::xgb.train(params = list(eta = param$eta, max_depth = param$max_depth, 
  #   gamma = param$gamma, colsample_bytree = param$colsample_bytree, 
  #   min_child_weight = param$min_child_weight, subsample = param$subsample), 
  #    data = x, nrounds = param$nrounds, objective = "binary:logistic")
  #params (as set within xgb.train):
  #  eta = "0.3", max_depth = "2", gamma = "0", colsample_bytree = "0.8", min_child_weight = "1", subsample = "1", objective = "binary:logistic", silent = "1"
  #xgb.attributes:
  #  niter
  #callbacks:
  #  cb.print.evaluation(period = print_every_n)
  #niter: 100
  #xNames: #V1V3V4V5V6V7V8V9V10V11V12V13V14V15V16V17V18V19V20V21V22V23V24V25V26V27V28V29V30V31V32V33V34
  #problemType: Classification
  #tuneValue:
  #          nrounds max_depth eta gamma colsample_bytree min_child_weight subsample
  #1     100         2 0.3     0              0.8                1         1
  #obsLevels: 01
  #param:
  #        list()
  </code></pre>