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 ) options(cite = FALSE)
Gamma Regression for Continuous, Positive Dependent Variables with gamma
.
Use the gamma regression model if you have a positive-valued dependent variable such as the number of years a parliamentary cabinet endures, or the seconds you can stay airborne while jumping. The gamma distribution assumes that all waiting times are complete by the end of the study (censoring is not allowed).
z.out <- zelig(Y ~ X1 + X2, model = "gamma", weights = w, data = mydata) x.out <- setx(z.out) s.out <- sim(z.out, x = x.out, x1 = NULL)
Attach the sample data:
rm(list=ls(pattern="\\.out")) suppressWarnings(suppressMessages(library(Zelig))) set.seed(1234)
data(coalition)
Estimate the model:
z.out <- zelig(duration ~ fract + numst2, model = "gamma", data = coalition)
View the regression output:
summary(z.out)
Set the baseline values (with the ruling coalition in the minority) and the alternative values (with the ruling coalition in the majority) for X:
x.low <- setx(z.out, numst2 = 0) x.high <- setx(z.out, numst2 = 1)
Simulate expected values (qi$ev
) and first differences (qi$fd
):
s.out <- sim(z.out, x = x.low, x1 = x.high)
summary(s.out)
plot(s.out)
$$ \begin{aligned} Y &\sim& \textrm{Gamma}(y_i \mid \lambda_i, \alpha) \ f(y) &=& \frac{1}{\alpha^{\lambda_i} \, \Gamma \lambda_i} \, y_i^{\lambda_i - 1} \exp -\left{ \frac{y_i}{\alpha} \right}\end{aligned} $$ | for $\alpha, \lambda_i, y_i > 0$.
$$ \lambda_i = \frac{1}{x_i \beta} $$
$$ E(Y) = \alpha \lambda_i. $$
The predicted values (qi$pr) are draws from the gamma distribution for each given set of parameters $(\alpha, \lambda_i)$.
If x1 is specified, sim() also returns the differences in the expected values (qi$fd),
$$ E(Y \mid x_1) - E(Y \mid x) $$
.
$$ \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 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$get_coef()
returns the estimated coefficients, z.out$get_vcov()
returns the estimated covariance matrix, and z.out$get_predict()
provides predicted values for all observations in the dataset from the analysis.
The gamma model is part of the stats package by the R Core Team. Advanced users may
wish to refer to help(glm)
and help(family)
.
z5 <- zgamma$new() z5$references()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.