Stata Tidbits

These tidbits contain bits and pieces of information I hope you find helpful to use Stata more effectively. You can receive notifications of new tidbits as they are added (via email) by clicking on the subscribe box at the left. (Every email has an unsubscribe link, making it a snap to unsubscribe.)
« Posting results: Multinomial Logistic | Posting results: Regression »
Tuesday
Mar302010

Posting results: Logistic and Ordinal Logistic

Last week we saw the basics of how to post results after the regress command. This week we will see how to post results after the logit and ologit commands. Next week we will see how to post results after the mlogit command.

Let's use the dataset xy2 and look at a logistic regression predicting y from x1.

. use xy2 . logit y x1 Iteration 0: log likelihood = -693.01918 Iteration 1: log likelihood = -692.94877 Iteration 2: log likelihood = -692.94877 Logistic regression Number of obs = 1000 LR chi2(1) = 0.14 Prob > chi2 = 0.7075 Log likelihood = -692.94877 Pseudo R2 = 0.0001 ------------------------------------------------------------------------------ y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | .0002394 .000638 0.38 0.708 -.0010111 .0014899 _cons | -.0319396 .0632583 -0.50 0.614 -.1559236 .0920444 ------------------------------------------------------------------------------

The two display commands below show how we can extract out the logistic regression coefficient (b) and standard error (se).

. display _b[x1] .00023938 . display _se[x1] .00063801

We can get the OR and the standard error of the OR using the formulas below.

. display "OR is " exp(_b[x1]) OR is 1.0002394 . display "se of OR is " exp(_b[x1])*_se[x1] se of OR is .00063817

We can confirm the above computations using the logit, or commmand to show the results as odds ratios.

. logit, or Logistic regression Number of obs = 1000 LR chi2(1) = 0.14 Prob > chi2 = 0.7075 Log likelihood = -692.94877 Pseudo R2 = 0.0001 ------------------------------------------------------------------------------ y | Odds Ratio Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | 1.000239 .0006382 0.38 0.708 .9989894 1.001491 ------------------------------------------------------------------------------

Further, we can use the test x1 command followed by the display r(p) to extract the p value associated with the coefficient for x1 (which is the same as the p value associated with the odds ratio of x1).

. test x1 ( 1) [y]x1 = 0 chi2( 1) = 0.14 Prob > chi2 = 0.7075 . display r(p) .70751437

So, let's run 10 logistic regressions predicting y from x1 to x10, as shown below and save the results using post commands as we did in last weeks tidbit. The difference is that we do conversions to save the OR and se of the OR, otherwise this is much like the program from last week.

. use xy2 . postfile xylogistic str10 xvar or se p using xylogistic, replace . foreach x of varlist x1-x10 { 2. quietly logit y `x' 3. local or = exp(_b[`x']) 4. local se = exp(_b[`x'])*_se[`x'] 5. quietly test `x' 6. local p = r(p) 7. post xylogistic ("`x'") (`or') (`se') (`p') 8. } . postclose xylogistic

The results of these 10 logistic regressions are stored in xylogistic. We can see the results below.

. use xylogistic, clear . list +---------------------------------------+ | xvar or se p | |---------------------------------------| 1. | x1 1.000239 .0006382 .7075143 | 2. | x2 1.000946 .0006386 .1384921 | 3. | x3 .9993839 .0006497 .3431251 | 4. | x4 .9995413 .0006185 .4583591 | 5. | x5 .9994677 .0006425 .407521 | |---------------------------------------| 6. | x6 1.000009 .0006357 .9888354 | 7. | x7 .9996594 .0006392 .5941362 | 8. | x8 1.000424 .0006311 .5014659 | 9. | x9 1.001669 .0006439 .0094808 | 10. | x10 1.000292 .0006309 .6434368 | +---------------------------------------+

This now produces a simple summary of the results of the 10 logistic regression commands.

We can use this same strategy for the ologit command. This is shown below using the file xy3, in which the variable y takes on three values, 1, 2 or 3.

. use xy3 . postfile xyologit str10 xvar or se p using xyologit, replace . foreach x of varlist x1-x10 { 2. quietly ologit y `x' 3. local or = exp(_b[`x']) 4. local se = exp(_b[`x'])*_se[`x'] 5. quietly test `x' 6. local p = r(p) 7. post xyologit ("`x'") (`or') (`se') (`p') 8. } . postclose xyologit

The results of these 10 ordinal logistic regressions are stored in xyologit. We can see the results below.

. use xyologit, clear . list +---------------------------------------+ | xvar or se p | |---------------------------------------| 1. | x1 1.000448 .0005829 .4425391 | 2. | x2 1.000606 .0005822 .2981577 | 3. | x3 .999413 .0005987 .3270006 | 4. | x4 .9991411 .0005617 .1264248 | 5. | x5 .9998972 .0005895 .8615878 | |---------------------------------------| 6. | x6 .9996794 .0005759 .5777518 | 7. | x7 .9997442 .0005857 .6623428 | 8. | x8 .9996998 .0005743 .601226 | 9. | x9 .9999883 .0005837 .9839603 | 10. | x10 1.000136 .0005795 .8140498 | +---------------------------------------+

You can download the example data files from this tidbit (as well as all of the other tidbits) as shown below. These will download all of the example data files into the current folder on your computer. (If you have done this before, then you may need to specify net get stowdata, replace to overwrite the existing files.

net from http://www.MichaelNormanMitchell.com/storage/stowdata net get stowdata
If you have thoughts on this Stata Tidbit of the Week, you can post a comment. You can also send me an email at MichaelNormanMitchell and then the at sign and gmail dot com. If you are receiving this tidbit via email, you can find the web version at http://www.michaelnormanmitchell.com/ .

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.
Editor Permission Required
You must have editing permission for this entry in order to post comments.