Logistic Regression for Dichotomous Dependent Variables
Logistic regression specifies a dichotomous dependent variable as a function of a set of explanatory variables.
With reference classes:
z5 <- zlogit$new()
z5$zelig(Y ~ X1 + X ~ X, weights = w, data = mydata)
z5$setx()
z5$sim()
With the Zelig 4 compatibility wrappers:
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)
Attaching the sample turnout dataset:
data(turnout)
Estimating parameter values for the logistic regression:
z.out1 <- zelig(vote ~ age + race, model = "logit", data = turnout)
## Warning in readLines(zeligmixedmodels): incomplete final line found on
## '/usr/lib64/R/library/ZeligMultilevel/JSON/zelig5mixedmodels.json'
## How to cite this model in Zelig:
## R Core Team. 2007.
## logit: Logistic Regression for Dichotomous Dependent Variables
## in Christine Choirat, Christopher Gandrud, James Honaker, Kosuke Imai, Gary King, and Olivia Lau,
## "Zelig: Everyone's Statistical Software," http://zeligproject.org/
Summarize estimated paramters:
summary(z.out1)
## Model:
##
## Call:
## z5$zelig(formula = vote ~ age + race, data = turnout)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.927 -1.296 0.707 0.777 1.072
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.03837 0.17692 0.22 0.82832
## age 0.01126 0.00305 3.69 0.00023
## racewhite 0.64555 0.13448 4.80 1.6e-06
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2266.7 on 1999 degrees of freedom
## Residual deviance: 2228.8 on 1997 degrees of freedom
## AIC: 2235
##
## Number of Fisher Scoring iterations: 4
##
## Next step: Use 'setx' method
Setting values for the explanatory variables:
x.out1 <- setx(z.out1, age = 36, race = "white")
Simulating quantities of interest from the posterior distribution.
s.out1 <- sim(z.out1, x = x.out1)
summary(s.out1)
##
## sim x :
## -----
## ev
## mean sd 50% 2.5% 97.5%
## [1,] 0.7477 0.01142 0.7478 0.7238 0.769
## pv
## 0 1
## [1,] 0.255 0.745
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)
## Warning in readLines(zeligmixedmodels): incomplete final line found on
## '/usr/lib64/R/library/ZeligMultilevel/JSON/zelig5mixedmodels.json'
## How to cite this model in Zelig:
## R Core Team. 2007.
## logit: Logistic Regression for Dichotomous Dependent Variables
## in Christine Choirat, Christopher Gandrud, James Honaker, Kosuke Imai, Gary King, and Olivia Lau,
## "Zelig: Everyone's Statistical Software," http://zeligproject.org/
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)
##
## sim x :
## -----
## ev
## mean sd 50% 2.5% 97.5%
## [1,] 0.8229 0.01008 0.8232 0.8024 0.8416
## pv
## 0 1
## [1,] 0.179 0.821
##
## sim x1 :
## -----
## ev
## mean sd 50% 2.5% 97.5%
## [1,] 0.7096 0.01274 0.7098 0.6848 0.7332
## pv
## 0 1
## [1,] 0.31 0.69
## fd
## mean sd 50% 2.5% 97.5%
## [1,] -0.1134 0.01119 -0.1133 -0.1345 -0.09177
plot(s.out2)
Let be the binary dependent variable for observation which takes the value of either 0 or 1.
The stochastic component is given by
where .
The systematic component is given by:
where is the vector of explanatory variables for observation and is the vector of coefficients.
The expected values (qi$ev) for the logit model are simulations of the predicted probability of a success:
given draws of from its sampling distribution.
The predicted values (qi$pr) are draws from the Binomial distribution with mean equal to the simulated expected value .
The first difference (qi$fd) for the logit model is defined as
The risk ratio (qi$rr) is defined as
In conditional prediction models, the average expected treatment effect (att.ev) for the treatment group is
where is a binary explanatory variable defining the treatment () and control () groups. Variation in the simulations are due to uncertainty in simulating , the counterfactual expected value of for observations in the treatment group, under the assumption that everything stays the same except that the treatment indicator is switched to .
In conditional prediction models, the average predicted treatment effect (att.pr) for the treatment group is
where is a binary explanatory variable defining the treatment () and control () groups. Variation in the simulations are due to uncertainty in simulating , the counterfactual predicted value of for observations in the treatment group, under the assumption that everything stays the same except that the treatment indicator is switched to .
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 commands demonstrated above, some simply utility functions (known as getters) provide easy access to the raw fields most commonly of use for further investigation.
In the example above z.out$getcoef() returns the estimated coefficients, z.out$getvcov() returns the estimated covariance matrix, and z.out$getpredict() provides predicted values for all observations in the dataset from the analysis.
The logit model is part of the stats package. Advanced users may wish to refer to help(glm) and help(family).
R Core Team (2016). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. <URL: https://www.R-project.org/>.