title: Calculating Gini and AUC in R , results depending on number of variables
body: <p>I tried to find R functions to calculate the Gini-coefficient and the AUC in R.
  I found the packages <code>ROCR</code> and <code>MLmetrics</code>. Usually you can switch between AUC and Gini by</p>
  
  <p>Gini = 2 AUC -1</p>
  
  <p>in the following example this is true for the case of 2 exaplantory variables but not true for the case of just one variable.</p>
  
  <p>Is this a flaw in the packages? </p>
  
  <pre><code>library(ROCR)
  library(MLmetrics)
  
  data(cars)
  ## 2 explanatory variables
  logreg &lt;- glm(vs ~ hp + wt,
                family = binomial(link = "logit"), data = mtcars)
  
  pred = prediction(logreg$fitted.values, mtcars$vs)
  auc_perf = performance( pred, measure = "auc" ) 
  ## both lines give 0.8968254
  2*auc_perf@y.values[[1]]-1
  Gini(y_pred = logreg$fitted.values, y_true = mtcars$vs)
  
  plot( performance( pred, "tpr", "fpr" )  )
  
  ## 1 explanatory variable
  logreg &lt;- glm(vs ~ hp ,
                family = binomial(link = "logit"), data = mtcars)
  
  pred = prediction(logreg$fitted.values, mtcars$vs)
  
  auc_perf = performance( pred, measure = "auc" ) 
  
  ## different results:
  2*auc_perf@y.values[[1]]-1
  Gini(y_pred = logreg$fitted.values, y_true = mtcars$vs)
  
  plot( performance( pred, "tpr", "fpr" )  )
  </code></pre>
  
  <p>Session info is:</p>
  
  <blockquote>
    <p>R version 3.4.3 (2017-11-30) Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 7 x64 (build 7601) Service Pack 1</p>
    
    <p>Matrix products: default</p>
    
    <p>locale: [1] LC_COLLATE=German_Austria.1252 
    LC_CTYPE=German_Austria.1252    [3] LC_MONETARY=German_Austria.1252
    LC_NUMERIC=C                    [5] LC_TIME=German_Austria.1252    </p>
    
    <p>attached base packages: [1] stats     graphics  grDevices utils<br>
    datasets  methods   base     </p>
    
    <p>other attached packages: [1] MLmetrics_1.1.1 ROCR_1.0-7<br>
    gplots_3.0.1   </p>
    
    <p>loaded via a namespace (and not attached): [1] compiler_3.4.3<br>
    tools_3.4.3        KernSmooth_2.23-15 gdata_2.18.0       [5]
    caTools_1.17.1     bitops_1.0-6       pacman_0.4.6       gtools_3.5.0</p>
  </blockquote>