----------------------------------------------------------------------------------------------- name: log: U:\projects\misc\tobit\tobit_d0.txt log type: text opened on: 29 Aug 2013, 15:27:59 . . // --- Simulate tobit data . clear . . set seed 1234567 . set obs 1000 obs was 0, now 1000 . . gen x1 = rchi2(1) . gen ystar = 3 -0.5*x1 + rnormal(0,2) . gen ytobit = cond(ystar <0, 0, ystar) . . hist ystar, saving(ystar.gph, replace) (bin=29, start=-5.9660091, width=.49397278) (file ystar.gph saved) . hist ytobit, saving(ytobit.gph, replace) (bin=29, start=0, width=.28824833) (file ytobit.gph saved) . graph combine ystar.gph ytobit.gph, col(1) . . // -- Write down likelihood . capture program drop mytobit2_lf . program mytobit2_lf 1. version 12 2. args todo b lnf 3. tempvar xb lnsigma sigma lj 4. . // Define theta . mleval `xb' = `b', eq(1) 5. mleval `lnsigma' = `b', scalar eq(2) 6. . // Transform so varariances are > 0 . qui gen double `sigma' = exp(`lnsigma') 7. . // Define likelihood . qui gen double `lj' = normal(-`xb'/`sigma') /// > if $ML_y1 ==0 8. qui replace `lj' = normalden($ML_y1,`xb',`sigma') /// > if $ML_y1 > 0 9. qui mlsum `lnf' = ln(`lj') 10. . if (`todo'==0 | `lnf' >=.) exit 11. end . * Program to display results . capture program drop Replay . program Replay, eclass 1. ml display 2. local scale `"sigma"' 3. . _diparm ln`scale', exp label(`scale') 4. ereturn scalar `scale'_est = r(est) 5. ereturn scalar `scale'_se = r(se) 6. _diparm __bot__ 7. end . . // -- Esimate model . ml model d0 mytobit2_lf (xb: ytobit = x1) /lnsigma . ml search initial: log likelihood = -5743.0178 alternative: log likelihood = -2728.9999 rescale: log likelihood = -2188.3808 rescale eq: log likelihood = -2081.2859 . ml maximize initial: log likelihood = -2081.2859 rescale: log likelihood = -2081.2859 rescale eq: log likelihood = -2081.2859 Iteration 0: log likelihood = -2081.2859 Iteration 1: log likelihood = -1971.993 Iteration 2: log likelihood = -1970.7715 Iteration 3: log likelihood = -1970.7697 Iteration 4: log likelihood = -1970.7697 Number of obs = 1000 Wald chi2(1) = 103.64 Log likelihood = -1970.7697 Prob > chi2 = 0.0000 ------------------------------------------------------------------------------ ytobit | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- xb | x1 | -.4904057 .0481711 -10.18 0.000 -.5848194 -.395992 _cons | 2.893981 .0801996 36.08 0.000 2.736792 3.051169 -------------+---------------------------------------------------------------- lnsigma | _cons | .6806156 .0245268 27.75 0.000 .632544 .7286872 ------------------------------------------------------------------------------ . Replay Number of obs = 1000 Wald chi2(1) = 103.64 Log likelihood = -1970.7697 Prob > chi2 = 0.0000 ------------------------------------------------------------------------------ ytobit | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- xb | x1 | -.4904057 .0481711 -10.18 0.000 -.5848194 -.395992 _cons | 2.893981 .0801996 36.08 0.000 2.736792 3.051169 -------------+---------------------------------------------------------------- lnsigma | _cons | .6806156 .0245268 27.75 0.000 .632544 .7286872 ------------------------------------------------------------------------------ sigma | 1.975093 .0484427 1.882393 2.072358 ------------------------------------------------------------------------------ . . * Can use Stata's default post-estimation of linear part . predict yhat . sum yhat Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- yhat | 1000 2.363421 .6988556 -1.852251 2.89398 . * note that there are negative values. The tobit model estiamates . * the latent variable . . // -- Compare to Stata's Tobit . tobit ytobit x1, ll(0) Tobit regression Number of obs = 1000 LR chi2(1) = 105.18 Prob > chi2 = 0.0000 Log likelihood = -1970.7697 Pseudo R2 = 0.0260 ------------------------------------------------------------------------------ ytobit | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | -.4904056 .048171 -10.18 0.000 -.5849336 -.3958776 _cons | 2.89398 .0801994 36.08 0.000 2.736602 3.051359 -------------+---------------------------------------------------------------- /sigma | 1.975093 .0484424 1.880033 2.070154 ------------------------------------------------------------------------------ Obs. summary: 122 left-censored observations at ytobit<=0 878 uncensored observations 0 right-censored observations . . // --- Compare to linear regression . reg ytobit x1 Source | SS df MS Number of obs = 1000 -------------+------------------------------ F( 1, 998) = 88.41 Model | 281.156588 1 281.156588 Prob > F = 0.0000 Residual | 3173.68062 998 3.1800407 R-squared = 0.0814 -------------+------------------------------ Adj R-squared = 0.0805 Total | 3454.8372 999 3.4582955 Root MSE = 1.7833 ------------------------------------------------------------------------------ ytobit | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | -.3722712 .0395915 -9.40 0.000 -.4499633 -.2945791 _cons | 2.902435 .0708147 40.99 0.000 2.763473 3.041398 ------------------------------------------------------------------------------ . . * Note that linear regression is biased but tobit model is not since . * the data generating process corresponds to a censored normal . . log close name: log: U:\projects\misc\tobit\tobit_d0.txt log type: text closed on: 29 Aug 2013, 15:28:04 -----------------------------------------------------------------------------------------------