class: center, middle, inverse, title-slide # Discrete Choice ### AECN 396/896-002 --- class: middle <style type="text/css"> @media print { .has-continuation { display: block !important; } } .remark-slide-content.hljs-github h1 { margin-top: 5px; margin-bottom: 25px; } .remark-slide-content.hljs-github { padding-top: 10px; padding-left: 30px; padding-right: 30px; } .panel-tabs { <!-- color: #062A00; --> color: #841F27; margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-bottom: 0px; } .panel-tab { margin-top: 0px; margin-bottom: 0px; margin-left: 3px; margin-right: 3px; padding-top: 0px; padding-bottom: 0px; } .panelset .panel-tabs .panel-tab { min-height: 40px; } .remark-slide th { border-bottom: 1px solid #ddd; } .remark-slide thead { border-bottom: 0px; } .gt_footnote { padding: 2px; } .remark-slide table { border-collapse: collapse; } .remark-slide tbody { border-bottom: 2px solid #666; } .important { background-color: lightpink; border: 2px solid blue; font-weight: bold; } .remark-code { display: block; overflow-x: auto; padding: .5em; background: #ffe7e7; } .hljs-github .hljs { background: #f2f2fd; } .remark-inline-code { padding-top: 0px; padding-bottom: 0px; background-color: #e6e6e6; } .r.hljs.remark-code.remark-inline-code{ font-size: 0.9em } .left-full { width: 80%; height: 92%; float: left; } .left-code { width: 38%; height: 92%; float: left; } .right-plot { width: 60%; float: right; padding-left: 1%; } .left5 { width: 49%; height: 92%; float: left; } .right5 { width: 49%; float: right; padding-left: 1%; } .left3 { width: 29%; height: 92%; float: left; } .right7 { width: 69%; float: right; padding-left: 1%; } .left4 { width: 38%; height: 92%; float: left; } .right6 { width: 60%; float: right; padding-left: 1%; } ul li{ margin: 7px; } ul, li{ margin-left: 15px; padding-left: 0px; } ol li{ margin: 7px; } ol, li{ margin-left: 15px; padding-left: 0px; } </style> <style type="text/css"> .content-box { box-sizing: border-box; background-color: #e2e2e2; } .content-box-blue, .content-box-gray, .content-box-grey, .content-box-army, .content-box-green, .content-box-purple, .content-box-red, .content-box-yellow { box-sizing: border-box; border-radius: 5px; margin: 0 0 10px; overflow: hidden; padding: 0px 5px 0px 5px; width: 100%; } .content-box-blue { background-color: #F0F8FF; } .content-box-gray { background-color: #e2e2e2; } .content-box-grey { background-color: #F5F5F5; } .content-box-army { background-color: #737a36; } .content-box-green { background-color: #d9edc2; } .content-box-purple { background-color: #e2e2f9; } .content-box-red { background-color: #ffcccc; } .content-box-yellow { background-color: #fef5c4; } .content-box-blue .remark-inline-code, .content-box-blue .remark-inline-code, .content-box-gray .remark-inline-code, .content-box-grey .remark-inline-code, .content-box-army .remark-inline-code, .content-box-green .remark-inline-code, .content-box-purple .remark-inline-code, .content-box-red .remark-inline-code, .content-box-yellow .remark-inline-code { background: none; } .full-width { display: flex; width: 100%; flex: 1 1 auto; } </style> <style type="text/css"> blockquote, .blockquote { display: block; margin-top: 0.1em; margin-bottom: 0.2em; margin-left: 5px; margin-right: 5px; border-left: solid 10px #0148A4; border-top: solid 2px #0148A4; border-bottom: solid 2px #0148A4; border-right: solid 2px #0148A4; box-shadow: 0 0 6px rgba(0,0,0,0.5); /* background-color: #e64626; */ color: #e64626; padding: 0.5em; -moz-border-radius: 5px; -webkit-border-radius: 5px; } .blockquote p { margin-top: 0px; margin-bottom: 5px; } .blockquote > h1:first-of-type { margin-top: 0px; margin-bottom: 5px; } .blockquote > h2:first-of-type { margin-top: 0px; margin-bottom: 5px; } .blockquote > h3:first-of-type { margin-top: 0px; margin-bottom: 5px; } .blockquote > h4:first-of-type { margin-top: 0px; margin-bottom: 5px; } .text-shadow { text-shadow: 0 0 4px #424242; } </style> <style type="text/css"> /****************** * Slide scrolling * (non-functional) * not sure if it is a good idea anyway slides > slide { overflow: scroll; padding: 5px 40px; } .scrollable-slide .remark-slide { height: 400px; overflow: scroll !important; } ******************/ .scroll-box-8 { height:8em; overflow-y: scroll; } .scroll-box-10 { height:10em; overflow-y: scroll; } .scroll-box-12 { height:12em; overflow-y: scroll; } .scroll-box-14 { height:14em; overflow-y: scroll; } .scroll-box-16 { height:16em; overflow-y: scroll; } .scroll-box-18 { height:18em; overflow-y: scroll; } .scroll-box-20 { height:20em; overflow-y: scroll; } .scroll-box-24 { height:24em; overflow-y: scroll; } .scroll-box-30 { height:30em; overflow-y: scroll; } .scroll-output { height: 90%; overflow-y: scroll; } </style> # Discrete Choice Analysis + Focus on understanding choices that are discrete (not continuous) * Whether you own a car or not (binary choice) * Whether you use an iPhone, Android, or other types of cell phones (Multinomial choice) * Which recreation sites you visit this winter (multinomial) + Linear models we have seen are often not appropriate --- class: middle # Binary Response Model .content-box-green[**Binary response**] `\(y = 0 \;\; \mbox{(if you do not own a car)}\)` `\(y = 1 \;\; \mbox{(if you own at least one car)}\)` <br> .content-box-green[**Question we would like to answer**] How do variables `\(x_1,\dots,x_k\)` affect the status of `\(y\)` (the choice of whether to own at least one car or not)? --- class: middle .content-box-green[**Binary response**] We try to model the .red[probability] of `\(y = 1\)` (own at least one car) `\(Pr(y=1|x_1,\dots,x_k) = f(x_1,\dots,x_k)\)` as a function of independent variables. --- class: middle .content-box-green[**Linear Probability Model**] `\(Pr(y=1|x_1,\dots,x_k) = \beta_0+\beta_1 x_1+\dots+\beta_k x_k\)` -- <br> .content-box-green[**Drawback**] There is no guarantee that the predicted probability is bounded within [0, 1]. --- class: middle .content-box-green[**How about this?**] `\(Pr(y=1|x_1,\dots,x_k) = G(\beta_0+\beta_1 x_1+\dots+\beta_k x_k)\)` where `\(0<G(z)<1\)` for all real numbers `\(z\)` --- class: middle Different choices of `\(G()\)` lead to different models. .content-box-green[**Logit model**] `\(G(z) = exp(z)/[1+exp(z)] = \frac{e^z}{1+e^z}\)` where `\(z = \beta_0+\beta_1 x_1+\dots+\beta_k x_k\)` <br> .content-box-green[**Probit model**] `\(G(z) = \Phi(z)\)` where `\(\Phi(z)\)` is the standard normal cumulative distribution function --- class: middle This what `\(G()\)` looks like for logit and probit. <img src="data:image/png;base64,#discrete_choice_x_files/figure-html/unnamed-chunk-3-1.png" width="70%" style="display: block; margin: auto;" /> --- class: middle `\(Pr(y=1|x_1,\dots,x_k) = G(\beta_0+\beta_1 x_1+\dots+\beta_k x_k)\)` + What do `\(\beta\)`s measure? + How do we interpret them? --- class: middle `\(\mbox{Before:} x_1=0,\dots,x_k=0 \Rightarrow z=\beta_0\)` `\(\mbox{After:}\;\; x_1=1 \;\;\mbox{and}\;\; x_2=0,\dots,x_k=0 \Rightarrow z=\beta_0+\beta_1\)` <img src="data:image/png;base64,#discrete_choice_x_files/figure-html/unnamed-chunk-5-1.png" width="70%" style="display: block; margin: auto;" /> --- class: middle <img src="data:image/png;base64,#discrete_choice_x_files/figure-html/unnamed-chunk-6-1.png" width="60%" style="display: block; margin: auto;" /> + `\(\beta\)`s measure how far you move along the x-axis + `\(\beta\)`s does not directly measure how independent variables influence the probability of `\(y=1\)` --- class: middle To understand the marginal impact of `\(x_k\)` on `\(Prob(y=1)\)` (how a change in `\(x_k\)` affects the likelihood of owning a car), you need to do a bit of math. <br> .content-box-green[**Model**] `\(Pr(y=1|x_1,\dots,x_k) = G(z)\)` `\(z = \beta_0+\beta_1 x_1+\dots+\beta_k x_k\)` <br> .content-box-green[**marginal impact**] Differentiating both sides with respect to `\(x_k\)`, `\(\frac{\partial Pr(y=1|x_1,\dots,x_k)}{\partial x_k} = G'(z)\times \frac{\partial z}{\partial x_k}\)` `\(\qquad\qquad\qquad\:\: = G'(z)\times \beta_k\)` --- class: middle .content-box-green[**marginal impact**] Differentiating both sides with respect to `\(x_k\)`, `\(\frac{\partial Pr(y=1|x_1,\dots,x_k)}{\partial x_k} = G'(z)\times \frac{\partial z}{\partial x_k}\)` `\(\qquad\qquad\qquad\:\: = G'(z)\times \beta_k\)` <br> .content-box-green[**Notes**] + The marginal impact of an independent variable depends on the values of all the independent variables: `\(G(\beta_0+\beta_1 x_1+\dots+\beta_k x_k)\)` + Since `\(G'()\)` is always positive, the sign of the marginal impact of an independent variable on `\(Prob(y=1)\)` is always the same as the sign of its coefficient --- class: middle .content-box-green[**Estimation of Binary Choice Models**] + Linear models: OLS + Binary choice models: .blue[Maximum Likelihood Estimation (MLE)] --- class: middle .content-box-green[**OLS**] Find parameters that makes the sum of residuals squared the smallest <br> .content-box-green[**MLE (very loosely put)**] Find parameters `\((\beta\)`s) that makes what we observed (collection of binary decisions made by different individuals) most likely (.blue[Maximum Likelihood]) --- class: middle .content-box-green[**Observed decisions made by two individuals**] + Individual 1: `\(y=1\)` (own at least one car) + Individual 2: `\(y=0\)` (does not own a car) -- <br> .content-box-green[**Probability of individual decisions**] + `\(\mbox{Individual 1}: Prob(y_1=1|\mathbf{x_1})= G(z_1)\)` + `\(\mbox{Individual 2}: Prob(y_2=0|\mathbf{x_2})= 1-G(z_2)\)` where + `\(\mathbf{x_i}\)` is a collection of independent variables for individual `\(i\)` `\((x_{1,i}, \dots, x_{k,i})\)`. + `\(z_i = \beta_0+\beta_1 x_{1,i}+\dots+\beta_k x_{k,i}\)` -- <br> .content-box-green[**Probability of a collection of decisions**] The probability that we observe a .blue[collection of choices] made by them (if their decisions are independent) `\(Prob(y_1=1|\mathbf{x_1})\times Prob(y_2=0|\mathbf{x_2}) = G(z_1)\times [1-G(z_2)]\)` which we call .blue[likelihood function]. --- class: middle .content-box-green[**Probability of a collection of decisions**] The probability that we observe a .blue[collection of choices] made by them (if their decisions are independent) `\(Prob(y_1=1|\mathbf{x_1})\times Prob(y_2=0|\mathbf{x_2}) = G(z_1)\times [1-G(z_2)]\)` which we call .blue[likelihood function]. -- <br> .content-box-green[**MLE**] `\(Max_{\beta_1,\dots,\beta_k}\;\; G(z_1)\times [1-G(z_2)]\)` --- class: middle .content-box-green[**MLE of Binary Choice Model in General**] Maximize the likelihood function: `\(Max_{\beta_1,\dots,\beta_k}\;\; L\)` where `\(L=\Pi_{i=1}^n \Big[y_i\times G(z_i)+(1-y_i)\times(1-G(z_i))\Big]\)` is the likelihood function. <br> .content-box-green[**Log-likelihood function**] `\(LL = log\Big(\Pi_{i=1}^n \Big[y_i\times G(z_i)+(1-y_i)\times(1-G(z_i))\Big]\Big)\)` `\(\qquad = \sum_{i=1}^n log\Big(y_i\times G(z_i)+(1-y_i)\times(1-G(z_i))\Big)\)` <br> .content-box-green[**MLE with `\(LL\)`**] `\(argmax_{\beta_1,\dots,\beta_k}\;\; L \equiv argmax_{\beta_1,\dots,\beta_k}\;\; LL\)` --- class: middle # Implementation in R with an example Participation of females in labor force: `\(Pr(inlf=1|\mathbf{x})= G(z)\)` where `\(z = \beta_0+\beta_1 nwifeinc+ \beta_2 educ+ \beta_3 exper\)` `\(\quad\;\; + \beta_4 exper^2 + \beta_5 age + \beta_6 kidslt6 + \beta_7 kidsge6\)` + `\(inlf\)`: 1 if in labor force in 1975, 0 otherwise + `\(nwifeinc\)`: earning as a family if she does not work + `\(kidslt6\)`: \# of kids less than 6 years old + `\(kidsge6\)`: \# of kids who are 6-18 year old --- class: middle ```r #--- import the data ---# data <- read.dta13("MROZ.dta") %>% mutate(exper2 = exper^2) #--- take a look ---# dplyr::select(data, inlf, nwifeinc, kidslt6, kidsge6, educ) %>% head() ``` ``` ## inlf nwifeinc kidslt6 kidsge6 educ ## 1 1 10.910060 1 0 12 ## 2 1 19.499981 0 2 12 ## 3 1 12.039910 1 3 12 ## 4 1 6.799996 0 3 12 ## 5 1 20.100058 1 2 14 ## 6 1 9.859054 0 0 12 ``` --- class: middle ``` ## inlf nwifeinc kidslt6 kidsge6 educ ## 1 1 10.910060 1 0 12 ## 2 1 19.499981 0 2 12 ## 3 1 12.039910 1 3 12 ## 4 1 6.799996 0 3 12 ## 5 1 20.100058 1 2 14 ## 6 1 9.859054 0 0 12 ``` For individual 1 (row 1 of the data), `\(z_1 = \beta_0+\beta_1 10.91+ \beta_2 12+ \beta_3 14 + \beta_4 196 + \beta_5 32 + \beta_6 1 + \beta_7 0\)` The probability that individual 1 would make the decision he/she made given `\(\beta\)`s is: `\(G(z_1)\)` (a function of `\(\beta\)`s) --- class: middle ``` ## inlf nwifeinc kidslt6 kidsge6 educ ## 748 0 5.330 0 2 12 ## 749 0 28.200 0 2 13 ## 750 0 10.000 2 3 12 ## 751 0 9.952 0 0 12 ## 752 0 24.984 0 0 12 ## 753 0 28.363 0 3 9 ``` For individual 753 (row 753 of the data), `\(z_{753} = \beta_0+\beta_1 28.36+ \beta_2 9+ \beta_3 12 + \beta_4 144 + \beta_5 39 + \beta_6 0 + \beta_7 3\)` The probability that individual 753 would make the decision he/she made given `\(\beta\)`s is: `\(1 - G(z_{753})\)` (a function of `\(\beta\)`s) --- class: middle Multiply all the probabilities of observed choices given `\(\beta\)`s, `\(L = G(z_1) \times G(z_2) \times \dots [1-G(z_753)]\)` -- `\(LL = log\Big(G(z_1) \times G(z_2) \times \dots [1-G(z_753)]\Big)\)` -- Solve the following problems to estimate `\(\beta\)`s: `\(Max_{\beta_1, \dots, \beta_7} \quad LL\)` --- class: middle .content-box-green[**Estimating binary choice model using `\(R\)`**] You can use the `glm()` function (no new packages installation necessary) when using cross-sectional data + `glm` refers to Generalized Linear Model, which encompass linear models we have been using + you specify the `family` option to tell what kind of model you are estimating --- class: middle .content-box-green[**Probit model estimation**] ```r probit_lf <- glm( #--- formula ---# inlf ~ nwifeinc + educ + exper + exper2 + age + kidslt6 + kidsge6, #--- data ---# data = data, #--- models ---# family = binomial(link = "probit") ) ``` <br> .content-box-green[**family option**] + `binomial()`: tells R that your dependent variable is binary + `link = "probit"`: tells R that you want to use the cumulative distribution function of the standard normal distribution as `\(G()\)` in `\(Prob(y=1|\mathbf{x})=G(z)\)` --- class: middle .left5[ ```r msummary( probit_lf, stars = TRUE, gof_omit = "IC|F", output = "flextable" ) %>% fontsize( size = 9, part = "all" ) %>% autofit() ``` ] .right5[ <template id="64d83280-6340-4ce7-a59f-28932a3a49e8"><style> .tabwid table{ border-spacing:0px !important; border-collapse:collapse; line-height:1; margin-left:auto; margin-right:auto; border-width: 0; display: table; margin-top: 1.275em; margin-bottom: 1.275em; border-color: transparent; } .tabwid_left table{ margin-left:0; } .tabwid_right table{ margin-right:0; } .tabwid td { padding: 0; } .tabwid a { text-decoration: none; } .tabwid thead { background-color: transparent; } .tabwid tfoot { background-color: transparent; } .tabwid table tr { background-color: transparent; } </style><div class="tabwid"><style>.cl-42fe984e{}.cl-42e767f0{font-family:'Helvetica';font-size:9pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-42e780be{margin:0;text-align:left;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-42e7ccea{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7ccf4{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7ccf5{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7ccfe{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd08{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd09{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd12{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd1c{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd1d{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd26{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd30{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd31{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd3a{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd44{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd45{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd4e{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd58{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cd59{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cdc6{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cdd0{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cdda{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cddb{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cde4{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cdee{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cdef{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(255, 255, 255, 0.00);border-top: 0 solid rgba(255, 255, 255, 0.00);border-left: 0 solid rgba(255, 255, 255, 0.00);border-right: 0 solid rgba(255, 255, 255, 0.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7cdf8{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(255, 255, 255, 0.00);border-top: 0 solid rgba(255, 255, 255, 0.00);border-left: 0 solid rgba(255, 255, 255, 0.00);border-right: 0 solid rgba(255, 255, 255, 0.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7ce02{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 2pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-42e7ce0c{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 2pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-42fe984e'><thead><tr style="overflow-wrap:break-word;"><td class="cl-42e7ce02"><p class="cl-42e780be"><span class="cl-42e767f0"> </span></p></td><td class="cl-42e7ce0c"><p class="cl-42e780be"><span class="cl-42e767f0">Model 1</span></p></td></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccea"><p class="cl-42e780be"><span class="cl-42e767f0">(Intercept)</span></p></td><td class="cl-42e7ccf4"><p class="cl-42e780be"><span class="cl-42e767f0">0.270</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.508)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cd58"><p class="cl-42e780be"><span class="cl-42e767f0">nwifeinc</span></p></td><td class="cl-42e7cd59"><p class="cl-42e780be"><span class="cl-42e767f0">-0.012*</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.005)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cdd0"><p class="cl-42e780be"><span class="cl-42e767f0">educ</span></p></td><td class="cl-42e7cdc6"><p class="cl-42e780be"><span class="cl-42e767f0">0.131***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.025)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cddb"><p class="cl-42e780be"><span class="cl-42e767f0">exper</span></p></td><td class="cl-42e7cdda"><p class="cl-42e780be"><span class="cl-42e767f0">0.123***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.019)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cdee"><p class="cl-42e780be"><span class="cl-42e767f0">exper2</span></p></td><td class="cl-42e7cde4"><p class="cl-42e780be"><span class="cl-42e767f0">-0.002**</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.001)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cd08"><p class="cl-42e780be"><span class="cl-42e767f0">age</span></p></td><td class="cl-42e7cd09"><p class="cl-42e780be"><span class="cl-42e767f0">-0.053***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.008)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cd1c"><p class="cl-42e780be"><span class="cl-42e767f0">kidslt6</span></p></td><td class="cl-42e7cd12"><p class="cl-42e780be"><span class="cl-42e767f0">-0.868***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.118)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cd1d"><p class="cl-42e780be"><span class="cl-42e767f0">kidsge6</span></p></td><td class="cl-42e7cd26"><p class="cl-42e780be"><span class="cl-42e767f0">0.036</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7ccf5"><p class="cl-42e780be"><span class="cl-42e767f0"></span></p></td><td class="cl-42e7ccfe"><p class="cl-42e780be"><span class="cl-42e767f0">(0.044)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cd30"><p class="cl-42e780be"><span class="cl-42e767f0">Num.Obs.</span></p></td><td class="cl-42e7cd31"><p class="cl-42e780be"><span class="cl-42e767f0">753</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cd44"><p class="cl-42e780be"><span class="cl-42e767f0">Log.Lik.</span></p></td><td class="cl-42e7cd3a"><p class="cl-42e780be"><span class="cl-42e767f0">-401.302</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-42e7cd4e"><p class="cl-42e780be"><span class="cl-42e767f0">RMSE</span></p></td><td class="cl-42e7cd45"><p class="cl-42e780be"><span class="cl-42e767f0">1.04</span></p></td></tr></tbody><tfoot><tr style="overflow-wrap:break-word;"><td colspan="2"class="cl-42e7cdf8"><p class="cl-42e780be"><span class="cl-42e767f0">+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001</span></p></td></tr></tfoot></table></div></template> <div class="flextable-shadow-host" id="9b8402d7-5716-4cd9-8f63-cae21076051c"></div> <script> var dest = document.getElementById("9b8402d7-5716-4cd9-8f63-cae21076051c"); var template = document.getElementById("64d83280-6340-4ce7-a59f-28932a3a49e8"); var caption = template.content.querySelector("caption"); if(caption) { caption.style.cssText = "display:block;text-align:center;"; var newcapt = document.createElement("p"); newcapt.appendChild(caption) dest.parentNode.insertBefore(newcapt, dest.previousSibling); } var fantome = dest.attachShadow({mode: 'open'}); var templateContent = template.content; fantome.appendChild(templateContent); </script> ] --- class: middle .content-box-green[**Logit model estimation**] ```r logit_lf <- glm( #--- formula ---# inlf ~ nwifeinc + educ + exper + exper2 + age + kidslt6 + kidsge6, #--- data ---# data = data, #--- models ---# family = binomial(link = "logit") ) ``` .content-box-green[**family option**] + `binomial()`: tells R that your dependent variable is binary + `link = "logit"`: tells `\(R\)` that you want to use `\(G(z) = \frac{e^z}{1+e^z}\)` in `\(Prob(y=1|\mathbf{x})=G(z)\)` --- class: middle .left5[ ```r msummary( logit_lf, stars = TRUE, gof_omit = "IC|F", output = "flextable" ) %>% fontsize( size = 9, part = "all" ) %>% autofit() ``` ] .right5[ <template id="98a31f52-7c8d-4d67-8e76-6329ce1e6017"><style> .tabwid table{ border-spacing:0px !important; border-collapse:collapse; line-height:1; margin-left:auto; margin-right:auto; border-width: 0; display: table; margin-top: 1.275em; margin-bottom: 1.275em; border-color: transparent; } .tabwid_left table{ margin-left:0; } .tabwid_right table{ margin-right:0; } .tabwid td { padding: 0; } .tabwid a { text-decoration: none; } .tabwid thead { background-color: transparent; } .tabwid tfoot { background-color: transparent; } .tabwid table tr { background-color: transparent; } </style><div class="tabwid"><style>.cl-6d6a361a{}.cl-6d6295f4{font-family:'Helvetica';font-size:9pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-6d62a53a{margin:0;text-align:left;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-6d62d654{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d668{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d669{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d672{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d67c{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6ae{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6b8{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6c2{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6c3{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6c4{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6cc{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6d6{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6f4{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d6fe{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d71c{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d726{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d727{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d728{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d744{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d74e{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d758{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d759{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d762{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d76c{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d76d{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(255, 255, 255, 0.00);border-top: 0 solid rgba(255, 255, 255, 0.00);border-left: 0 solid rgba(255, 255, 255, 0.00);border-right: 0 solid rgba(255, 255, 255, 0.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d776{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(255, 255, 255, 0.00);border-top: 0 solid rgba(255, 255, 255, 0.00);border-left: 0 solid rgba(255, 255, 255, 0.00);border-right: 0 solid rgba(255, 255, 255, 0.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d794{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 2pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-6d62d795{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 2pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-6d6a361a'><thead><tr style="overflow-wrap:break-word;"><td class="cl-6d62d794"><p class="cl-6d62a53a"><span class="cl-6d6295f4"> </span></p></td><td class="cl-6d62d795"><p class="cl-6d62a53a"><span class="cl-6d6295f4">Model 1</span></p></td></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-6d62d654"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(Intercept)</span></p></td><td class="cl-6d62d668"><p class="cl-6d62a53a"><span class="cl-6d6295f4">0.425</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.860)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d727"><p class="cl-6d62a53a"><span class="cl-6d6295f4">nwifeinc</span></p></td><td class="cl-6d62d728"><p class="cl-6d62a53a"><span class="cl-6d6295f4">-0.021*</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.008)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d74e"><p class="cl-6d62a53a"><span class="cl-6d6295f4">educ</span></p></td><td class="cl-6d62d744"><p class="cl-6d62a53a"><span class="cl-6d6295f4">0.221***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.043)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d759"><p class="cl-6d62a53a"><span class="cl-6d6295f4">exper</span></p></td><td class="cl-6d62d758"><p class="cl-6d62a53a"><span class="cl-6d6295f4">0.206***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.032)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d76c"><p class="cl-6d62a53a"><span class="cl-6d6295f4">exper2</span></p></td><td class="cl-6d62d762"><p class="cl-6d62a53a"><span class="cl-6d6295f4">-0.003**</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.001)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d67c"><p class="cl-6d62a53a"><span class="cl-6d6295f4">age</span></p></td><td class="cl-6d62d6ae"><p class="cl-6d62a53a"><span class="cl-6d6295f4">-0.088***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.015)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d6c2"><p class="cl-6d62a53a"><span class="cl-6d6295f4">kidslt6</span></p></td><td class="cl-6d62d6b8"><p class="cl-6d62a53a"><span class="cl-6d6295f4">-1.443***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.204)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d6c3"><p class="cl-6d62a53a"><span class="cl-6d6295f4">kidsge6</span></p></td><td class="cl-6d62d6c4"><p class="cl-6d62a53a"><span class="cl-6d6295f4">0.060</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d669"><p class="cl-6d62a53a"><span class="cl-6d6295f4"></span></p></td><td class="cl-6d62d672"><p class="cl-6d62a53a"><span class="cl-6d6295f4">(0.075)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d6cc"><p class="cl-6d62a53a"><span class="cl-6d6295f4">Num.Obs.</span></p></td><td class="cl-6d62d6d6"><p class="cl-6d62a53a"><span class="cl-6d6295f4">753</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d6fe"><p class="cl-6d62a53a"><span class="cl-6d6295f4">Log.Lik.</span></p></td><td class="cl-6d62d6f4"><p class="cl-6d62a53a"><span class="cl-6d6295f4">-401.765</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-6d62d726"><p class="cl-6d62a53a"><span class="cl-6d6295f4">RMSE</span></p></td><td class="cl-6d62d71c"><p class="cl-6d62a53a"><span class="cl-6d6295f4">1.04</span></p></td></tr></tbody><tfoot><tr style="overflow-wrap:break-word;"><td colspan="2"class="cl-6d62d776"><p class="cl-6d62a53a"><span class="cl-6d6295f4">+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001</span></p></td></tr></tfoot></table></div></template> <div class="flextable-shadow-host" id="46c1666a-01a5-45f3-86ed-a0336722a070"></div> <script> var dest = document.getElementById("46c1666a-01a5-45f3-86ed-a0336722a070"); var template = document.getElementById("98a31f52-7c8d-4d67-8e76-6329ce1e6017"); var caption = template.content.querySelector("caption"); if(caption) { caption.style.cssText = "display:block;text-align:center;"; var newcapt = document.createElement("p"); newcapt.appendChild(caption) dest.parentNode.insertBefore(newcapt, dest.previousSibling); } var fantome = dest.attachShadow({mode: 'open'}); var templateContent = template.content; fantome.appendChild(templateContent); </script> ] --- class: middle .content-box-green[**Important**] + You <span style = "color: red;"> cannot </span> directly compare the coefficient on the same variable from probit and logit! The fact that the coefficient on `educ` is higher from the logit model does not mean the logit model is suggesting `educ` is more influential than the probit model suggests. They are on different scales. --- class: middle # Post-estimation operations and diagnostics --- class: middle .content-box-green[**Log-likelihood (fitted)**] `\(LL =\sum_{i=1}^n log\Big(y_i\times G(\hat{z}_i)+(1-y_i)\times(1-G(\hat{z}_i))\Big)\)` + `\(\hat{z}_i=\hat{\beta}_0+\hat{\beta}_1 x_1+\dots+\hat{\beta}_k x_k\)` + `\(G(\hat{z}_i)\)` is the fitted value of `\(Prob(y=1|\mathbf{x})\)` <br> .content-box-green[**Example**] + `\(G(\hat{z}_i) = 0.9\)`: predicted that individual `\(i\)` is very likely to own a car + `\(y_i = 0\)`: in reality, individual `\(i\)` does not own a car `\(\Rightarrow\)` `\(log\Big(0\times 0.9 +(1-0)\times(1-0.9)\Big) = log(0.1) = -2.3\)` --- class: middle .content-box-green[**Log-likelihood (fitted)**] `\(LL =\sum_{i=1}^n log\Big(y_i\times G(\hat{z}_i)+(1-y_i)\times(1-G(\hat{z}_i))\Big)\)` + `\(\hat{z}_i=\hat{\beta}_0+\hat{\beta}_1 x_1+\dots+\hat{\beta}_k x_k\)` + `\(G(\hat{z}_i)\)` is the fitted value of `\(Prob(y=1|\mathbf{x})\)` <br> .content-box-green[**Example**] + `\(G(\hat{z}_i) = 0.9\)`: predicted that individual `\(i\)` is very likely to own a car + `\(y_i = 1\)`: in reality, individual `\(i\)` indeed owns a car `\(\Rightarrow\)` `\(log\Big(1\times 0.9 +(1-1)\times(1-0.9)\Big) = log(0.9) = -0.11\)` --- class: middle .content-box-green[**Log-likelihood (fitted)**] So, the better your prediction (model fit) is, the .blue[the greater (less negative) LL is.] --- class: middle .content-box-green[**McFadden's**] pseudo- `\(R^2\)` A measure of how much better your model is compared to the model with only the intercept. `\(pseudo-R^2=1-LL/LL_0\)` where `\(LL_0\)` is the log-likelihood when you include only the intercept. --- class: middle .content-box-green[**R code**] ```r logit_lf_0 <- glm( inlf ~ 1, data = data, family = binomial(link = "logit") ) #--- extract LL using the logLik() function ---# (LL0 <- logLik(logit_lf_0)) ``` ``` ## 'log Lik.' -514.8732 (df=1) ``` ```r #--- extract LL using the logLik() function from your preferred model ---# (LL <- logLik(logit_lf)) ``` ``` ## 'log Lik.' -401.7652 (df=8) ``` ```r #--- pseudo R2 ---# 1 - LL / LL0 ``` ``` ## 'log Lik.' 0.2196814 (df=8) ``` --- class: middle .content-box-green[**Alternatively**] ```r #--- or more easily ---# 1 - logit_lf$deviance / logit_lf$null.deviance ``` ``` ## [1] 0.2196814 ``` ```r #--- what are deviances? ---# logit_lf$null.deviance # = -2*LL0 ``` ``` ## [1] 1029.746 ``` ```r logit_lf$deviance # = -2*LL ``` ``` ## [1] 803.5303 ``` + `null.deviance` `\(= -2\times LL_0\)` + `deviance` `\(= -2\times LL\)` --- class: middle .content-box-green[**Testing joint significance**] You can do Likelihood Ratio (LR) test: `\(LR = 2(LL_{unrestricted}-LL_{restricted}) \sim \chi^2_{df\_restrictions}\)` where `\(df\_restrictions\)` is the number of restrictions. <br> .content-box-green[**Note**] LR test is very similar conceptually to F-test. <br> .content-box-green[**Example**] + `\(H_0:\)` the coefficients on `\(exper\)`, `\(exper2\)`, and `\(age\)` are `\(0\)` + `\(H_1:\)` `\(H_0\)` is false --- class: middle ```r #--- unrestricted ---# logit_ur <- glm( inlf ~ nwifeinc + educ + exper + exper2 + age + kidslt6 + kidsge6, data = data, family = binomial(link = "logit") ) #--- restricted ---# logit_r <- glm( inlf ~ nwifeinc + educ + kidslt6 + kidsge6, data = data, family = binomial(link = "logit") ) #--- LR test using lrtest() from the lmtest package ---# library(lmtest) lrtest(logit_r, logit_ur) ``` ``` ## Likelihood ratio test ## ## Model 1: inlf ~ nwifeinc + educ + kidslt6 + kidsge6 ## Model 2: inlf ~ nwifeinc + educ + exper + exper2 + age + kidslt6 + kidsge6 ## #Df LogLik Df Chisq Pr(>Chisq) ## 1 5 -464.92 ## 2 8 -401.77 3 126.32 < 2.2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` --- class: middle # Prediction After estimating a binary choice model, you can easily predict the following two + `\(\hat{z}=\hat{\beta}_0+\hat{\beta}_1 x_1+\dots+\hat{\beta}_k x_k\)` + `\(\widehat{Prob(y=1|\mathbf{x})}=G(\hat{z})=G(\hat{\beta}_0+\hat{\beta}_1 x_1+\dots+\hat{\beta}_k x_k)\)` --- class: middle .content-box-green[**R code**] ```r #--- z hat ---# z <- predict(probit_lf, type = "link") head(z) ``` ``` ## 1 2 3 4 5 6 ## 0.5071349 0.6624576 0.5116317 0.7423429 0.1972781 0.8837878 ``` ```r #--- G(z) hat ---# Gz <- predict(probit_lf, type = "response") head(Gz) ``` ``` ## 1 2 3 4 5 6 ## 0.6939699 0.7461610 0.6955456 0.7710602 0.5781950 0.8115946 ``` --- class: middle # Marginal effect of an independent variable + Coefficient estimates across different models (probit and logit) are not meaningful because the same value of a coefficient estimate means different things + They are the estimates of `\(\beta\)`s, not the direct impact of the independent variables on the `\(Prob(y=1)\)` --- class: middle Marginal effect of an independent variable `\(\frac{\partial Pr(y=1|x_1,\dots,x_k)}{\partial x_k} = G'(z)\times \beta_k\)` + the marginal impact depends on the current levels of all the independent variables + we typically report one of the two types of marginal impacts * (becoming obsolete) the marginal impact .blue[at the mean] (average person): when all the independent variables take on their respective means * the average of the marginal impacts calculated for each of all the individuals observed --- class: middle .content-box-green[**Marginal impact at the mean**] `\(\frac{\partial Pr(y=1|x_1,\dots,\bar{x_k})}{\partial x_k} = G'(\beta_0+\beta_1 \bar{x_1}+\dots+\beta_k\bar{x_k})\times \beta_k\)` <br> .content-box-green[**Mean marginal impact (MME)**] `\(\sum_{i=1}^n \frac{\partial Pr(y_i=1|x_{i,1},\dots,x_{i,k})}{\partial x_k} = \sum_{i=1}^n G'(z_i)\times \beta_k\)` --- class: middle .content-box-green[**R codes to get MME**] $$ \begin{aligned} \hat{z}=\hat{\beta}_0+\hat{\beta}_1 x_1+\dots+\hat{\beta}_k x_k \end{aligned} $$ ```r #--- get z for all the individuals ---# z <- predict(probit_lf, type = "link") ``` -- $$ \begin{aligned} G'(\beta_0+\beta_1 \bar{x_1}+\dots+\beta_k\bar{x_k}) \end{aligned} $$ where `\(G(z)\)` is the cumulative distribution function for the standard normal distribution. ```r #--- get G'(z) ---# Gz_indiv <- dnorm(z) ``` -- $$ \begin{aligned} G'(\beta_0+\beta_1 \bar{x_1}+\dots+\beta_k\bar{x_k}) \end{aligned}\times \beta_k $$ ```r #--- mean marignal impact of eduction ---# mean(Gz_indiv) * probit_lf$coef["educ"] ``` ``` ## educ ## 0.03937009 ``` --- class: middle Fortunately, the `margins` package provides you with a more convenient way of calculating MMEs. ```r library(margins) #--- calculate MME based on the probit estimation ---# mme_lf <- margins(probit_lf, type = "response") #--- get the summary ---# summary(mme_lf) ``` ``` ## factor AME SE z p lower upper ## age -0.0159 0.0024 -6.7392 0.0000 -0.0205 -0.0113 ## educ 0.0394 0.0073 5.4186 0.0000 0.0251 0.0536 ## exper 0.0371 0.0052 7.1779 0.0000 0.0270 0.0472 ## exper2 -0.0006 0.0002 -3.2050 0.0014 -0.0009 -0.0002 ## kidsge6 0.0108 0.0132 0.8189 0.4129 -0.0151 0.0367 ## kidslt6 -0.2612 0.0319 -8.1860 0.0000 -0.3237 -0.1986 ## nwifeinc -0.0036 0.0015 -2.4604 0.0139 -0.0065 -0.0007 ``` For example, the average marginal effect of `educ` on the probability of "in labor force" is 0.039. That is if education increases by one year, then the probability of being in labor force increases by 4%. --- class: inverse, center, middle # Multinomial Choice Model <html><div style='float:left'></div><hr color='#EB811B' size=1px width=1000px></html> --- class: middle # Multinomial Choice Instead of two options, you are picking one option out of more than two options + which carrier? * Verizon * Sprint * AT\&T * T-mobile + which transportation means to commute? * drive * Uber * bus * train * bike --- class: middle # Multinomial logit model The most popular model to analyze multinomial choice + environmental evaluation + tranposrtation + marketing --- class: middle # Understanding multinomial logit model .content-box-green[**Choice of train route options**] + 10 euros, 30 minutes travel time, one change + 20 euros, 20 minutes travel time, one change + 22 euros, 22 minutes travel time, no change -- <br> .content-box-green[**Associated utility**] + `\(V_1=\alpha_1 + \beta 10 + \gamma 30 + \rho 1 + v_1\)` + `\(V_2=\alpha_2 + \beta 20 + \gamma 20 + \rho 1 + v_2\)` + `\(V_3=\alpha_3 + \beta 22 + \gamma 22 + \rho 0 + v_3\)` --- class: middle .content-box-green[**Choice probability**] Logit model assumes that the probability of choosing an alternative is the following: + `\(P_1=\frac{e^{V_1}}{e^{V_1}+e^{V_2}+e^{V_3}}\)` + `\(P_2=\frac{e^{V_2}}{e^{V_1}+e^{V_2}+e^{V_3}}\)` + `\(P_3=\frac{e^{V_3}}{e^{V_1}+e^{V_2}+e^{V_3}}\)` -- .content-box-green[**Notes**] + `\(0<P_j<1\)`, `\(^\forall j=1,2,3\)` + `\(\sum_{j=1}^3=1\)` --- class: middle .content-box-green[**Modeled probability of choices**] Modeled probability of observing individual `\(i\)` choosing the option `\(i\)` chose `\(P_i = \Pi_{j=1}^3 y_{i,j}\times P_j\)` where `\(y_{i,j}=1\)` if `\(i\)` chose `\(j\)`, 0 otherwise. <br> .content-box-green[**Example**] `\(y_{i,1} = 0, \;\;y_{i,2} = 1,\;\;y_{i,3} = 0\)` `\(P_i = \Pi_{j=1}^3 y_{i,j}\times P_j = 0\times P_1 + 1\times P_2 + 0\times P_3\)` --- class: middle The probability of observing a series of chocies made by all the subjects is `\(LL = \Pi_{i=1}^n P_i = \Pi_{i=1}^n \Pi_{j=1}^3 y_{i,j}\times P_j\)` if choices made by the subjects are independent with each other. -- <br> .content-box-green[**MLE**] `\(Max_{\beta,\gamma,\rho}\;\; log(LL)\)` --- class: middle # Interpretation of the coefficients .content-box-green[**Model in general**] `\(V_{i,j} = \alpha_j + \beta_1 x_{1,i,j} + \dots + \beta_k x_{k,i,j}\)` `\(P_{i,j} = \frac{e^{V_{i,j}}}{\sum_{k=1}^J e^{V_{i,k}}}\)` -- <br> .content-box-green[**Interpretation of the coefficients**] `\(\frac{\partial P_{i,j}}{\partial x_{k,i,j}} = \beta_k P_{i,j}(1-P_{i,j})\)` + A marginal change in `\(k\)`th variable for alternative `\(j\)` would change the probability of choosing alternative `\(j\)` by `\(\beta_k P_{i,j}(1-P_{i,j})\)` + the sign of the impact is the same as the sign of the coefficient --- class: middle .content-box-green[**Implementation in `\(R\)`**] You can use `\(mlogit\)` package to estimate multinomial logit models: + format your data in a specific manner + convert your data using `\(mlogit.data()\)` + estimate the model using `\(mlogit()\)` --- class: middle ```r #--- library ---# library(mlogit) #--- get the travel mode data from the mlogit package ---# data("TravelMode", package = "AER") #--- take a look at the data ---# # first 10 rows head(TravelMode, 10) ``` ``` ## individual mode choice wait vcost travel gcost income size ## 1 1 air no 69 59 100 70 35 1 ## 2 1 train no 34 31 372 71 35 1 ## 3 1 bus no 35 25 417 70 35 1 ## 4 1 car yes 0 10 180 30 35 1 ## 5 2 air no 64 58 68 68 30 2 ## 6 2 train no 44 31 354 84 30 2 ## 7 2 bus no 53 25 399 85 30 2 ## 8 2 car yes 0 11 255 50 30 2 ## 9 3 air no 69 115 125 129 40 1 ## 10 3 train no 34 98 892 195 40 1 ``` --- class: middle .content-box-green[**R code: data preparation**] ```r #--- convert the data ---# TM <- mlogit.data(TravelMode, shape = "long", # what format is the data in? choice = "choice", # name of the variable that indicates choice made chid.var = "individual", # name of the variable that indicates who made choices alt.var = "mode" # the name of the variable that indicates options ) ``` --- class: middle ```r #--- take a look at the data ---# # first 10 rows head(TM, 10) ``` .scroll-box-16[ ``` ## ~~~~~~~ ## first 10 observations out of 840 ## ~~~~~~~ ## individual mode choice wait vcost travel gcost income size idx ## 1 1 air FALSE 69 59 100 70 35 1 1:air ## 2 1 train FALSE 34 31 372 71 35 1 1:rain ## 3 1 bus FALSE 35 25 417 70 35 1 1:bus ## 4 1 car TRUE 0 10 180 30 35 1 1:car ## 5 2 air FALSE 64 58 68 68 30 2 2:air ## 6 2 train FALSE 44 31 354 84 30 2 2:rain ## 7 2 bus FALSE 53 25 399 85 30 2 2:bus ## 8 2 car TRUE 0 11 255 50 30 2 2:car ## 9 3 air FALSE 69 115 125 129 40 1 3:air ## 10 3 train FALSE 34 98 892 195 40 1 3:rain ## ## ~~~ indexes ~~~~ ## chid alt ## 1 1 air ## 2 1 train ## 3 1 bus ## 4 1 car ## 5 2 air ## 6 2 train ## 7 2 bus ## 8 2 car ## 9 3 air ## 10 3 train ## indexes: 1, 2 ``` ] --- class: middle ```r #--- estimate ---# ml_reg <- mlogit(choice ~ wait + vcost + travel, data = TM) ``` --- class: middle .scroll-box-24[ ```r summary(ml_reg) ``` ``` ## ## Call: ## mlogit(formula = choice ~ wait + vcost + travel, data = TM, method = "nr") ## ## Frequencies of alternatives:choice ## air train bus car ## 0.27619 0.30000 0.14286 0.28095 ## ## nr method ## 5 iterations, 0h:0m:0s ## g'(-H)^-1g = 0.000192 ## successive function values within tolerance limits ## ## Coefficients : ## Estimate Std. Error z-value Pr(>|z|) ## (Intercept):train -0.78666667 0.60260733 -1.3054 0.19174 ## (Intercept):bus -1.43363372 0.68071345 -2.1061 0.03520 * ## (Intercept):car -4.73985647 0.86753178 -5.4636 4.665e-08 *** ## wait -0.09688675 0.01034202 -9.3683 < 2.2e-16 *** ## vcost -0.01391160 0.00665133 -2.0916 0.03648 * ## travel -0.00399468 0.00084915 -4.7043 2.547e-06 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Log-Likelihood: -192.89 ## McFadden R^2: 0.32024 ## Likelihood ratio test : chisq = 181.74 (p.value = < 2.22e-16) ``` ] --- class: middle # Understanding the results ```r summary(ml_reg)$coef ``` ``` ## (Intercept):train (Intercept):bus (Intercept):car wait vcost travel ## -0.786666672 -1.433633718 -4.739856473 -0.096886747 -0.013911604 -0.003994681 ## attr(,"names.sup.coef") ## character(0) ## attr(,"fixed") ## (Intercept):train (Intercept):bus (Intercept):car wait vcost travel ## FALSE FALSE FALSE FALSE FALSE FALSE ## attr(,"sup") ## character(0) ``` + intercept for `\(air\)` is dropped `\((air\)` is the base) * train:(intercept) is `\(-0.786\)` means that train is less likely to be chosen if all the other .blue[included] variables are the same + the greater the travel time, the less likely the option is chosen --- class: inverse, center, middle # Count data (Poisson) <html><div style='float:left'></div><hr color='#EB811B' size=1px width=1000px></html> --- class: middle # Count data (Poisson) Count variables take <span style = "color: blue;"> non-negative discrete integers </span> `\((0,1,\dots,)\)` + the number of times individuals get arrested in a year + the number of cars owned by a family + the number of kids in a family --- class: middle # Poisson regression By far the most popular choice to analyze count variables is <span style = "color: blue;"> Poisson regression </span> + The outcome (count) variable is assumed to be Poisson distributed + The mean of the Poisson distribution is assumed to be a function of some variables you believe matter -- <br> .content-box-green[**Poisson distribution**] Poisson distribution is a discrete probability distribution that describes the probability of the number of events that occur in a fixed interval of time and/or space $$ \begin{aligned} Prob(y|\lambda)=\frac{\lambda^y e^{-\lambda}}{y!}, \;\; \mbox{where} \;\; \lambda=E[y] \end{aligned} $$ --- class: middle <img src="data:image/png;base64,#discrete_choice_x_files/figure-html/poisson-1.png" width="60%" style="display: block; margin: auto;" /> .content-box-green[**Poisson regression**] We try to learn what and how variables affect the <span style = "color: blue;"> expected value </span> (the expected number of events conditional on independent variables). --- class: middle .content-box-green[**Expected number of events conditional on independent variables**] $$ \begin{aligned} E[y|\mathbf{x}] = G(\beta_0+\beta_1 x_1+\dots + \beta_k x_k) \end{aligned} $$ + This is exactly the same modeling framework we used - Linear model: `\(G(z)=z\)` - Probit model: `\(G(z)=\Phi(z)\)` -- <br> .content-box-green[**A popular choice of **]$G()$ $$ \begin{aligned} G(z) = exp(z) \end{aligned} $$ This ensures that the expected value conditional on `\(\mathbf{x}\)` is always positive --- class: middle .content-box-green[**The number of events for two individuals**] + Individual 1: `\(y=3\)` (own three cars) + Individual 2: `\(y=1\)` (own one car) -- <br> .content-box-green[**Expected number of events observed**] + Individual 1: `\(\lambda_1= exp(z_1)\)` + Individual 2: `\(\lambda_2= exp(z_2)\)` --- <br> .content-box-green[**Probability of observing the number of events we observed**] $$ \begin{aligned} \mbox{Individual 1}:& Prob(y=3|\mathbf{x_1})=\frac{\lambda_1^3 e^{-\lambda_1}}{3!} \\ \mbox{Individual 2}:& Prob(y=1|\mathbf{x_2})= \frac{\lambda_2^1 e^{-\lambda_2}}{1!} \end{aligned} $$ -- <br> .content-box-green[**Probability of observing a series of events by all individuals**] The probability that we observe the collection of choices made by them (if their events are independent) $$ \begin{aligned} L=Prob(y_1=3|\mathbf{x_1})\times Prob(y_2=1|\mathbf{x_2}) = \frac{\lambda_1^3 e^{-\lambda_1}}{3!}\times\frac{\lambda_2^1 e^{-\lambda_2}}{1!} \end{aligned} $$ which we call llikelihood function. -- <br> .content-box-green[**Log-likelihood function**] $$ \begin{aligned} LL=log(L)&= log(\frac{\lambda_1^3 e^{-\lambda_1}}{3!}) + log(\frac{\lambda_2^1 e^{-\lambda_2}}{1!}) \end{aligned} $$ (Remember `\(\lambda_i=exp(\beta_0+\beta_1 x_{i,1}+\dots + \beta_k x_{i,k})\)`) -- <br> $$ \begin{aligned} Max_{\beta_1,\dots,\beta_k}\;\; & LL \end{aligned} $$ --- class: middle # Implementation in R with an example The number of times a man is arrested during 1986: $$ \begin{aligned} Pr(narr86|\mathbf{x})= G(z) \end{aligned} $$ where $$ \begin{aligned} z = & \beta_0+\beta_1 pcnv+ \beta_2 tottime + \beta_3 qemp86 + \beta_4 inc86\\ & + \beta_5 black + \beta_6 hispan \end{aligned} $$ + `\(narr86\)`: \# of times arrested in 1986 + `\(pcnv\)`: proportion of prior conviction + `\(tottime\)`: time in prison since 18 + `\(qemp86\)`: \# quarters employed in 1986 + `\(inc86\)`: legal income in 1986 (in $\$100$) --- class: middle R code: importing the data ```r #--- import the data ---# data <- read.dta13("CRIME1.dta") #--- take a look ---# dplyr::select(data, narr86, pcnv, qemp86, inc86) %>% head() ``` ``` ## narr86 pcnv qemp86 inc86 ## 1 0 0.38 0 0.0 ## 2 2 0.44 1 0.8 ## 3 1 0.33 0 0.0 ## 4 2 0.25 2 8.8 ## 5 1 0.00 2 8.1 ## 6 0 1.00 4 97.6 ``` --- class: middle .content-box-green[**R code: Poisson model estimation using glm()**] ```r pois_lf <- glm( #--- formula ---# narr86 ~ pcnv + tottime + qemp86 + inc86 + black + hispan, #--- data ---# data = data, #--- models ---# family = poisson(link = "log") ) ``` .content-box-green[**`family` option**] + `poisson()`: tells `\(R\)` that your dependent variable is Poisson distributed + `link = "log"`: tells `\(R\)` that you want to use `\(exp()\)` (the inverse of `\(log()\)`) as `\(G()\)` in `\(E(y=1|\mathbf{x})=G(z)\)` --- class: middle .left4[ ```r msummary( pois_lf, # keep these options as they are stars = TRUE, gof_omit = "IC|Log|Adj|F|Pseudo|Within" ) ``` ] .right6[ <template id="aaf43cea-fb6a-464c-8779-61b847dc91ec"><style> .tabwid table{ border-spacing:0px !important; border-collapse:collapse; line-height:1; margin-left:auto; margin-right:auto; border-width: 0; display: table; margin-top: 1.275em; margin-bottom: 1.275em; border-color: transparent; } .tabwid_left table{ margin-left:0; } .tabwid_right table{ margin-right:0; } .tabwid td { padding: 0; } .tabwid a { text-decoration: none; } .tabwid thead { background-color: transparent; } .tabwid tfoot { background-color: transparent; } .tabwid table tr { background-color: transparent; } </style><div class="tabwid"><style>.cl-63756c58{}.cl-636fe472{font-family:'Helvetica';font-size:9pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-636ffb56{margin:0;text-align:left;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-63702a0e{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a18{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a22{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a23{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a2c{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a2d{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a36{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a37{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a40{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a41{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a4a{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a4b{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a54{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a55{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a5e{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a5f{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a68{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a69{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a6a{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(255, 255, 255, 0.00);border-top: 0 solid rgba(255, 255, 255, 0.00);border-left: 0 solid rgba(255, 255, 255, 0.00);border-right: 0 solid rgba(255, 255, 255, 0.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a72{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(255, 255, 255, 0.00);border-top: 0 solid rgba(255, 255, 255, 0.00);border-left: 0 solid rgba(255, 255, 255, 0.00);border-right: 0 solid rgba(255, 255, 255, 0.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a73{width:61.5pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 2pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-63702a7c{width:56.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 2pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-63756c58'><thead><tr style="overflow-wrap:break-word;"><td class="cl-63702a73"><p class="cl-636ffb56"><span class="cl-636fe472"> </span></p></td><td class="cl-63702a7c"><p class="cl-636ffb56"><span class="cl-636fe472">Model 1</span></p></td></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-63702a0e"><p class="cl-636ffb56"><span class="cl-636fe472">(Intercept)</span></p></td><td class="cl-63702a18"><p class="cl-636ffb56"><span class="cl-636fe472">-0.666***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a23"><p class="cl-636ffb56"><span class="cl-636fe472"></span></p></td><td class="cl-63702a22"><p class="cl-636ffb56"><span class="cl-636fe472">(0.064)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a55"><p class="cl-636ffb56"><span class="cl-636fe472">pcnv</span></p></td><td class="cl-63702a54"><p class="cl-636ffb56"><span class="cl-636fe472">-0.432***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a23"><p class="cl-636ffb56"><span class="cl-636fe472"></span></p></td><td class="cl-63702a22"><p class="cl-636ffb56"><span class="cl-636fe472">(0.085)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a5f"><p class="cl-636ffb56"><span class="cl-636fe472">tottime</span></p></td><td class="cl-63702a5e"><p class="cl-636ffb56"><span class="cl-636fe472">-0.001</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a23"><p class="cl-636ffb56"><span class="cl-636fe472"></span></p></td><td class="cl-63702a22"><p class="cl-636ffb56"><span class="cl-636fe472">(0.006)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a69"><p class="cl-636ffb56"><span class="cl-636fe472">qemp86</span></p></td><td class="cl-63702a68"><p class="cl-636ffb56"><span class="cl-636fe472">-0.010</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a23"><p class="cl-636ffb56"><span class="cl-636fe472"></span></p></td><td class="cl-63702a22"><p class="cl-636ffb56"><span class="cl-636fe472">(0.029)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a2d"><p class="cl-636ffb56"><span class="cl-636fe472">inc86</span></p></td><td class="cl-63702a2c"><p class="cl-636ffb56"><span class="cl-636fe472">-0.008***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a23"><p class="cl-636ffb56"><span class="cl-636fe472"></span></p></td><td class="cl-63702a22"><p class="cl-636ffb56"><span class="cl-636fe472">(0.001)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a2d"><p class="cl-636ffb56"><span class="cl-636fe472">black</span></p></td><td class="cl-63702a2c"><p class="cl-636ffb56"><span class="cl-636fe472">0.644***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a23"><p class="cl-636ffb56"><span class="cl-636fe472"></span></p></td><td class="cl-63702a22"><p class="cl-636ffb56"><span class="cl-636fe472">(0.074)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a37"><p class="cl-636ffb56"><span class="cl-636fe472">hispan</span></p></td><td class="cl-63702a36"><p class="cl-636ffb56"><span class="cl-636fe472">0.473***</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a23"><p class="cl-636ffb56"><span class="cl-636fe472"></span></p></td><td class="cl-63702a22"><p class="cl-636ffb56"><span class="cl-636fe472">(0.074)</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a41"><p class="cl-636ffb56"><span class="cl-636fe472">Num.Obs.</span></p></td><td class="cl-63702a40"><p class="cl-636ffb56"><span class="cl-636fe472">2725</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-63702a4a"><p class="cl-636ffb56"><span class="cl-636fe472">RMSE</span></p></td><td class="cl-63702a4b"><p class="cl-636ffb56"><span class="cl-636fe472">1.02</span></p></td></tr></tbody><tfoot><tr style="overflow-wrap:break-word;"><td colspan="2"class="cl-63702a6a"><p class="cl-636ffb56"><span class="cl-636fe472">+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001</span></p></td></tr></tfoot></table></div></template> <div class="flextable-shadow-host" id="bdc083f2-eea2-41fb-b7be-5382f2823498"></div> <script> var dest = document.getElementById("bdc083f2-eea2-41fb-b7be-5382f2823498"); var template = document.getElementById("aaf43cea-fb6a-464c-8779-61b847dc91ec"); var caption = template.content.querySelector("caption"); if(caption) { caption.style.cssText = "display:block;text-align:center;"; var newcapt = document.createElement("p"); newcapt.appendChild(caption) dest.parentNode.insertBefore(newcapt, dest.previousSibling); } var fantome = dest.attachShadow({mode: 'open'}); var templateContent = template.content; fantome.appendChild(templateContent); </script> ] --- class: middle # Calculate average marginal effects Just like the binomial regressions we saw earlier, we can use `margins::margins()` function to get the average marginal effects of covariates. ```r pois_marginal_e <- margins(pois_lf, type = "response") ```