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: Logistic and Ordinal Logistic | Always starting a log »
Tuesday
Mar232010

Posting results: Regression

This tidbit introduces how you can post results from estimation commands, focusing on the regress command. To make things simple, this tidbit focuses just on the basics.

These examples will use the dataset named xy. Let's look at a simple regression predicting y from x1.

. use xy . regress y x1 Source | SS df MS Number of obs = 1000 -------------+------------------------------ F( 1, 998) = 1.43 Model | 15505.8385 1 15505.8385 Prob > F = 0.2329 Residual | 10859270.1 998 10881.0321 R-squared = 0.0014 -------------+------------------------------ Adj R-squared = 0.0004 Total | 10874775.9 999 10885.6616 Root MSE = 104.31 ------------------------------------------------------------------------------ y | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | .0397098 .0332648 1.19 0.233 -.0255672 .1049869 _cons | 2.921238 3.298654 0.89 0.376 -3.551856 9.394332 ------------------------------------------------------------------------------

The two display commands below show how we can extract out the regression coefficient (b) and standard error (se) for predicting y from x1.

. display _b[x1] .03970984 . display _se[x1] .03326485

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.

. test x1 ( 1) x1 = 0 F( 1, 998) = 1.43 Prob > F = 0.2329 . display r(p) .23286031

We can then use the postfile, post and postclose commands to save these values into a Stata dataset. There are three steps, as shown below.

. use xy . . * step 1 - create the empty file xyresults1.dta . postfile anything b se p using xyresults1, replace . * step 2a - run the regression and save the results . regress y x1 Source | SS df MS Number of obs = 1000 -------------+------------------------------ F( 1, 998) = 1.43 Model | 15505.8385 1 15505.8385 Prob > F = 0.2329 Residual | 10859270.1 998 10881.0321 R-squared = 0.0014 -------------+------------------------------ Adj R-squared = 0.0004 Total | 10874775.9 999 10885.6616 Root MSE = 104.31 ------------------------------------------------------------------------------ y | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | .0397098 .0332648 1.19 0.233 -.0255672 .1049869 _cons | 2.921238 3.298654 0.89 0.376 -3.551856 9.394332 ------------------------------------------------------------------------------ . local b = _b[x1] . local se = _b[x1] . test x1 ( 1) x1 = 0 F( 1, 998) = 1.43 Prob > F = 0.2329 . local p = r(p) . * step 2b - post the results . post anything (`b') (`se') (`p') . * step 3 - close the postfile . postclose anything . * step 4 - Show the results . use xyresults1, clear . list +--------------------------------+ | b se p | |--------------------------------| 1. | .0397098 .0397098 .2328603 | +--------------------------------+

The three steps are repeated and dissected below.

. use xy . * step 1 - create the empty file xyresults1.dta . postfile anything b se p using xyresults1, replace

This step creates the post file xyresults1.dta. It has zero observations but contains the variables b, se and p. We can see this file with the describe command shown below.

. describe using xyresults1 Contains data obs: 0 21 Mar 2010 16:04 vars: 3 size: 0 ------------------------------------------------------------------------------- storage display value variable name type format label variable label ---------------------------------------------------------------------------------------------------- b float %9.0g se float %9.0g p float %9.0g ------------------------------------------------------------------------------- Sorted by:

Now, let's look at the second step.

. * step 2a - run the regression and save the results . regress y x1 Source | SS df MS Number of obs = 1000 -------------+------------------------------ F( 1, 998) = 1.43 Model | 15505.8385 1 15505.8385 Prob > F = 0.2329 Residual | 10859270.1 998 10881.0321 R-squared = 0.0014 -------------+------------------------------ Adj R-squared = 0.0004 Total | 10874775.9 999 10885.6616 Root MSE = 104.31 ------------------------------------------------------------------------------ y | Coef. Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | .0397098 .0332648 1.19 0.233 -.0255672 .1049869 _cons | 2.921238 3.298654 0.89 0.376 -3.551856 9.394332 ------------------------------------------------------------------------------ . local b = _b[x1] . local se = _se[x1] . test x1 ( 1) x1 = 0 F( 1, 998) = 1.43 Prob > F = 0.2329 . local p = r(p) . * step 2b - post the results . post anything (`b') (`se') (`p')

