Implementation in Stata
Contents
Implementation in Stata#
The following code shows some of the mechanics for running a Tobit Model as well as ways we can use the model results after estimation. This uses a “toy dataset” that has only 1 dependent variable.
# or ["remove_output", "remove_input"]
# start a connected stata17 session
from pystata import config
config.init('be')
config.set_streaming_output_mode('off')
%%stata
clear
webuse set "https://rlhick.people.wm.edu/econ407/data"
webuse toy_tobit
sum
. clear
. webuse set "https://rlhick.people.wm.edu/econ407/data"
(prefix now "https://rlhick.people.wm.edu/econ407/data")
. webuse toy_tobit
. sum
Variable | Obs Mean Std. dev. Min Max
-------------+---------------------------------------------------------
index | 5,000 2499.5 1443.52 0 4999
y | 5,000 5.082118 .9811196 3.858572 8.662185
x | 5,000 -.0008725 1.001673 -3.620101 3.748413
.
The first 5 rows of data looks like this
%%stata
list in 1/5
+--------------------------------+
| index y x |
|--------------------------------|
1. | 0 4.2630254 -1.2008912 |
2. | 1 4.6382766 .1366034 |
3. | 2 4.776782 1.1964091 |
4. | 3 6.8389654 .71531347 |
5. | 4 5.2471644 .39031073 |
+--------------------------------+
Of particular interest is our censored dependent variable \(\mathbf{y}\). The histogram is
%%stata
hist y, frequency bin(20) graphregion(color(white)) ///
title("Histogram of the Censored Dependent Variable") xtitle("y")
*graph export "/tmp/toy_tobit_hist.eps", replace
. hist y, frequency bin(20) graphregion(color(white)) ///
> title("Histogram of the Censored Dependent Variable") xtitle("y")
(bin=20, start=3.8585718, width=.24018066)
. *graph export "/tmp/toy_tobit_hist.eps", replace
.
The scatterplot also shows how the censored values have been “stacked” on the lowering censoring point and how lower values of \(\mathbf{y}\) are dragged right towards the censoring point.
%%stata
scatter x y, graphregion(color(white)) title("Scatterplot of x and y") ///
msize(tiny) xtitle("x") ytitle("y") xlab(0(.5)9)
*graph export "/tmp/toy_tobit_scatter.eps", replace
. scatter x y, graphregion(color(white)) title("Scatterplot of x and y") ///
> msize(tiny) xtitle("x") ytitle("y") xlab(0(.5)9)
. *graph export "/tmp/toy_tobit_scatter.eps", replace
.
To run the tobit model, we issue the command
%%stata
egen a = min(y)
display "Lower Censoring Point (a): ", a
tobit y x, ll
. egen a = min(y)
. display "Lower Censoring Point (a): ", a
Lower Censoring Point (a): 3.8585718
. tobit y x, ll
Refining starting values:
Grid node 0: log likelihood = -6924.13
Fitting full model:
Iteration 0: log likelihood = -6924.13
Iteration 1: log likelihood = -6813.1161
Iteration 2: log likelihood = -6810.4363
Iteration 3: log likelihood = -6810.4337
Iteration 4: log likelihood = -6810.4337
Tobit regression Number of obs = 5,000
Uncensored = 4,198
Limits: Lower = 3.86 Left-censored = 802
Upper = +inf Right-censored = 0
LR chi2(1) = 1074.36
Prob > chi2 = 0.0000
Log likelihood = -6810.4337 Pseudo R2 = 0.0731
------------------------------------------------------------------------------
y | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
x | .5062554 .01488 34.02 0.000 .4770841 .5354267
_cons | 4.986882 .0147063 339.10 0.000 4.958051 5.015713
-------------+----------------------------------------------------------------
var(e.y)| 1.02864 .0232449 .9840649 1.075235
------------------------------------------------------------------------------
.
where ll
signals that Stata should use a lower censor limit.
Outputs from the Tobit Model#
Once we have run the model, we can use the results for creating parameter values that we might want to use for veryifying our understanding of the Tobit Model.
Parameter |
Stata Command |
---|---|
\(\sigma\) |
|
\(log(L)\) |
|
\(\beta_{name}\) |
|
\(\mathbf{x}\hat{\beta}\) |
|
Example code using these special variables will create a new variable in your data space:
gen sigma = sqrt(_b[/var(e.y)])
gen logLike = e(ll)
gen beta_x = _b[x]
predict xb, xb
These can be used for replicating a number of the outputs available as postestimation commands. The following table lists the various expected values from the Tobit model, the formula it is based on, and the stata commands for generating marginal effects and predicted values, respectively.
Expected Value |
Formula |
Stata Command |
---|---|---|
\(E[y \shortmid y\hspace{.03in} obs]\) |
\(\mathbf{x}_i \beta^{T} + \sigma \frac{\phi \left(\frac{a-\mathbf{x}_i \beta^{T}}{\sigma} \right)}{1-\Phi \left(\frac{a-\mathbf{x}_i \beta^{T}}{\sigma}\right)}\) |
|
|
||
\(Prob(\text{Not Censored})\) |
\(1-\Phi \left(\frac{a-\mathbf{x}_i \beta^{T}}{\sigma} \right)\) |
|
|
||
\(E[y]\) |
\(\Phi \left (\frac{a-\mathbf{x}_i \beta^{T}}{\sigma} \right ) a + \left (1-\Phi \left(\frac{a-\mathbf{x}_i \beta^{T}}{\sigma}\right ) \right) \left [\mathbf{x}_i \beta^{T} + \sigma \frac{\phi \left( \frac{a-\mathbf{x}_i \beta^{T}}{\sigma} \right)}{1-\Phi \left (\frac{a-\mathbf{x}_i \beta^{T}}{\sigma} \right)} \right ]\) |
|
|
||
\(E[y^*]\) |
\(\mathbf{x}_i \beta^{T}\) |
|
|
Noting that \(\beta^T\) are the estimates of \(\beta\) from the Tobit Model.