1
$\begingroup$

R has more than one way to create logistic regressions to predict binary outcomes. Here's the code that I'm using that is giving strange answers.

library(ElemStatLearn)
data(SAheart)
set.seed(8484)
train <- sample(1:dim(SAheart)[1],size=dim(SAheart)[1]/2,replace=F)
trainSA <- SAheart[train,]
testSA <- SAheart[-train,]

Now, I can use two different packages to generate the logistic regression. Here's the first.

modFit4 <-
        glm(chd ~ age + alcohol + obesity + tobacco + typea + ldl,
            family = "binomial",
            data = trainSA)

and here's the second.

modFit4.1 <-
        train(
                form = as.factor(chd) ~ age + alcohol + obesity + tobacco + typea + ldl,
                method = "glm",
                data = trainSA
        )

When I run a "predict" function on each I find that the first gives many different numerical values. These range from -3.44 to +2.88. The second gives only 0 and 1 values (which, assuming I understand correctly, is the best sort of output for a binomial classification).

1.a.) Why are they different? 1.b.) Are there default parameters that are different in one than in the other? 2.) Is there a generally preferred package for use in real-world regression applications?

$\endgroup$
  • $\begingroup$ There is an important statistical rather than coding issue here: the difference between predicted probabilities and predicted class membership. So I don't think this is off topic despite the original title (which I am now editing). I'm not sure whether it's a duplicate. $\endgroup$ – EdM May 27 at 21:39
3
$\begingroup$

1.

Running predict on a glm object returns predictions on the log odds scale. If you want to return probabilities, run predict(modFit4, type = 'response'). To get predictions (i.e. 0 or 1), then you need to come up with a decision rule for when to assign an observation to 0 or 1 based on the probability. A simple rule is if the probability is larger than 50%, then assign it to 1 else 0. This can be achieved with round(predict(mod4Fit, type = 'response'))

This is because glm is usually used for inference rather than for prediction (usually, not always), whereas caret is more for prediction rather than inference, which explains why the predict method jumps right to predictions.

2.

The only general preference is your own. When all I care about is predictions, caret provides a nice API. If I want to do inference, then I use glm.

$\endgroup$
  • 3
    $\begingroup$ Note that the 50% rule you cite is presumably a hidden default parameter value in the second example of the OP. Depending on the relative costs of false positives and false negatives, that's not always the best choice. So it's important to bring that hidden default out into the open. $\endgroup$ – EdM May 27 at 21:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.