In this step, the regress command is run and then the post command is used to save the value of the b, se, and p into the post file. Two things to note. First, the order of b, se, and p needs to match those specified on the postfile command. Second, the post command refers to anything which is the name that was specified on the postfile command. If the postfile command specified abc in place of anything, then the post command should refer to abc.

Now let's look at the third step

. * step 3 - close the postfile . postclose anything

This closes the postfile. Again, note how the postclose command refers to anything, because that is the name that was given in the postfile command.

Finally, we look at the results stored in the postfile. In this case, we refer directly to the name of the Stata dataset, xyresults1.

. * step 4 - Show the results . use xyresults1, clear . list +--------------------------------+ | b se p | |--------------------------------| 1. | .0397098 .0332648 .2328603 | +--------------------------------+

Now that we have the hang of this, let's do something more useful. Let's run 100 regressions predicting y from x1 to x100, as shown below. Note how we additionally save a variable named xvar which contains the name of the x variable.

. use xy . postfile abc str10 xvar b se p using xyresults2, replace . foreach x of varlist x1-x100 { 2. quietly regress y `x' 3. local b = _b[`x'] 4. local se = _se[`x'] 5. quietly test `x' 6. local p = r(p) 7. post abc ("`x'") (`b') (`se') (`p') 8. } . postclose abc

The results of these 100 regressions are stored in xyresult2.dta. We can see the results below.

. use xyresults2, clear . list +----------------------------------------+ | xvar b se p | |----------------------------------------| 1. | x1 .0397098 .0332648 .2328603 | 2. | x2 .0334058 .0331735 .3141779 | 3. | x3 -.0301979 .0338641 .3727483 | 4. | x4 -.0476022 .032228 .1399793 | 5. | x5 .0018482 .0335124 .9560308 | |----------------------------------------| 6. | x6 -.0093772 .0331742 .7774918 | 7. | x7 -.0082713 .0333563 .8042111 | 8. | x8 -.0065389 .0329 .8424978 | 9. | x9 .0169392 .0332035 .6100498 | 10. | x10 .0175903 .0329017 .5930225 | |----------------------------------------| 11. | x11 -.015198 .0324805 .6399506 | 12. | x12 .015636 .0336187 .6419622 | 13. | x13 .0323721 .0330926 .3281993 | 14. | x14 -.0231605 .0338074 .4934581 | 15. | x15 -.0648239 .0325613 .0467728 | |----------------------------------------| 16. | x16 .0657252 .0322756 .0419766 | 17. | x17 -.0181394 .0329624 .582233 | 18. | x18 -.0194358 .0332124 .5585471 | 19. | x19 -.0234693 .0323054 .4677149 | 20. | x20 .0118172 .032358 .7150392 | |----------------------------------------| 21. | x21 -.0250117 .0344311 .4677474 | 22. | x22 -.0199562 .0325901 .5404531 | 23. | x23 .0318358 .032708 .3306225 | 24. | x24 -.0115636 .0332936 .7284217 | 25. | x25 -.0518721 .0338858 .1261381 | |----------------------------------------| 26. | x26 -.0340167 .0331866 .3056059 | 27. | x27 -.0077389 .0328435 .8137681 | 28. | x28 .0068168 .0342228 .8421553 | 29. | x29 .0374444 .0320244 .2425836 | 30. | x30 -.018622 .0330229 .5729399 | |----------------------------------------| 31. | x31 -.0151392 .0338834 .6551135 | 32. | x32 -.0289142 .0331142 .3827825 | 33. | x33 .0039586 .0334651 .9058611 | 34. | x34 -.0017908 .0334633 .9573323 | 35. | x35 -.0557673 .0333651 .0949506 | |----------------------------------------| 36. | x36 .0452721 .0335712 .1777907 | 37. | x37 -.0397043 .0334503 .235524 | 38. | x38 .0042338 .0322152 .8954685 | 39. | x39 .0046227 .031712 .8841314 | 40. | x40 .0390124 .0345339 .2588807 | |----------------------------------------| 41. | x41 .0242861 .0326666 .4573825 | 42. | x42 .0085724 .0328216 .7940066 | 43. | x43 -.030186 .0328965 .3590479 | 44. | x44 .056413 .0341162 .0985324 | 45. | x45 .0051521 .0321642 .8727705 | |----------------------------------------| 46. | x46 -.0225197 .0329684 .4947214 | 47. | x47 -.0114251 .033668 .7344197 | 48. | x48 .0130566 .0329525 .6920244 | 49. | x49 .0151789 .0338704 .6541447 | 50. | x50 -.0188491 .0319606 .5554841 | |----------------------------------------| 51. | x51 .0195093 .0335168 .5606483 | 52. | x52 .0184728 .0329199 .5748248 | 53. | x53 .0113336 .0328838 .7304267 | 54. | x54 -.0595445 .033921 .0794995 | 55. | x55 .0387227 .0326868 .2364347 | |----------------------------------------| 56. | x56 -.0100061 .0337787 .7671202 | 57. | x57 .0118071 .0338538 .7273371 | 58. | x58 .0212494 .0330234 .5200698 | 59. | x59 -.0710336 .0323363 .0282704 | 60. | x60 -.018764 .0340923 .5821761 | |----------------------------------------| 61. | x61 .0027353 .0345876 .9369823 | 62. | x62 .0135402 .032791 .6797507 | 63. | x63 .0876803 .0332958 .0085848 | 64. | x64 .0441164 .0331545 .1836149 | 65. | x65 -.0145597 .0342071 .6704666 | |----------------------------------------| 66. | x66 .0249142 .0336884 .4597483 | 67. | x67 .0267071 .0348938 .4442241 | 68. | x68 -.0108167 .032392 .7385029 | 69. | x69 .014612 .0335588 .6633561 | 70. | x70 .059107 .0332778 .0760094 | |----------------------------------------| 71. | x71 .0615522 .0330189 .062594 | 72. | x72 -.0144563 .0317238 .6487099 | 73. | x73 .0659707 .0337294 .0507576 | 74. | x74 -.0174617 .0336447 .6038727 | 75. | x75 -.0183615 .0340873 .5902413 | |----------------------------------------| 76. | x76 -.0122445 .0326835 .708009 | 77. | x77 -.0149046 .032707 .6487051 | 78. | x78 .0044065 .0326479 .8926615 | 79. | x79 -.0416902 .0336802 .2160721 | 80. | x80 -.0031531 .0326521 .9230906 | |----------------------------------------| 81. | x81 .0136838 .0342138 .6892778 | 82. | x82 .0344681 .0330064 .2966062 | 83. | x83 .001485 .0328 .9638976 | 84. | x84 -.0608335 .0316533 .0549064 | 85. | x85 -.0290916 .0329286 .3771915 | |----------------------------------------| 86. | x86 -.0111502 .0339515 .7426651 | 87. | x87 -.0230264 .0332722 .4890592 | 88. | x88 .0947912 .0337496 .0050719 | 89. | x89 -.0232496 .033228 .4842786 | 90. | x90 .0207071 .0342625 .5457375 | |----------------------------------------| 91. | x91 -.0231259 .0332872 .4873807 | 92. | x92 .0235007 .033334 .4809708 | 93. | x93 .0078037 .0339568 .8182854 | 94. | x94 -.0113803 .0333331 .7328657 | 95. | x95 -.0658523 .033239 .047846 | |----------------------------------------| 96. | x96 .0036719 .0326262 .9104145 | 97. | x97 .0497483 .0329796 .1317542 | 98. | x98 .0300887 .0335537 .3700775 | 99. | x99 -.0113786 .0342688 .7399289 | 100. | x100 -.056091 .0331682 .0911281 | +----------------------------------------+

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

Let's do this again, but create two different datasets. One dataset would contain the significant results (p less than or equal to 0.05) named sig.dta and the other would contain the non-significant results (p greater than 0.05) named nonsig.dta

. use xy . postfile sig str10 xvar b se p using xysig, replace . postfile nonsig str10 xvar b se p using xynonsig, replace . foreach x of varlist x1-x100 { 2. quietly regress y `x' 3. local b = _b[`x'] 4. local se = _se[`x'] 5. quietly test `x' 6. local p = r(p) 7. if (`p' <= 0.05) { 8. post sig ("`x'") (`b') (`se') (`p') 9. } 10. else { 11. post nonsig ("`x'") (`b') (`se') (`p') 12. } 13. } . postclose sig . postclose nonsig

Now, the dataset xysig contains the results of the significant results.

. use xysig, clear . list +----------------------------------------+ | xvar b se p | |----------------------------------------| 1. | x15 -.0648239 .0325613 .0467728 | 2. | x16 .0657252 .0322756 .0419766 | 3. | x59 -.0710336 .0323363 .0282704 | 4. | x63 .0876803 .0332958 .0085848 | 5. | x88 .0947912 .0337496 .0050719 | |----------------------------------------| 6. | x95 -.0658523 .033239 .047846 | +----------------------------------------+

And the dataset xynonsig.dta contains the non-significant results.

. use xynonsig, clear . list +----------------------------------------+ | xvar b se p | |----------------------------------------| 1. | x1 .0397098 .0332648 .2328603 | 2. | x2 .0334058 .0331735 .3141779 | 3. | x3 -.0301979 .0338641 .3727483 | 4. | x4 -.0476022 .032228 .1399793 | 5. | x5 .0018482 .0335124 .9560308 | |----------------------------------------| 6. | x6 -.0093772 .0331742 .7774918 | 7. | x7 -.0082713 .0333563 .8042111 | 8. | x8 -.0065389 .0329 .8424978 | 9. | x9 .0169392 .0332035 .6100498 | 10. | x10 .0175903 .0329017 .5930225 | |----------------------------------------| 11. | x11 -.015198 .0324805 .6399506 | 12. | x12 .015636 .0336187 .6419622 | 13. | x13 .0323721 .0330926 .3281993 | 14. | x14 -.0231605 .0338074 .4934581 | 15. | x17 -.0181394 .0329624 .582233 | |----------------------------------------| 16. | x18 -.0194358 .0332124 .5585471 | 17. | x19 -.0234693 .0323054 .4677149 | 18. | x20 .0118172 .032358 .7150392 | 19. | x21 -.0250117 .0344311 .4677474 | 20. | x22 -.0199562 .0325901 .5404531 | |----------------------------------------| 21. | x23 .0318358 .032708 .3306225 | 22. | x24 -.0115636 .0332936 .7284217 | 23. | x25 -.0518721 .0338858 .1261381 | 24. | x26 -.0340167 .0331866 .3056059 | 25. | x27 -.0077389 .0328435 .8137681 | |----------------------------------------| 26. | x28 .0068168 .0342228 .8421553 | 27. | x29 .0374444 .0320244 .2425836 | 28. | x30 -.018622 .0330229 .5729399 | 29. | x31 -.0151392 .0338834 .6551135 | 30. | x32 -.0289142 .0331142 .3827825 | |----------------------------------------| 31. | x33 .0039586 .0334651 .9058611 | 32. | x34 -.0017908 .0334633 .9573323 | 33. | x35 -.0557673 .0333651 .0949506 | 34. | x36 .0452721 .0335712 .1777907 | 35. | x37 -.0397043 .0334503 .235524 | |----------------------------------------| 36. | x38 .0042338 .0322152 .8954685 | 37. | x39 .0046227 .031712 .8841314 | 38. | x40 .0390124 .0345339 .2588807 | 39. | x41 .0242861 .0326666 .4573825 | 40. | x42 .0085724 .0328216 .7940066 | |----------------------------------------| 41. | x43 -.030186 .0328965 .3590479 | 42. | x44 .056413 .0341162 .0985324 | 43. | x45 .0051521 .0321642 .8727705 | 44. | x46 -.0225197 .0329684 .4947214 | 45. | x47 -.0114251 .033668 .7344197 | |----------------------------------------| 46. | x48 .0130566 .0329525 .6920244 | 47. | x49 .0151789 .0338704 .6541447 | 48. | x50 -.0188491 .0319606 .5554841 | 49. | x51 .0195093 .0335168 .5606483 | 50. | x52 .0184728 .0329199 .5748248 | |----------------------------------------| 51. | x53 .0113336 .0328838 .7304267 | 52. | x54 -.0595445 .033921 .0794995 | 53. | x55 .0387227 .0326868 .2364347 | 54. | x56 -.0100061 .0337787 .7671202 | 55. | x57 .0118071 .0338538 .7273371 | |----------------------------------------| 56. | x58 .0212494 .0330234 .5200698 | 57. | x60 -.018764 .0340923 .5821761 | 58. | x61 .0027353 .0345876 .9369823 | 59. | x62 .0135402 .032791 .6797507 | 60. | x64 .0441164 .0331545 .1836149 | |----------------------------------------| 61. | x65 -.0145597 .0342071 .6704666 | 62. | x66 .0249142 .0336884 .4597483 | 63. | x67 .0267071 .0348938 .4442241 | 64. | x68 -.0108167 .032392 .7385029 | 65. | x69 .014612 .0335588 .6633561 | |----------------------------------------| 66. | x70 .059107 .0332778 .0760094 | 67. | x71 .0615522 .0330189 .062594 | 68. | x72 -.0144563 .0317238 .6487099 | 69. | x73 .0659707 .0337294 .0507576 | 70. | x74 -.0174617 .0336447 .6038727 | |----------------------------------------| 71. | x75 -.0183615 .0340873 .5902413 | 72. | x76 -.0122445 .0326835 .708009 | 73. | x77 -.0149046 .032707 .6487051 | 74. | x78 .0044065 .0326479 .8926615 | 75. | x79 -.0416902 .0336802 .2160721 | |----------------------------------------| 76. | x80 -.0031531 .0326521 .9230906 | 77. | x81 .0136838 .0342138 .6892778 | 78. | x82 .0344681 .0330064 .2966062 | 79. | x83 .001485 .0328 .9638976 | 80. | x84 -.0608335 .0316533 .0549064 | |----------------------------------------| 81. | x85 -.0290916 .0329286 .3771915 | 82. | x86 -.0111502 .0339515 .7426651 | 83. | x87 -.0230264 .0332722 .4890592 | 84. | x89 -.0232496 .033228 .4842786 | 85. | x90 .0207071 .0342625 .5457375 | |----------------------------------------| 86. | x91 -.0231259 .0332872 .4873807 | 87. | x92 .0235007 .033334 .4809708 | 88. | x93 .0078037 .0339568 .8182854 | 89. | x94 -.0113803 .0333331 .7328657 | 90. | x96 .0036719 .0326262 .9104145 | |----------------------------------------| 91. | x97 .0497483 .0329796 .1317542 | 92. | x98 .0300887 .0335537 .3700775 | 93. | x99 -.0113786 .0342688 .7399289 | 94. | x100 -.056091 .0331682 .0911281 | +----------------------------------------+

This covers the basics of how you can use the postfile, post, and postclose commands to save the results of estimation commands.

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 (4)

Great tidbit! One question: is there a way to control the numeric display (e.g. decimal points) before it gets written to the postfile?

June 16, 2010 | Unregistered CommenterThomas

Dear Thomas

Rather than changing the numeric display before writing to the postfile, perhaps it would be better to change the numeric display when listing out the results from the postfile. That way, the postfile retains the full precision of the results, but then we can display the results to any level of precision we desire. For example we could use the commands format b se %52.f and format %5.3f p before the list command.

Thanks for the comment!

Michael Mitchell

June 16, 2010 | Registered CommenterMichael Mitchell

I really love your tidbits. Best thing on-line I've seen in months. Thank you very much!

July 19, 2010 | Unregistered CommenterPatrick

Thanks Patrick! I enjoy sharing these tidbits very much.

July 20, 2010 | Registered CommenterMichael Mitchell
Editor Permission Required
You must have editing permission for this entry in order to post comments.