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

st: RE: error suppression with by and ttest


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: error suppression with by and ttest
Date   Thu, 16 Jan 2003 19:19:39 -0000

Ray, Bryan
> 
> I am doing a simple difference of means test using the 
> ttest function. I
> want to perform the test for several different groups that 
> are defined by a
> variable called customer. However, for some customers the 
> test cannot be
> performed because one of the samples does not exist. When 
> this error is
> encountered the output is terminated. Is it possible to 
> somehow suppress the
> error so that the output will not terminate? See below for 
> the command that
> I am using and a snippet of the output. Thanks.
> 
> 
> by customer: ttest pos, by(dum)
> 
> <ouputt removed>
> 
> -> customer = Ten
> 1 group found, 2 required

One first stab is 

bysort customer (dum) : gen OK = dum[1] != dum[_N] 
by customer: ttest pos if OK, by(dum) 

This may be rendered less cryptic by looking at 
How do I list observations in a group that differ on a variable?
http://www.stata.com/support/faqs/data/diff.html

The potential problem with this is that it 
will fail to give the desired result if the values 
of -dum- for a customer are (1) all of one 
non-missing value apart from (2) those that are missing. 

This should a bit more robust (example assumes
values are 1 and 2):  

bysort customer : egen one = sum(dum == 1)
by customer : egen two = sum(dum == 2) 
by customer : ttest pos if one & two, by(dum) 

There are other ways to do it, no doubt. Note
that 

bysort customer : noisily capture ttest pos, by(dum) 

is not one of them. 

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