Built using Zelig version r packageVersion('Zelig')
knitr::opts_knit$set( stop_on_error = 2L ) knitr::opts_chunk$set( fig.height = 11, fig.width = 7 )
Logistic Regression for Dichotomous Dependent Variables with logit
.
Logistic regression specifies a dichotomous dependent variable as a function of a set of explanatory variables.
z.out <- zelig(Y ~ X1 + X2, model = "logit", weights = w, data = mydata) x.out <- setx(z.out) s.out <- sim(z.out, x = x.out, x1 = NULL)
Load Zelig and attach the sample turnout dataset:
library(Zelig) data(turnout)
Estimating parameter values for the logistic regression:
z.out1 <- zelig(vote ~ age + race, model = "logit", data = turnout, cite = FALSE)
Summarize estimated paramters:
summary(z.out1)
For logit
models you can also include the argument odds_ratios = TRUE
in the
summary
call to return odds ratios estimates ($\mathrm{exp}(\beta)$):
summary(z.out1, odds_ratios = TRUE)
Set values for the explanatory variables:
x.out1 <- setx(z.out1, age = 36, race = "white")
Simulate quantities of interest from the posterior distribution:
s.out1 <- sim(z.out1, x = x.out1) summary(s.out1)
Show the results graphically:
plot(s.out1)
Estimating the risk difference (and risk ratio) between low education (25th percentile) and high education (75th percentile) while all the other variables held at their default values.
z.out2 <- zelig(vote ~ race + educate, model = "logit", data = turnout, cite = FALSE) x.high <- setx(z.out2, educate = quantile(turnout$educate, prob = 0.75)) x.low <- setx(z.out2, educate = quantile(turnout$educate, prob = 0.25)) s.out2 <- sim(z.out2, x = x.high, x1 = x.low) summary(s.out2)
plot(s.out2)
Let $Y_i$ be the binary dependent variable for observation $i$ which takes the value of either 0 or 1.
$$\begin{aligned} Y_i &\sim& \textrm{Bernoulli}(y_i \mid \pi_i) \ &=& \pi_i^{y_i} (1-\pi_i)^{1-y_i}\end{aligned} $$
where $\pi_i=\Pr(Y_i=1)$.
$$\pi_i \; = \; \frac{1}{1 + \exp(-x_i \beta)}.$$
where $x_i$ is the vector of $k$ explanatory variables for observation $i$ and $\beta$ is the vector of coefficients.
$$ E(Y) = \pi_i= \frac{1}{1 + \exp(-x_i \beta)}, $$
given draws of $\beta$ from its sampling distribution.
The predicted values are draws from the Binomial distribution with mean equal to the simulated expected value $\pi_i$.
The first difference for the logit model is defined as
$$\textrm{FD} = \Pr(Y = 1 \mid x_1) - \Pr(Y = 1 \mid x).$$
The risk ratio is defined as
$$\textrm{RR} = \Pr(Y = 1 \mid x_1) \ / \ \Pr(Y = 1 \mid x).$$
In conditional prediction models, the average expected treatment effect (att.ev) for the treatment group is
$$ \frac{1}{\sum_{i=1}^n t_i}\sum_{i:t_i=1}^n \left{ Y_i(t_i=1) - E[Y_i(t_i=0)] \right}, $$
where $t_i$ is a binary explanatory variable defining the treatment ($t_i=1$) and control ($t_i=0$) groups. Variation in the simulations are due to uncertainty in simulating $E[Y_i(t_i=0)]$, the counterfactual expected value of $Y_i$ for observations in the treatment group, under the assumption that everything stays the same except that the treatment indicator is switched to $t_i=0$.
$$ \frac{1}{\sum_{i=1}^n t_i}\sum_{i:t_i=1}^n \left{ Y_i(t_i=1) - \widehat{Y_i(t_i=0)}\right}, $$
where $t_i$ is a binary explanatory variable defining the treatment ($t_i=1$) and control ($t_i=0$) groups. Variation in the simulations are due to uncertainty in simulating $\widehat{Y_i(t_i=0)}$, the counterfactual predicted value of $Y_i$ for observations in the treatment group, under the assumption that everything stays the same except that the treatment indicator is switched to $t_i=0$.
The Zelig object stores fields containing everything needed to rerun the Zelig output, and all the results and simulations as they are generated. In addition to the summary functions demonstrated above, use standard R utility functions such as coef
, vcov
, predict
to extract model estimates and zelig_qi_to_df
to extract simulations.
The logit model is part of the stats R package. Advanced users may wish to refer to help(glm)
and help(family)
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.