The predict()
function can be used to predict the probability that the
market will go up, given values of the predictors. The type="response"
option tells R to output probabilities of the form \(P(Y = 1|X)\), as opposed
to other information such as the logit. If no data set is supplied to the
predict()
function, then the probabilities are computed for the training
data that was used to fit the logistic regression model. Here we have printed
only the first ten probabilities. We know that these values correspond to
the probability of the market going up, rather than down, because the
contrasts()
function indicates that R has created a dummy variable with
a 1 for Up.
> glm.probs <- predict(glm.fit, type = "response")
> glm.probs[1:10]
1 2 3 4 5 6 7 8
0.5070841 0.4814679 0.4811388 0.5152224 0.5107812 0.5069565 0.4926509 0.5092292
9 10
0.5176135 0.4888378
> contrasts(Direction)
Up
Down 0
Up 1
Try calculating the probabilities for this model:
Assume that:
ISLR2
library has been loadedSmarket
dataset has been loaded and attached