-
Notifications
You must be signed in to change notification settings - Fork 82
/
hurdle.R
168 lines (127 loc) · 3.5 KB
/
hurdle.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#' ---
#' title: "Hurdle Models"
#' author: "Michael Clark"
#' date: ""
#' ---
#'
#'
#' # Poisson
hurdpoisloglik = function(y, X, par) {
# Extract parameters
logitpars = par[grep('logit', names(par))]
poispars = par[grep('pois', names(par))]
# Logit model part
Xlogit = X
ylogit = ifelse(y == 0, 0, 1)
LPlogit = Xlogit %*% logitpars
mulogit = plogis(LPlogit)
# Calculate the likelihood
logliklogit = -sum( ylogit*log(mulogit) + (1 - ylogit)*log(1 - mulogit) )
# Poisson part
Xpois = X[y > 0, ]
ypois = y[y > 0]
mupois = exp(Xpois %*% poispars)
# Calculate the likelihood
loglik0 = -mupois
loglikpois = -sum(dpois(ypois, lambda = mupois, log = TRUE)) + sum(log(1 - exp(loglik0)))
# combine likelihoods
loglik = loglikpois + logliklogit
loglik
}
hurdNBloglik = function(y, X, par) {
# Extract parameters
logitpars = par[grep('logit', names(par))]
NegBinpars = par[grep('NegBin', names(par))]
theta = exp(par[grep('theta', names(par))])
# Logit model part
Xlogit = X
ylogit = ifelse(y == 0, 0, 1)
LPlogit = Xlogit%*%logitpars
mulogit = plogis(LPlogit)
# Calculate the likelihood
logliklogit = -sum( ylogit*log(mulogit) + (1 - ylogit)*log(1 - mulogit) )
#NB part
XNB = X[y > 0, ]
yNB = y[y > 0]
muNB = exp(XNB %*% NegBinpars)
# Calculate the likelihood
loglik0 = dnbinom(0, mu = muNB, size = theta, log = TRUE)
loglik1 = dnbinom(yNB, mu = muNB, size = theta, log = TRUE)
loglikNB = -( sum(loglik1) - sum(log(1 - exp(loglik0))) )
# combine likelihoods
loglik = loglikNB + logliklogit
loglik
}
#' # Data Import
#' Import a simple data set. Example from the Stata help file for zinb command;
#' can compare results with hnblogit command
library(haven)
fish = read_dta("http://www.stata-press.com/data/r11/fish.dta")
# Get some starting values.
init_mod = glm(
count ~ persons + livebait,
data = fish,
family = poisson,
x = TRUE,
y = TRUE
)
#' for these functions, a named vector for the starting values
starts = c(logit = coef(init_mod), pois = coef(init_mod))
#' # Poisson hurdle
#' Use `optim` to estimate parameters. I fiddle with some options to reproduce the
#' hurdle function as much as possible.
#'
optPois1 = optim(
par = starts,
fn = hurdpoisloglik,
X = init_mod$x,
y = init_mod$y,
control = list(maxit = 5000, reltol = 1e-12),
hessian = TRUE
)
# optPois1
#' Extract the elements from the output to create a summary table.
B = optPois1$par
se = sqrt(diag(solve(optPois1$hessian)))
Z = B/se
p = ifelse(Z >= 0, pnorm(Z, lower = FALSE)*2, pnorm(Z)*2)
summarytable = round(data.frame(B, se, Z, p), 3)
list(summary = summarytable, ll = optPois1$value)
#' Compare to hurdle from pscl package.
library(pscl)
poismod = hurdle(
count ~ persons + livebait,
data = fish,
zero.dist = "binomial",
dist = "poisson"
)
summary(poismod)
#' # Negative Binomial hurdle
starts = c(
logit = coef(init_mod),
NegBin = coef(init_mod),
theta = 1
)
optNB1 = optim(
par = starts,
fn = hurdNBloglik,
X = init_mod$x,
y = init_mod$y,
control = list(maxit = 5000, reltol = 1e-12),
method = "BFGS",
hessian = TRUE
)
# optNB1
B = optNB1$par
se = sqrt(diag(solve(optNB1$hessian)))
Z = B/se
p = ifelse(Z >= 0, pnorm(Z, lower = FALSE)*2, pnorm(Z)*2)
summarytable = round(data.frame(B, se, Z, p), 3)
list(summary = summarytable, ll = optNB1$value)
NBmod = hurdle(
count ~ persons + livebait,
data = fish,
zero.dist = "binomial",
dist = "negbin"
)
summary(NBmod)