Does anyone have suggestions on how to make a more descriptive log file within a loop. For example, if you're running multiple regressions and you want to see the command used to generate the output of each regression, is there a way to log each iteration of the loop? Here's a more detailed example:
Here's the output of this script:
Code:
log using "example.txt", name(example) text replace
sysuse auto.dta
* Not that helpful in a log file
forvalues i = 0/1 {
regress mpg price if foreign == `i'
}
* More helpful in a log file
regress mpg price if foreign == 0
regress mpg price if foreign == 1
log close example
Code:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
name: example
log: example.txt
log type: text
opened on: 23 Dec 2022, 15:19:05
.
. sysuse auto.dta
(1978 automobile data)
.
. * Not that helpful in a log file
. forvalues i = 0/1 {
2. regress mpg price if foreign == `i'
3. }
Source | SS df MS Number of obs = 52
-------------+---------------------------------- F(1, 50) = 17.05
Model | 291.772809 1 291.772809 Prob > F = 0.0001
Residual | 855.669499 50 17.11339 R-squared = 0.2543
-------------+---------------------------------- Adj R-squared = 0.2394
Total | 1147.44231 51 22.4988688 Root MSE = 4.1368
------------------------------------------------------------------------------
mpg | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
price | -.0007723 .000187 -4.13 0.000 -.001148 -.0003966
_cons | 24.51661 1.272427 19.27 0.000 21.96086 27.07235
------------------------------------------------------------------------------
Source | SS df MS Number of obs = 22
-------------+---------------------------------- F(1, 20) = 13.25
Model | 365.808088 1 365.808088 Prob > F = 0.0016
Residual | 552.055549 20 27.6027774 R-squared = 0.3985
-------------+---------------------------------- Adj R-squared = 0.3685
Total | 917.863636 21 43.7077922 Root MSE = 5.2538
------------------------------------------------------------------------------
mpg | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
price | -.0015918 .0004373 -3.64 0.002 -.002504 -.0006797
_cons | 34.9361 3.008144 11.61 0.000 28.66122 41.21097
------------------------------------------------------------------------------
.
. * More helpful in a log file
. regress mpg price if foreign == 0
Source | SS df MS Number of obs = 52
-------------+---------------------------------- F(1, 50) = 17.05
Model | 291.772809 1 291.772809 Prob > F = 0.0001
Residual | 855.669499 50 17.11339 R-squared = 0.2543
-------------+---------------------------------- Adj R-squared = 0.2394
Total | 1147.44231 51 22.4988688 Root MSE = 4.1368
------------------------------------------------------------------------------
mpg | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
price | -.0007723 .000187 -4.13 0.000 -.001148 -.0003966
_cons | 24.51661 1.272427 19.27 0.000 21.96086 27.07235
------------------------------------------------------------------------------
. regress mpg price if foreign == 1
Source | SS df MS Number of obs = 22
-------------+---------------------------------- F(1, 20) = 13.25
Model | 365.808088 1 365.808088 Prob > F = 0.0016
Residual | 552.055549 20 27.6027774 R-squared = 0.3985
-------------+---------------------------------- Adj R-squared = 0.3685
Total | 917.863636 21 43.7077922 Root MSE = 5.2538
------------------------------------------------------------------------------
mpg | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
price | -.0015918 .0004373 -3.64 0.002 -.002504 -.0006797
_cons | 34.9361 3.008144 11.61 0.000 28.66122 41.21097
------------------------------------------------------------------------------
.
. log close example
name: example
log: example.txt
log type: text
closed on: 23 Dec 2022, 15:19:05
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Comment