Labeling Data in Loops

Re: Labeling Data in Loops

by Elizabeth Uy-Smith -
Number of replies: 0

Hello Meghan,

Thanks to Mark, we've figured it out! We need to add an additional command within the start of the loop to tell Stata to display the variable you're using in the t-test. Below is what it looks like using the Coronary Calcium dataset. Let us know if you have more issues!

ellie

------

. foreach x in htn dm smoke {

2. display ""
3. display "`x'"
4. ttest age, by(`x')
5. }

htn

Two-sample t test with equal variances
------------------------------------------------------------------------------
Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
---------+--------------------------------------------------------------------
0 | 5852 51.98257 .1344378 10.28427 51.71902 52.24612
1 | 4520 55.0635 .1544737 10.38541 54.76065 55.36634
---------+--------------------------------------------------------------------
combined | 10372 53.3252 .1025142 10.44036 53.12425 53.52615
---------+--------------------------------------------------------------------
diff | -3.080926 .2045247 -3.481833 -2.680018
------------------------------------------------------------------------------
diff = mean(0) - mean(1) t = -15.0638
Ho: diff = 0 degrees of freedom = 10370

Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000

dm

Two-sample t test with equal variances
------------------------------------------------------------------------------
Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
---------+--------------------------------------------------------------------
0 | 9468 53.01658 .1068942 10.4012 52.80705 53.22612
1 | 902 56.56098 .3435351 10.3175 55.88675 57.2352
---------+--------------------------------------------------------------------
combined | 10370 53.32488 .1025337 10.44134 53.12389 53.52587
---------+--------------------------------------------------------------------
diff | -3.544393 .362191 -4.254358 -2.834429
------------------------------------------------------------------------------
diff = mean(0) - mean(1) t = -9.7860
Ho: diff = 0 degrees of freedom = 10368

Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000

smoke

Two-sample t test with equal variances
------------------------------------------------------------------------------
Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval]
---------+--------------------------------------------------------------------
0 | 6262 53.52156 .1360509 10.76609 53.25485 53.78827
1 | 4110 53.02603 .1546945 9.917354 52.72275 53.32932
---------+--------------------------------------------------------------------
combined | 10372 53.3252 .1025142 10.44036 53.12425 53.52615
---------+--------------------------------------------------------------------
diff | .4955245 .209543 .08478 .9062691
------------------------------------------------------------------------------
diff = mean(0) - mean(1) t = 2.3648
Ho: diff = 0 degrees of freedom = 10370

Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
Pr(T < t) = 0.9910 Pr(|T| > |t|) = 0.0181 Pr(T > t) = 0.0090

.
end of do-file