Nick,
Thanks so much for your advice. Your second (sequel) approach mentioned at
the bottom of this message works fine. For folks like me, this assistance is
a KEY reason why I chose and stick with Stata.
My attempts to use your first approach, immediately below, yielded an error
message as you can see.
chitest rep78, count
count invalid
r(198);
But I have been successful using your second approach; it took me a while
since this is my first experience with macros and multi-step commands
like -forvalues-. But this was bonus learning for me.
As you suggested, I added a couple of things and now have a do file [listed
below] (with arguments) that has been working fine, especially since I have
no data with '0' observed values.
DO FILE
.do chitest SEX "& racewbh==3 & csample==1"
.forval i = 0/5 {
2. qui count if `1'== `i' `2'
3. if r(N)==0 {
4. continue
5. }
6. local counts "`counts' `r(N)'"
7. }
. di "`counts'
950 767
. chitesti `counts'
Chi-square test:
observed frequencies from keyboard
expected frequencies equal
Pearson chi2(1) = 19.5044 Pr = 0.000
likelihood-ratio chi2(1) = 19.5415 Pr = 0.000
residuals
observed expected classic Pearson
1. 950 858.500 91.500 3.123
2. 767 858.500 -91.500 -3.123
. macro drop _counts
. end of do-file
----- Original Message -----
From: "Nick Cox" <[email protected]>
To: <[email protected]>
Sent: Thursday, January 30, 2003 5:31 AM
Subject: st: RE: Re: RE: one-sample chi square test
> Wendell Joice
>
> > -chitesti- works fine; I need to tabulate the variables of
> > interest and then key in the frequencies to -chitesti-.
> >
> > Perchance, however, is there a command (other than and not
> > as elaborate
> > as -collapse-), that will produce a variable B containing
> > the frequencies of
> > the values in variable A? That way, I can place variable B directly
> > into -chitest- .
> >
> > Unless, of course, you know of a one-sample test that will
> > carry out the
> > entire process of calculating the frequencies of the
> > values of a specified
> > categorical variable and then provide a one-sample
> > chi-square over those
> > frequencies? (Analogous to -tabulate- for two sample chi square)
> >
>
> -chitest- is able to do this for you. Here is
> a silly example for the simplest case of equal
> expected frequencies.
>
> . chitest rep78, count
>
> Chi-square test:
> observed frequencies from rep78
> expected frequencies equal
>
> Pearson chi2(4) = 33.3913 Pr = 0.000
> likelihood-ratio chi2(4) = 34.7183 Pr = 0.000
>
> residuals
> +-----------------------------------------+
> | observed expected classic Pearson |
> |-----------------------------------------|
> 1. | 2 13.800 -11.800 -3.176 |
> 2. | 8 13.800 -5.800 -1.561 |
> 3. | 30 13.800 16.200 4.361 |
> 4. | 18 13.800 4.200 1.131 |
> 5. | 11 13.800 -2.800 -0.754 |
> +-----------------------------------------+
>
> What -chitest- can't do is count values which
> could be there in principle but which in
> practice don't occur (cells with observed 0,
> in short).
>
> I suspect there are other problems, but at worst
> you need to type a few numbers at -chitesti-.
>
> If this doesn't solve the problem, please give
> a clear example of the data structure you have in
> mind.
>
> Nick
SECOND APPROACH
As a sequel to my previous reply, here is one technique
illustrated:
. forval i = 1/5 {
2. qui count if rep78 == `i'
3. local counts "`counts' `r(N)'"
4. }
. di "`counts'
2 8 30 18 11
. chitesti `counts'
One advantage of this technique is that
will work fine when observed frequencies
are 0. You just have to use -forvalues-
or -foreach- to cycle over the possible
values of a variable relevant to your
test.
This could also be automated in a program.
Nick *
*
* 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/