Logistic Regression

STAT 20: Introduction to Probability and Statistics

Warmup
10:00

What are the best/worst values for Confusion matrix, MCR, Precision, Recall?

Imagine your sample has \(n=180\), with \(70\) positive observations, and \(110\) negative ones.

  • Diagonal Confusion matrix is best, completely off diagonal is worst.

\[ \text{Best case: } \begin{pmatrix} 110 & 0 \\ 0 & 70 \end{pmatrix} \qquad \text{Worst case: } \begin{pmatrix} 0 & 70 \\ 110 & 0 \end{pmatrix} \]

  • \(\mbox{MCR} = \frac{0 + 0}{180} = 0\) is best, \(\mbox{MCR} =\frac{110 + 70}{180} = 1\) is worst.
  • \(\mbox{Precision} = \frac{TP}{TP + 0} = 1\) is best, \(\mbox{Precision} = \frac{0}{0 + FP} = 0\) is worst.
  • \(\mbox{Recall} = \frac{70}{70} = 1\) is best, \(\mbox{Recall} = \frac{0}{70} = 0\) is worst.

Estimated probabilities

m1 <- glm(sex ~ body_mass_g, data = penguins, family = "binomial")
 (Intercept)  body_mass_g 
-5.162541644  0.001239819 

Write down the mathematical expression for the estimated probability that a penguin that weighs 4000 g is a female. Then write down the code to extract the same number.

Don’t forget that female is the reference level, \(Y=0\), so …

\[ \widehat{P} (\mbox{female} \mid \mbox{body_mass_g} = 4000 g ) = 1 - \widehat{p} (\mbox{body_mass_g} = 4000 g)= 1 - \frac1{1 + {\rm e}^{-\left( -5.162541644 + 0.001239819 \cdot 4000 \right)}} \]

1-predict(m1,newdata= data.frame(body_mass_g = 4000) ,type="response")
       1 
0.550642 
01:00

What is the misclassification rate, Precision, and Recall of this model?

# A tibble: 4 × 3
# Groups:   sex [2]
  sex    sex_hat     n
  <fct>  <chr>   <int>
1 female female    109
2 female male       56
3 male   female     74
4 male   male       94
01:00

(56+74)/(109+56+74+94) = 130/333

(94)/(94+56) = 94/150

(94)/(94+74) = 94/168

Concept Questions

Are FP or FN worse in this situation?

It’s up to you but I’d say misclassifying a male penguin as female or vice versa seems equally problematic. But maybe it isn’t. It depends on what you do with that prediction.

m2 <- glm(sex ~ body_mass_g + bill_length_mm, 
          data = penguins, family = "binomial")
   (Intercept)    body_mass_g bill_length_mm 
   -6.91208086     0.00101530     0.06112808 

Open up RStudio and fit the model here in the slides. What are the predicted sexes of these two penguins, using a probability threshold of 0.5?

  1. body mass = 3900 g, bill length = 50
  2. body mass = 4100 g, bill length = 35
01:00

Important: Attendance Required NEXT WEEK 11/18 and 11/20!

November 18 & November 20

Warning

We will be conducting an actual experiment in class on these dates.

You need to be present to participate!

It’s gonna be soooo much fun!

Break

05:00

Lab: Cancer Diagnosis

Lab: Cancer Diagnosis

45:00