Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: no stop option


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   Re: st: no stop option
Date   Thu, 16 Oct 2003 10:42:51 +0100

Yong-Seok Choi
 
> Here is my problem for which I need your help. (I'm using STATA8.0)
> Simplified form of the data look like...
> 
> id   group  var1   var2   var3  var4 .........
> 1      a         1        .       3
> 2      a         3        .       2
> 3      b         1        2      4
> 4      b         2        1      5
> 
> I would like to do the mean-difference test for each 
> variables by group.
> So I make the do file containing
> 
> local i = 1
> while `i' <= 3 {
> ttest var`i', by(group)
> local i = `i' + 1
> }
> 
> Then, ttest for var1 is ok, but with var2 STATA makes error 
> "r(420) 1group found, 2 required" (because of data missing) 
> and stops without ttesting for var3.
> Is there any way that I can get the result of ttest for 
> var1 and var3 without being stopped at var2?
> 
> PS. I need to have the ttest results for var1 and var3, so 
> I cannot use "capture" command.

As Anthony Gichangi pointed out 

> You can use capture command to be able to extract 
> the results that are possible to compute. 
> An the recompute your ttest for valid varlists
> like this

> local i = 1
> while `i' <= 3 {
> cap ttest var`i', by(group)
> if _rc==0 {
> ttest var`i', by(group)
> }
> local i = `i' + 1
> }

> You get results for Var1 and Var2

First, let's take Anthony's solution, modifying 
some details to see variations on the theme. 

The -while- loop can be rewritten as a -forval-
loop 

forval i = 1/3 { 
	cap ttest var`i', by(group) 
	if _rc == 0 ttest var`i', by(group) 
} 

and in fact the middle can be combined in 
this way 

forval i = 1/3 { 
	cap noisily ttest var`i', by(group)
} 

Here the -capture- catches the problem
variables, and the -noisily- ensures that 
we see valid output when we get it. 

A more general case, closer to what is 
presumably Yong-Seok Choi's real problem, is to 
make this into a -foreach- loop over 
variables: 

foreach v of var var* { 
	cap noisily ttest `v', by(group) 
} 

However, minimalism has its cost, and you 
might want to decorate this a bit to make 
the output more readable: 

foreach v of var var* { 
	di "{res:-> `v'}" 
	cap noisily ttest `v', by(group) 
	di " " 
} 

We get a -by:- type line before and a blank 
line after each block of results. 

Second, another way would be to do the t test 
if and only if there are two groups to play 
with: 

foreach v of var var* { 
	qui tab group if !mi(`v') 
	if r(r) == 2 { 
		di "{res:-> `v'}" 
		ttest `v', by(group) 
		di " " 
	}
} 

"!mi(`v')" means "not missing on `v'". 
"if `v' < ." would be an alternative. 

This hinges on the fact that -tabulate- 
leaves r(r) behind in memory, irrespective
of whether we see the result, which contains
the number of groups (_r_ows in the table). 

Nick 
[email protected] 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index