How to know probability output by a model corresponds to which class?

Viewed 71

I am studying the third chapter of An Introduction to Statistical Learning with Application in R which discusses classification models. In section 4.7.3 Linear Discriminate Analysis (Lab) the model is applied on a dataset named Smarket to predict the up and down of the stock market. Here the total number of down and up prediction was done by the following lines of code: sum(lda.pred$posterior[, 1]>= .5) and sum(lda.pred$posterior[, 1] < .5) and the writers wrote that

Notice that the posterior probability output by the model corresponds to the probability that the market will decrease

and then to verify these line of codes were written:

lda.pred$posterior [1:20 , 1] 

which gives posterior probability of 20 observations and

lda.class [1:20]

which gives classes corresponding to the probabilities of the observations given above

Also when I wrote the line of code (thanks to ISLR online course):

data.frame(lda.pred)[1:20, ]

which gives classes and corresponding probabilites. Here is seen that observations having probabilities < 0.5 are classified as down class and observations having probabilities >= 0.5 are classified as up class.

This all is a bit confusing to me. My question is in the first case how do we know that when the probability is greater or equal to 0.5 the prediction is down? Because using contrast() function it is seen that R has created a dummy variable with a 1 for Up which means that the values correspond to the probability of the market going up, rather than down. Again in the second case why observations having probabilities >= 0.5 are classified as up? Don't the first and second case contradict?

1 Answers

You are predicting both, where the posterior probability of "Down" is 1 - "Up". It just so happens that class "Down" is stored in the first column of lda.pred lda.pred$posterior [1:20 , 1]. The probabilities of "Up" are stored in the second column. (lda.pred$posterior [1:20 , 2]).

Related