The basic idea is 'success probability of binomial distribution' * 'lambda (= an expected value) of poisson distribution'. But you have to consider that the poisson model in count part never returns 0.
I supposed your example data and predicted when width
is c(23, 26, 29)
.
library(pscl)
data <- data.frame(y = c(8, 0, 3, 7), width = c(34.40, 22.50, 28.34, 32.22))
model <- hurdle(y ~ width, data = data)
model
# Count model coefficients (truncated poisson with log link):
# (Intercept) width
# -3.4520 0.1629 # model$coef$count[[1]] (left); model$coef$count[[2]] (right)
# Zero hurdle model coefficients (binomial with logit link):
# (Intercept) width
# -198.851 7.809 # model$coef$zero[[1]] (left); model$coef$zero[[2]] (right)
First, you calculate a success probability of a binomial model, phi_zero
, (logistic equation). (I attached a taking log version because of predict()
uses it.)
phi_zero <- 1 / ( 1 + exp(-(model$coef$zero[[1]] + model$coef$zero[[2]] * c(23, 26, 29))))
# p0_zero <- log(phi_zero)
Second, you calculate a param (= expected value) of a poisson model, mu
, and the > 0 probability, phi_count
.
mu <- exp(model$coef$count[[1]] + model$coef$count[[2]] * c(23, 26, 29))
phi_count <- ppois(0, lambda = mu, lower.tail = F) # not 0 probability
# p0_count <- ppois(0, lambda = mu, lower.tail = F, log.p = T)
Finally, you integrate values of both model.
phi <- phi_zero / phi_count # because there isn't 0 coming from poisson.
rval <- phi * mu
# logphi <- p0_zero - p0_count
# rval2 <- exp(logphi + log(mu))
rval
# [1] 8.051324e-09 2.429582e+00 3.674317e+00
And predict()
takes a class hurdle
as an argument (see ?predict.hurdle
).
predict(model, data.frame(width = c(23, 26, 29)), type = "response")
# 1 2 3
# 8.051324e-09 2.429582e+00 3.674317e+00 # the same