Zero-inflated and hurdle count models
zeroinfl_fit.RdCounts with more zeros than a Poisson or negative binomial predicts, as
pscl::zeroinfl() and pscl::hurdle(), with pscl's two-part formula
y ~ x | z: the count part on x, mu = exp(x beta + offset), the zero
part on z with a binary link.
Usage
zeroinfl_fit(
formula,
data,
dist = "poisson",
link = "logit",
offset = NULL,
weights = NULL
)
hurdle_fit(
formula,
data,
dist = "poisson",
link = "logit",
offset = NULL,
weights = NULL
)
zero_model(ptr, terms, xlevels, zterms, zxlevels, n)
count_log_likelihoods(y, mu, theta = NULL)
vuong_test(m1, m2, k1 = NULL, k2 = NULL)Arguments
- formula
A two-part formula
y ~ x | zor a one-party ~ x.- data
A data frame.
- dist
"poisson"or"negbin".- link
The zero part's link:
"logit","probit"or"cloglog".- offset, weights
An optional offset of the count part (added to any
offset()term) and prior weights.- y, mu, theta
Counts, means and the negative binomial's
theta(NULLfor the Poisson).- m1, m2
Two
zero_models or numeric vectors of the rows' log-likelihoods.- k1, k2
Their parameter counts, needed for a numeric vector.
Value
A zero_model with count_coefficients, count_std_errors,
zero_coefficients, zero_std_errors (of the structural zero's
probability, or of P(y > 0) for a hurdle), theta and
se_log_theta (NA for the Poisson), covariance (count, zero and
log(theta)), loglik, log_likelihoods (each row's, without its
weight), aic, bic, fitted, n_parameters, kind, dist and
iterations. coef() names them as pscl's count_x and zero_x;
logLik(), AIC() and BIC() work; predict(m, newdata, type =)
gives the mean ("response"), P(y = 0) ("zero") or a matrix of
P(y = k) for k in 0:max ("prob"); predict_distribution()
joint draws. vuong_test() returns a data frame of the raw, AIC- and
BIC-corrected statistics with their one-sided p-values (a positive
statistic favours m1).
Details
zeroinfl_fit(): a zero is structural with probability
pi = h(z gamma), otherwise the count is drawn from the Poisson or the
negative binomial (theta estimated), so P(0) = pi + (1 - pi) f(0);
the parameters are estimated jointly by maximum likelihood.
hurdle_fit(): the zero part gives p = h(z gamma) = P(y > 0) and a
positive count is drawn from the count distribution truncated at zero;
its two parts are fitted one at a time. Both maximise by Newton's method
on analytic gradients, and their standard errors come from the observed
information, as pscl's. Without | both parts use the formula's terms;
unlike pscl, the formula's offset() (an exposure) then applies to the
count part only: write it on the zero side too (y ~ x + offset(log(e)) | x + offset(log(e))) to put it there. Prior weights weight the whole
likelihood, theta's score included (pscl's leaves them out of it;
docs/design/models.md, "Zero-inflated and hurdle counts").
vuong_test() is Vuong's non-nested test of two models, as
pscl::vuong(): each a zero_model or a numeric vector of the rows'
log-likelihoods (with its parameter count k1 or k2). The counts are
pscl's, the coefficients without theta, so a plain GLM's is
length(coef(m)). count_log_likelihoods() gives the rows'
log-likelihoods of a plain Poisson or negative binomial with means mu.
Examples
d <- data.frame(x = rep(0:3 / 3, 50))
d$y <- ifelse(seq_len(200) %% 3 == 1, 0, (seq_len(200) * 7) %% 5)
m <- zeroinfl_fit(y ~ x | 1, d)
coef(m)
#> count_(Intercept) count_x zero_(Intercept)
#> 0.82890750 -0.05325571 -0.37995471
h <- hurdle_fit(y ~ x, d, dist = "negbin")
#> Error in hurdle_fit(y ~ x, d, dist = "negbin"): invalid data: the hurdle's truncated count maximum was not reached in 200 Newton steps; with a negative binomial count part, theta grows without bound when the counts are not overdispersed: use the Poisson
predict(h, d[1:2, , drop = FALSE], type = "prob", max = 3)
#> Error: object 'h' not found
g <- glm_fit(y ~ x, d, family = "poisson")
vuong_test(m, count_log_likelihoods(d$y, g@fitted), k2 = 2)
#> statistic p_value
#> raw 5.170795 1.165503e-07
#> aic 5.042622 2.295980e-07
#> bic 4.831245 6.784111e-07