class: center, middle, title-slide, inverse, no-scribble layout: false background-image: url("media/as_snowy_village.jpg") background-size: cover # Categorical outcomes: logistic regression ## Professor Andy Field <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/twitter_60.png"> <span style="line-height:40px;">@profandyfield</span> </div> <div> <img style="vertical-align:middle; width:60px" src="media/youtube.png"> <span style="line-height:40px;">www.youtube.com/user/ProfAndyField/</span> </div> <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/ds_com_fav.png"> <span style="line-height:40px;">www.discoveringstatistics.com</span> </div> <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/milton_grey_fav.png"> <span style="line-height:40px;">www.milton-the-cat.rocks</span> </div> <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/discovr_fav.png"> <span style="line-height:40px;">www.discovr.rocks</span> </div> ??? h or ?: Toggle the help window j: Jump to next slide k: Jump to previous slide b: Toggle blackout mode m: Toggle mirrored mode. p: Toggle PresenterMode f: Toggle Fullscreen t: Reset presentation timer <number> + <Return>: Jump to slide <number> c: Create a clone presentation on a new window --- class: center, middle, title-slide, inverse layout: false background-image: url("media/as_snowy_village.jpg") background-size: cover exclude: ![:isSV] # Categorical outcomes: .red[Christmas] log~~istic~~ regression ## ~~Professor Andy Field~~ .red[Santa] <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/twitter_60.png"> <span style="line-height:40px;">@profandyfield</span> </div> <div> <img style="vertical-align:middle; width:60px" src="media/youtube.png"> <span style="line-height:40px;">www.youtube.com/user/ProfAndyField/</span> </div> <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/ds_com_fav.png"> <span style="line-height:40px;">www.discoveringstatistics.com</span> </div> <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/milton_grey_fav.png"> <span style="line-height:40px;">www.milton-the-cat.rocks</span> </div> <div> <img style="vertical-align:middle; width:30px; height:30px" src="media/discovr_fav.png"> <span style="line-height:40px;">www.discovr.rocks</span> </div> --- background-image: none background-color: #000000 class: no-scribble exclude: ![:isSV] <video width="100%" height="100%" controls id="my_video"> <source src="media/xmas_santa_01.mp4" type="video/mp4"> </video> ??? Santa: long intro [After Santa says about Krampus and head popping out of bucket ...] Andy: Santa, I can help. Tell me about your operations. --- background-image: none background-color: #000000 class: no-scribble exclude: ![:isSV] <video width="100%" height="100%" controls id="my_video"> <source src="media/xmas_santa_02.mp4" type="video/mp4"> </video> --- class: center  ??? We've seen this map of the process of fitting models before --- class: center exclude: ![:isSV]  ??? Today we focus back on the model itself to look at the form of the model we're fitting. The faded stuff still applies though - we'll look at bias, robust models, and of course samples and estimation and so on. But they are the same as for other models, what is different is the form of the model we're fitting. --- class: inverse background-image: url("media/as_blu_house_93514381.jpg") background-size: cover # A festive example .pull-right[ Santa Claus wanted to test the effects of different types of treats on whether presents got delivered: * Predictors + **treat**: Christmas pudding, Mulled wine * Outcome + **delivered**: Did the presents get delivered? ] ??? --- class: inverse background-image: url("media/as_santa_moon_36326866.jpg") background-size: cover exclude: ![:isSV] --- class: inverse background-image: url("media/as_santa_moon_36326866.jpg") background-size: cover # The data in <svg style="height: 1em; top:.04em; position: relative; fill: #136CB9;" viewBox="0 0 581 512"><path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"/></svg>
--- class: inverse background-image: url("media/as_snowy_trees_126530339.jpg") background-size: cover # If it were a standard linear model .center[.eq_lrge[.white[ $$ `\begin{aligned} \text{delivered}_{i} &= \hat{b}_{0} + \hat{b}_{1}\text{treat}_{i} + e_{i} \end{aligned}` $$ ]]] </br> .pull-left[ Assumption of linearity * Violated with categorical outcomes * We can’t fit this model ] .pull-right[.whitebox9[ <table> <thead> <tr> <th style="text-align:left;"> Group </th> <th style="text-align:center;"> Treat </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Christmas pudding </td> <td style="text-align:center;"> 0 </td> </tr> <tr> <td style="text-align:left;"> Mulled wine </td> <td style="text-align:center;"> 1 </td> </tr> </tbody> </table> ]] --- class: inverse background-image: url("media/as_blu_globe_391811093.jpg") background-size: cover # But it’s not a standard linear model .pull-right[ We predict the probability of the outcome occurring .eq_lrge[.white[ $$ `\begin{aligned} P(Y) &= \frac{1}{1+ e^{-(\hat{b}_0 + \hat{b}_1X_i+e_i)}} \\ P(\text{delivery}) &= \frac{1}{1+ e^{-(\hat{b}_0 + \hat{b}_1\text{treat}_i+e_i)}} \\ \end{aligned}` $$ ]] Note the equation contains the linear model ] --- class: inverse background-image: url("media/as_snowy_trees_126530339.jpg") background-size: cover # Or … .eq_lrge[.white[ $$ `\begin{aligned} \ln\bigg(\frac{P(Y)}{1- P(Y)}\bigg) &= \hat{b}_0 + \hat{b}_1X_{i} + e_i \\ \ln\bigg(\frac{P(\text{delivery})}{1- P(\text{delivery})}\bigg) &= \hat{b}_0 + \hat{b}_1\text{treat}_{i} + e_i \\ \end{aligned}` $$ ]] -- .pull-left[.blubox[ ### Outcome * We predict the log odds of the outcome occurring ]] -- .pull-right[.blubox[ ### `\(\hat{b}_0\)` and `\(\hat{b}_1\)` * Note the the logistic regression equation is the same as the linear model, except we predict the log odds of the outcome * `\(\hat{b}_1\)` is the change in the log odds of the outcome associated with a unit change in the predictor ]] --- class: inverse background-image: url("media/as_red_santa_globe_45715114.jpg") background-size: cover # Logs and exponents .pull-right[  ] --- background-image: url("media/as_snowman_wave_303329070.jpg") background-size: cover # The odds ratio: exp(*b*) .center[.eq_lrge[ $$ `\begin{aligned} \exp(b) = \frac{\text{odds after a unit change in the predictor}}{\text{original odds}} \end{aligned}` $$ ]] ### *b*<sub>0</sub> * Log odds of outcome when the predictors are 0 * Easier to interpret exp(*b*<sub>0</sub>), the odds of outcome when predictor is 0 ### *b*<sub>1</sub> * Change in the log odds of outcome associated with a unit change in the predictor * Easier to interpret exp (*b*<sub>1</sub>), the odds ratio associated with a unit change in the predictor * OR > 1: Predictor `\(\uparrow\)`, probability of outcome occurring `\(\uparrow\)` * OR < 1: Predictor `\(\uparrow\)`, probability of outcome occurring `\(\downarrow\)` --- class: inverse background-image: url("media/as_santa_vortex_94489562.jpg") background-size: cover exclude: ![:isSV] --- class: inverse background-image: url("media/as_santa_vortex_94489562.jpg") background-size: cover # Classification table .pull-left[.whitebox9[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:center;"> Delivered </th> <th style="text-align:center;"> Not delivered </th> <th style="text-align:center;"> Total </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Christmas pudding </td> <td style="text-align:center;"> 150 </td> <td style="text-align:center;"> 28 </td> <td style="text-align:center;color: #912D25 !important;"> 178 </td> </tr> <tr> <td style="text-align:left;"> Mulled wine </td> <td style="text-align:center;"> 100 </td> <td style="text-align:center;"> 122 </td> <td style="text-align:center;color: #912D25 !important;"> 222 </td> </tr> <tr> <td style="text-align:left;color: #912D25 !important;"> Total </td> <td style="text-align:center;color: #912D25 !important;"> 250 </td> <td style="text-align:center;color: #912D25 !important;"> 150 </td> <td style="text-align:center;color: #912D25 !important;color: #912D25 !important;"> 400 </td> </tr> </tbody> </table> ]] .pull-right[.whitebox9[ $$ `\begin{aligned} \text{odds}_\text{delivery} &= \frac{\text{Number delivered}}{\text{Number not delivered}} \\ &= \frac{250}{150} \\ &= 1.67 \end{aligned}` $$ ]] --- class: inverse background-image: url("media/as_gingerbread_229820523.jpg") background-size: cover exclude: ![:isSV] --- class: inverse background-image: url("media/as_gingerbread_229820523.jpg") background-size: cover # The odds ratio .whitebox9[ $$ `\begin{aligned} \text{odds}_\text{delivered after pudding} &= \frac{\text{Number delivered after pudding}}{\text{Number not delivered after pudding}} \\ &= \frac{150}{28} \\ &= 5.36 \end{aligned}` $$ ] </br> -- .whitebox9[ $$ `\begin{aligned} \text{odds}_\text{delivered after wine} &= \frac{\text{Number delivered after wine}}{\text{Number not delivered after wine}} \\ &= \frac{100}{122} \\ &= 0.82 \end{aligned}` $$ ] --- class: inverse background-image: url("media/as_gingerbread_229820523.jpg") background-size: cover # The odds ratio .whitebox9[.eq_lrge[ $$ `\begin{aligned} \text{odds ratio} &= \frac{\text{odds}_\text{delivered after wine}}{\text{odds}_\text{delivered after pudding}} \\ &= \frac{0.82}{5.36} \\ &= 0.15 \end{aligned}` $$ ]] --- class: inverse background-image: url("media/as_gingerbread_229820523.jpg") background-size: cover # The odds ratio .whitebox9[.eq_lrge[ $$ `\begin{aligned} \text{odds ratio} &= \frac{\text{odds}_\text{delivered after pudding}}{\text{odds}_\text{delivered after wine}} \\ &= \frac{5.36}{0.82} \\ &= 6.54 \end{aligned}` $$ ]] --- class: inverse background-image: url("media/as_santa_over_village_71686107.jpg") background-size: cover exclude: ![:isSV] --- class: inverse background-image: url("media/as_santa_over_village_71686107.jpg") background-size: cover # Fitting the model ```r santa_mod <- glm(delivered ~ treat, data = santa_tib, family = binomial()) santa_mod |> broom::tidy(conf.int = TRUE) |> knitr::kable(digits = 2, align = 'lcccccc') ``` .blubox[ <table class="table" style="font-size: 28px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> std.error </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 1.68 </td> <td style="text-align:center;"> 0.21 </td> <td style="text-align:center;"> 8.15 </td> <td style="text-align:center;"> 0 </td> <td style="text-align:center;"> 1.29 </td> <td style="text-align:center;"> 2.10 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> treatMulled wine </td> <td style="text-align:center;color: #2C5577 !important;"> -1.88 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.25 </td> <td style="text-align:center;color: #2C5577 !important;"> -7.63 </td> <td style="text-align:center;color: #2C5577 !important;"> 0 </td> <td style="text-align:center;color: #2C5577 !important;"> -2.37 </td> <td style="text-align:center;color: #2C5577 !important;"> -1.41 </td> </tr> </tbody> </table> ] --- class: inverse background-image: url("media/as_santa_over_village_71686107.jpg") background-size: cover # Odds ratios ```r santa_mod |> broom::tidy(conf.int = TRUE, `exponentiate = TRUE`) |> knitr::kable(digits = 2, align = 'lcccccc') ``` .blubox[ <table class="table" style="font-size: 28px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> std.error </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 5.36 </td> <td style="text-align:center;"> 0.21 </td> <td style="text-align:center;"> 8.15 </td> <td style="text-align:center;"> 0 </td> <td style="text-align:center;"> 3.64 </td> <td style="text-align:center;"> 8.18 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> treatMulled wine </td> <td style="text-align:center;color: #2C5577 !important;"> 0.15 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.25 </td> <td style="text-align:center;color: #2C5577 !important;"> -7.63 </td> <td style="text-align:center;color: #2C5577 !important;"> 0 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.09 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.24 </td> </tr> </tbody> </table> ] --- background-image: none background-color: #000000 class: no-scribble exclude: ![:isSV] <video width="100%" height="100%" controls id="my_video"> <source src="media/xmas_scene_2.mp4" type="video/mp4"> </video> --- class: inverse background-image: url("media/as_blu_house_93514381.jpg") background-size: cover # A festive example .pull-right[ Santa Claus wanted to test the effects of different types of treats and the quantity of them consumed on whether presents got delivered: * Predictors + **treat**: Christmas pudding, Mulled wine + **quantity**: 0, 1, 2, 3, or 4 * Outcome + **delivered**: Did the presents get delivered? ] --- class: inverse background-image: url("media/as_santa_moon_36326866.jpg") background-size: cover # The data in <svg style="height: 1em; top:.04em; position: relative; fill: #136CB9;" viewBox="0 0 581 512"><path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"/></svg>
--- class: inverse background-image: url("media/as_red_snowflake_228468210.jpg") background-size: cover # Extending the model .center[.eq_med[.white[ $$ `\begin{aligned} \text{delivered}_{i} &= \hat{b}_{0} + \hat{b}_{1}\text{treat}_{i} + \hat{b}_{2}\text{quantity}_{i} + \hat{b}_{3}\text{treat} \times \text{quantity}_{i} + e_{i} \end{aligned}` $$ ]]] .pull-right[.whitebox9[ <table> <thead> <tr> <th style="text-align:left;"> Group </th> <th style="text-align:center;"> Treat </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Christmas pudding </td> <td style="text-align:center;"> 0 </td> </tr> <tr> <td style="text-align:left;"> Mulled wine </td> <td style="text-align:center;"> 1 </td> </tr> </tbody> </table> ]] </br> -- .eq_med[.white[ $$ `\begin{aligned} P(\text{delivery}) &= \frac{1}{1+e^{-(\hat{b}_0 + \hat{b}_1\text{treat}_{i} + \hat{b}_2\text{quantity}_{i} + \hat{b}_3\text{treat} \times \text{quantity}_{i} + e_i)}} \\ \end{aligned}` $$ ]] -- .eq_med[.white[ $$ `\begin{aligned} \ln\bigg(\frac{P(\text{delivery})}{1- P(\text{delivery})}\bigg) &= \hat{b}_0 + \hat{b}_1\text{treat}_{i} + \hat{b}_2\text{quantity}_{i} + \hat{b}_3\text{treat} \times \text{quantity}_{i} + e_i \\ \end{aligned}` $$ ]] --- class: inverse background-image: url("media/as_tree_train_93514522.jpg") background-size: cover # Building the model .pull-left[ * Forced entry: all variables entered simultaneously. * Hierarchical: variables entered in blocks. + Blocks should be based on past research, or theory being tested. + Good method. * Stepwise: variables entered on the basis of statistical criteria (i.e., Relative contribution to predicting outcome). + Should be used only for exploratory analysis. ] --- class: inverse background-image: url("media/as_blu_house_93514381.jpg") background-size: cover # Things that can go wrong .pull-right[ ### Things we’ve met before .eq_med[ * Linearity (of the logit) * Spherical residuals + Independent errors * Multicollinearity ] ### Unique problems .eq_med[ * Incomplete information * Complete separation ] ] --- class: inverse background-image: url("media/as_santa_moon_37337682.jpg") background-size: cover # Incomplete information .pull-left[.blubox[ ### Empty cells .eq_lrge[* We don’t know how many presents are delivered after two puddings or not delivered after 5 wines * Problem quickly escalates with continuous predictors * Inflates standard errors ]]] .pull-right[.blubox[ <table> <thead> <tr> <th style="text-align:left;background-color: #15224c !important;font-size: 18px;"> </th> <th style="text-align:center;background-color: #15224c !important;font-size: 18px;"> Quantity </th> <th style="text-align:center;background-color: #15224c !important;font-size: 18px;"> Delivered </th> <th style="text-align:center;background-color: #15224c !important;font-size: 18px;"> Not delivered </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Pudding </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 0 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 27 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 3 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Pudding </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 1 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 36 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 10 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Pudding </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 2 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> - </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 5 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Pudding </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 3 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 37 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 6 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Pudding </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 4 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 12 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 4 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Mulled wine </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 0 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 27 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 3 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Mulled wine </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 1 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 32 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 11 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Mulled wine </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 2 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 24 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 36 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Mulled wine </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 3 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 14 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 48 </td> </tr> <tr> <td style="text-align:left;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> Mulled wine </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 4 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> 3 </td> <td style="text-align:center;color: white !important;background-color: rgba(32, 21, 34, 0.3) !important;font-size: 18px;"> - </td> </tr> </tbody> </table> ]] --- background-image: url("media/as_krampus_374161953.jpg") background-size: cover # Complete separation When the outcome variable can be perfectly predicted * Predicting whether someone is a krampus based on weight .pull-left[ ### Santa vs. krampus <!-- --> ]   -- .pull-right[ ### Elves vs. krampus <!-- --> ]   --- exclude: ![:isSV] class: no-scribble <video width="100%" height="100%" controls id="my_video"> <source src="media/logistic_regression_song_instrumental.mp4" type="video/mp4"> </video> --- class: no-scribble exclude: ![:isSV] <video width="100%" height="100%" controls id="my_video"> <source src="media/logistic_regression_song.mp4" type="video/mp4"> </video> --- class: inverse background-image: url("media/as_snowy_trees_white_126530339.jpg") background-size: cover # Fitting the model ```r santa_full_mod <- glm(delivered ~ treat*quantity, data = santa_tib, family = binomial()) santa_full_mod |> broom::tidy(conf.int = TRUE) |> knitr::kable(digits = 2, align = 'lcccccc') ``` </br> -- .blubox[ <table class="table" style="font-size: 28px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> std.error </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 1.83 </td> <td style="text-align:center;"> 0.38 </td> <td style="text-align:center;"> 4.80 </td> <td style="text-align:center;"> 0.00 </td> <td style="text-align:center;"> 1.13 </td> <td style="text-align:center;"> 2.63 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> treatMulled wine </td> <td style="text-align:center;color: #2C5577 !important;"> 0.20 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.52 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.38 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.70 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.83 </td> <td style="text-align:center;color: #2C5577 !important;"> 1.22 </td> </tr> <tr> <td style="text-align:left;"> quantity </td> <td style="text-align:center;"> -0.08 </td> <td style="text-align:center;"> 0.17 </td> <td style="text-align:center;"> -0.48 </td> <td style="text-align:center;"> 0.63 </td> <td style="text-align:center;"> -0.41 </td> <td style="text-align:center;"> 0.25 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> treatMulled wine:quantity </td> <td style="text-align:center;color: #2C5577 !important;"> -1.03 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.23 </td> <td style="text-align:center;color: #2C5577 !important;"> -4.45 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.00 </td> <td style="text-align:center;color: #2C5577 !important;"> -1.49 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.58 </td> </tr> </tbody> </table> ] --- class: inverse background-image: url("media/as_snowy_trees_white_126530339.jpg") background-size: cover -- </br> </br> .pull-left[ ```r santa_tib |> filter(treat == "Pudding") |> glm(delivered ~ quantity, data = _, family = binomial()) ``` .blubox[ <table class="table" style="font-size: 16px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 1.83 </td> <td style="text-align:center;"> 4.80 </td> <td style="text-align:center;"> 0.00 </td> <td style="text-align:center;"> 1.13 </td> <td style="text-align:center;"> 2.63 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> quantity </td> <td style="text-align:center;color: #2C5577 !important;"> -0.08 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.48 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.63 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.41 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.25 </td> </tr> </tbody> </table> ]]  -- .pull-right[ ```r santa_tib |> filter(treat == "Mulled wine") |> glm(delivered ~ quantity, data = _, family = binomial()) ``` .blubox[ <table class="table" style="font-size: 16px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 2.03 </td> <td style="text-align:center;"> 5.73 </td> <td style="text-align:center;"> 0 </td> <td style="text-align:center;"> 1.37 </td> <td style="text-align:center;"> 2.76 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> quantity </td> <td style="text-align:center;color: #2C5577 !important;"> -1.11 </td> <td style="text-align:center;color: #2C5577 !important;"> -6.99 </td> <td style="text-align:center;color: #2C5577 !important;"> 0 </td> <td style="text-align:center;color: #2C5577 !important;"> -1.44 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.81 </td> </tr> </tbody> </table> ]]  </br> -- .center[.eq_lrge[.blu_dk[ $$ `\begin{aligned} b_\text{wine} − b_\text{pudding} &= −1.11−(−0.08) \\ &= −1.03 \end{aligned}` $$ ]]] --- class: inverse background-image: url("media/as_snowy_trees_white_126530339.jpg") background-size: cover # Odds ratio ```r santa_full_mod |> broom::tidy(conf.int = TRUE, `exponentiate = TRUE`) |> knitr::kable(digits = 2, align = 'lcccccc') ``` .blubox[ <table class="table" style="font-size: 28px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> std.error </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 6.23 </td> <td style="text-align:center;"> 0.38 </td> <td style="text-align:center;"> 4.80 </td> <td style="text-align:center;"> 0.00 </td> <td style="text-align:center;"> 3.08 </td> <td style="text-align:center;"> 13.86 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> treatMulled wine </td> <td style="text-align:center;color: #2C5577 !important;"> 1.22 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.52 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.38 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.70 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.43 </td> <td style="text-align:center;color: #2C5577 !important;"> 3.37 </td> </tr> <tr> <td style="text-align:left;"> quantity </td> <td style="text-align:center;"> 0.92 </td> <td style="text-align:center;"> 0.17 </td> <td style="text-align:center;"> -0.48 </td> <td style="text-align:center;"> 0.63 </td> <td style="text-align:center;"> 0.66 </td> <td style="text-align:center;"> 1.28 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> treatMulled wine:quantity </td> <td style="text-align:center;color: #2C5577 !important;"> 0.36 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.23 </td> <td style="text-align:center;color: #2C5577 !important;"> -4.45 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.00 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.23 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.56 </td> </tr> </tbody> </table> ] --- class: inverse background-image: url("media/as_snowy_trees_white_126530339.jpg") background-size: cover </br> </br> .pull-left[ ```r santa_tib |> filter(treat == "Pudding") |> glm(delivered ~ quantity, data = _, family = binomial()) ``` .blubox[ <table class="table" style="font-size: 16px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 1.83 </td> <td style="text-align:center;"> 4.80 </td> <td style="text-align:center;"> 0.00 </td> <td style="text-align:center;"> 1.13 </td> <td style="text-align:center;"> 2.63 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> quantity </td> <td style="text-align:center;color: #2C5577 !important;"> -0.08 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.48 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.63 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.41 </td> <td style="text-align:center;color: #2C5577 !important;"> 0.25 </td> </tr> </tbody> </table> ]]  .pull-right[ ```r santa_tib |> filter(treat == "Mulled wine") |> glm(delivered ~ quantity, data = _, family = binomial()) ``` .blubox[ <table class="table" style="font-size: 16px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;color: #2C5577 !important;"> term </th> <th style="text-align:center;color: #2C5577 !important;"> estimate </th> <th style="text-align:center;color: #2C5577 !important;"> statistic </th> <th style="text-align:center;color: #2C5577 !important;"> p.value </th> <th style="text-align:center;color: #2C5577 !important;"> conf.low </th> <th style="text-align:center;color: #2C5577 !important;"> conf.high </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:center;"> 2.03 </td> <td style="text-align:center;"> 5.73 </td> <td style="text-align:center;"> 0 </td> <td style="text-align:center;"> 1.37 </td> <td style="text-align:center;"> 2.76 </td> </tr> <tr> <td style="text-align:left;color: #2C5577 !important;"> quantity </td> <td style="text-align:center;color: #2C5577 !important;"> -1.11 </td> <td style="text-align:center;color: #2C5577 !important;"> -6.99 </td> <td style="text-align:center;color: #2C5577 !important;"> 0 </td> <td style="text-align:center;color: #2C5577 !important;"> -1.44 </td> <td style="text-align:center;color: #2C5577 !important;"> -0.81 </td> </tr> </tbody> </table> ]]  </br> .center[.eq_lrge[.blu_dk[ $$ `\begin{aligned} b_\text{wine} − b_\text{pudding} &= −1.11−(−0.08) \\ &= −1.03 \end{aligned}` $$ ]]] -- .center[.eq_lrge[.blu_dk[ $$ `\begin{aligned} \exp(b)_\text{difference} &= e^{−1.03} \\ &= 0.36 \end{aligned}` $$ ]]] --- class: inverse, center, middle background-image: url("media/as_snowman_wave_303329070.jpg") background-size: cover exclude: ![:isSV] --- class: inverse, center, middle background-image: url("media/as_snowman_wave_303329070.jpg") background-size: cover <!-- -->   --- class: inverse, center, middle background-image: url("media/as_snowman_wave_303329070.jpg") background-size: cover exclude: true <!-- --> --- class: inverse background-image: url("media/as_blu_snowman_389261412.jpg") background-size: cover exclude: true # Conclusions .pull-left[ ### It’s OK for elves to eat puddings ### It’s not OK for elves to drink more than 1 glass of mulled wine! ] --- class: inverse background-image: url("media/as_log_cabin_184043999.jpg") background-size: cover exclude: true .left[.blubox[ You’d better watch out, you might want to cry </br> You’d better beware, I’m telling you why </br> Logistic regression is coming to town Santa’s checking the output, he’s checking it twice </br> To try to predict, who’s naughty or nice </br> Logistic regression is coming to town He’s got a binary outcome. Residuals aren’t normal </br> But with a logit link function, everything is cool </br> So, there’s nothing to fear. There’s no need to cry </br> It’s like every other model we’ve tried </br> Logistic regression is coming to town The effect of predictors, is easy to see </br> You just to need to check the exponent of *b* </br> Logistic regression is coming to town But design cells can be empty, that’s incomplete information </br> And if prediction is perfect then you have complete separation </br> But there’s nothing to fear. There’s no need to cry </br> It’s like every other, model we’ve tried </br> Logistic regression is coming to town ]] --- background-image: none background-color: #000000 class: no-scribble exclude: ![:isSV] <video width="100%" height="100%" controls id="my_video"> <source src="media/xmas_santa_03.mp4" type="video/mp4"> </video> ??? --- background-image: none background-color: #000000 class: no-scribble exclude: ![:isSV] <video width="100%" height="100%" controls id="my_video"> <source src="media/i_wish_it_could_be_christmas.mp4" type="video/mp4"> </video> ---