Labeling Data in Loops

Labeling Data in Loops

by Meghan McGarry -
Number of replies: 2

I've created a loop to analyze my data but then the t-tests come out unlabeled.  Is there any way to have each ttest labeled with what was analyzed using a loop? I've labeled all the data but it doesn't show the "delta1 delta2 fev1fvcpred prefev1percpred" at all.

Thanks! Meghan

My loop..

foreach var in delta1 delta2 fev1fvcpred prefev1percpred  {

*smoking variables

ttest `var', by (preg_smoke_1st)

ttest `var', by (preg_smoke_2nd)

ttest `var', by (preg_smoke_3rd)

ttest `var', by (adults_smoke_u2yrs)

ttest `var', by (adults_smoke_3_6yrs)

ttest `var', by (adults_smoke_7plsyrs)

ttest `var', by (smoke_current_mother)

ttest `var', by (smoke_current_father)

ttest `var', by (child_smoke_ever)

ttest `var', by (underweight)

ttest `var', by (daycare)

ttest `var', by (underweight)

ttest `var', by (underweight) 

}

 

In reply to Meghan McGarry

Re: Labeling Data in Loops

by Elizabeth Uy-Smith -

Hello Meghan,

Thanks for your excellent question. In my limited knowledge of loops, I don't think there's a way to program a label for the looped t-tests. I attempted a loop using the Coronary Calcium data (see Stata output below), but also came out with unlabeled t-tests. But, I'll ask other folks if they have other suggestions for programming the loop to have labels. 

Thanks!

ellie

 

----------

here's my Stata output using the loop in the Coronary Calcium dataset: 

. foreach x in htn dm smoke {
2. ttest age, by(`x')
3. }

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

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

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

 

In reply to Meghan McGarry

Re: Labeling Data in Loops

by Elizabeth Uy-Smith -

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