-
Notifications
You must be signed in to change notification settings - Fork 17
/
tobit.Rmd
215 lines (159 loc) · 5.12 KB
/
tobit.Rmd
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Tobit Regression
The following is a simple demonstration of tobit regression via maximum
likelihood. The issue is one where data is censored such that while we observe
the value, it is not the true value, which would extend beyond the range of the
observed data. This is very commonly seen in cases where the dependent variable
has been given some arbitrary cutoff at the lower or upper end of the range,
often resulting in floor or ceiling effects respectively. The conceptual idea is
that we are interested in modeling the underlying latent variable that would not
have such restriction if it was actually observed.
## Censoring with an Upper Limit
### Data Setup
Data regards academic aptitude (GRE scores) with will be modeled using reading
and math test scores, as well as the type of program the student is enrolled in
(academic, general, or vocational). See
[this](https://stats.idre.ucla.edu/r/dae/tobit-models/) for an applied example
and more detail.
```{r tobit-setup}
library(tidyverse)
acad_apt = read_csv("https://stats.idre.ucla.edu/stat/data/tobit.csv") %>%
mutate(prog = factor(prog, labels = c('acad', 'general', 'vocational')))
```
Setup data and initial values.
```{r tobit-initial}
initmod = lm(apt ~ read + math + prog, data = acad_apt)
X = model.matrix(initmod)
init = c(coef(initmod), log_sigma = log(summary(initmod)$sigma))
```
### Function
```{r tobit-ll}
tobit_ll <- function(par, X, y, ul = -Inf, ll = Inf) {
# this function only takes a lower OR upper limit
# parameters
sigma = exp(par[length(par)])
beta = par[-length(par)]
# create indicator depending on chosen limit
if (!is.infinite(ll)) {
limit = ll
indicator = y > ll
} else {
limit = ul
indicator = y < ul
}
# linear predictor
lp = X %*% beta
# log likelihood
ll = sum(indicator * log((1/sigma)*dnorm((y-lp)/sigma)) ) +
sum((1-indicator) * log(pnorm((lp-limit)/sigma, lower = is.infinite(ll))))
-ll
}
```
### Estimation
Estimate via <span class="func" style = "">optim</span>.
```{r tobit-estimate}
fit_tobit = optim(
par = init,
tobit_ll,
y = acad_apt$apt,
X = X,
ul = 800,
method = 'BFGS',
control = list(maxit = 2000, reltol = 1e-15)
)
# this would be more akin to the default Stata default approach
# optim(
# par = init,
# tobit_ll,
# y = acad_apt$apt,
# X = X,
# ul = 800,
# control = list(maxit = 16000, reltol = 1e-15)
# )
```
### Comparison
Compare to <span class="pack" style = "">AER</span> package tobit function.
```{r tobit-compare-aer}
library(survival)
fit_aer = AER::tobit(
apt ~ read + math + prog,
data = acad_apt,
left = -Inf,
right = 800
)
```
```{r tobit-compare-aer-show, echo=FALSE}
rbind(
fit_tobit = c(
fit_tobit$par[1:5],
sigma = exp(fit_tobit$par[6]),
logLike = -fit_tobit$value
),
AER = c(coef(fit_aer), fit_aer$scale, logLik(fit_aer))
) %>%
kable_df()
```
```{r tobit-compare-aer}
```
<span class="pack" style = "">AER</span> is actually just using <span
class="func" style = "">survreg</span> from the <span class="pack" style =
"">survival</span> package. Survival models are usually for modeling time to
some event, e.g. death in medical studies, and the censoring comes from the fact
that the observed event does not occur for some people. Like our tobit function,
an indicator is needed to denote who is or isn't censored. In survival models,
the indicator is for the event itself, and means they are NOT censored. So
we'll reverse the indicator used in the tobit function for <span class="func"
style = "">survreg</span>.
```{r tobit-surv}
fit_surv = survreg(Surv(apt, apt < 800, type = 'right') ~ read + math + prog,
data = acad_apt,
dist = 'gaussian')
```
Compare all results.
```{r tobit-compare-surv, echo=FALSE}
rbind(
fit_tobit = c(
fit_tobit$par[1:5],
sigma = exp(fit_tobit$par[6]),
logLike = -fit_tobit$value
),
AER = c(coef(fit_aer), fit_aer$scale, logLik(fit_aer)),
survival = c(coef(fit_surv), fit_surv$scale, logLik(fit_surv))
) %>%
kable_df()
```
## Censoring with a Lower Limit
Create a censored data situation for the low end. The scale itself would be censored for anyone scoring a 200, but that basically doesn't happen. In this data, 15 are less than a score of 500, so we'll do that.
```{r tobit-ll-setup}
acad_apt = acad_apt %>%
mutate(apt2 = apt,
apt2 = if_else(apt2 < 500, 500, apt2))
```
Estimate and use <span class="pack" style = "">AER</span> for comparison.
```{r tobit-ll-est}
fit_tobit = optim(
par = init,
tobit_ll,
y = acad_apt$apt2,
X = X,
ll = 400,
method = 'BFGS',
control = list(maxit = 2000, reltol = 1e-15)
)
fit_aer = AER::tobit(apt2 ~ read + math + prog,
data = acad_apt,
left = 400)
```
### Comparison
```{r tobit-ll-compare, echo=FALSE}
rbind(
fit_tobit = c(
fit_tobit$par[1:5],
sigma = exp(fit_tobit$par[6]),
logLike = -fit_tobit$value
),
AER = c(coef(fit_aer), fit_aer$scale, logLik(fit_aer))
) %>%
kable_df()
```
## Source
Original code available at https://github.com/m-clark/Miscellaneous-R-Code/blob/master/ModelFitting/tobit.R