Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: graph results of bitest stratified with by(var)


From   Michael McCulloch <[email protected]>
To   [email protected]
Subject   Re: st: graph results of bitest stratified with by(var)
Date   Fri, 24 Jan 2014 07:49:14 -0800

Nick, apologies for veering off track.
The -statsby- suggestion works well, indeed with more levels as I've tested below:

sysuse auto.dta, clear
gen group="a" if price<5000
replace group="b" if price>=7000&price<10000
replace group="c" if price>=10000
bitest foreign=.2 if group=="a"
bitest foreign=.2 if group=="b"
statsby, clear by(group): bitest foreign=.2
l group N p, clean noobs

Many thanks.


Best wishes,
Michael McCulloch

--
Pine Street Foundation, since 1989
124 Pine Street | San Anselmo | California | 94960-2674  
P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org

On Jan 24, 2014, at 6:20 AM, Nick Cox wrote:

> <>
> 
> This is triply mysterious:
> 
> 1. You refer to -svret- and -tabout-, but these are user-written
> commands (both from SSC), as you are asked to explain.
> 
> 2. On the face of it, -statsby- is a superior solution for saving
> results groupwise, as -svret- saves only the last set of results and
> so it use would necessitate reading your data in again.
> 
> 3. It is not correct that  -svret- saves only the first values.
> Compare these results, which show that once the loop over groups
> is completed, the results saved are those for the last (second) values
> calculated, as should be expected: each time around the loop, previous
> r-class results are overwritten.
> 
> . sysuse auto.dta, clear
> (1978 Automobile Data)
> .  gen group=1 if price<5000
> (37 missing values generated)
> .  replace group=2 if price>=5000
> (37 real changes made)
> .  levelsof group, local(levels)
> 1 2
> .          foreach l of local levels {
>  2.          bitest foreign=.2 if group == `l'
>  3.          }
>    Variable |        N   Observed k   Expected k   Assumed p   Observed p
> -------------+------------------------------------------------------------
>     foreign |       37          8          7.4       0.20000      0.21622
>  Pr(k >= 8)           = 0.467034  (one-sided test)
>  Pr(k <= 8)           = 0.685908  (one-sided test)
>  Pr(k <= 6 or k >= 8) = 0.836862  (two-sided test)
>    Variable |        N   Observed k   Expected k   Assumed p   Observed p
> -------------+------------------------------------------------------------
>     foreign |       37         14          7.4       0.20000      0.37838
>  Pr(k >= 14)           = 0.009277  (one-sided test)
>  Pr(k <= 14)           = 0.996629  (one-sided test)
>  Pr(k <= 1 or k >= 14) = 0.011938  (two-sided test)
> .  svret r, format(%8.2f)
> .  l
>     +----------------------------------------------------+
>     | r_k_opp    r_p   r_k   r_P_p   r_N   r_p_l   r_p_u |
>     |----------------------------------------------------|
>  1. |       1   0.01    14    0.20    37    1.00    0.01 |
>     +----------------------------------------------------+
> 
> I can't advise on exact syntax for -tabout-, which I don't use.
> 
> 
> Nick
> [email protected]
> 
> 
> On 24 January 2014 07:09, Michael McCulloch <[email protected]> wrote:
>> Thanks Nick & Richard,
>> Here's a shot at using -statsby-  that gets me what I want.
>> 
>> sysuse auto.dta, clear
>> gen group="a" if price<5000
>> replace group="b" if price>=5000
>> bitest foreign=.2 if group=="a"
>> bitest foreign=.2 if group=="b"
>> statsby, clear by(group): bitest foreign=.2
>> l group N p, clean noobs
>> 
>> 
>> Then I tried:
>> 
>> sysuse auto.dta, clear
>> gen group=1 if price<5000
>> replace group=2 if price>=5000
>> levelsof group, local(levels)
>>        foreach l of local levels {
>>        bitest foreign=.2 if group == `l'
>>        }
>> svret r, format(%8.2f)
>> l
>> 
>> I have two questions:
>> 
>> 1. Why in this -svret- attempt does the list return only the first value of group?
>> 2. Is it possible to use -tabout- to make the list into a nice table showing both values of group?
>> 
>> 
>> Best wishes,
>> Michael McCulloch
>> 
>> --
>> Pine Street Foundation, since 1989
>> 124 Pine Street | San Anselmo | California | 94960-2674
>> P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org
>> 
>> On Jan 23, 2014, at 10:33 AM, Richard Goldstein wrote:
>> 
>>> true ;-)
>>> 
>>> On 1/23/14, 1:22 PM, Nick Cox wrote:
>>>> I see, but you can just calculate that from the -statsby- results.
>>>> 
>>>> Nick
>>>> [email protected]
>>>> 
>>>> 
>>>> On 23 January 2014 18:19, Richard Goldstein <[email protected]> wrote:
>>>>> 1. Michael, if you look at -h levelsof- there is an example showing its
>>>>> use with -foreach-
>>>>> 
>>>>> 2. Nick, I did not suggest -statsby- because Michael asked for something
>>>>> that is not in the return list (r(k)/r(N)) and I don't think that this
>>>>> is allowed with -statsby-, but maybe I'm wrong about that?
>>>>> 
>>>>> Rich
>>>>> 
>>>>> On 1/23/14, 1:07 PM, Nick Cox wrote:
>>>>>> -search foreach- does exactly what you ask, namely point to sources on
>>>>>> -foreach-.
>>>>>> 
>>>>>> But -help statsby- does even better.
>>>>>> 
>>>>>> Here's a dopey example.
>>>>>> 
>>>>>> sysuse auto, clear
>>>>>> statsby N=r(N) k=r(k) p_l=r(p_l) p_u=r(p_u) , by(rep78): bitest
>>>>>> foreign=.2
>>>>>> list
>>>>>> 
>>>>>> Nick
>>>>>> [email protected]
>>>>>> 
>>>>>> On 23 January 2014 17:55, Michael McCulloch <[email protected]>
>>>>>> 
>>>>>>> Thank you Richard, that's exactly what I'm wanting to achieve.
>>>>>>> I understand now that -bysort- clears the scalars at each re-call.
>>>>>>> 
>>>>>>> Can you point me to primers so I can learn how to wrap this into a -foreach- / -levelsof- loop?
>>>>>> 
>>>>>> 
>>>>>> On Jan 23, 2014, at 5:45 AM, Richard Goldstein wrote:
>>>>>>> 
>>>>>>>> If I understand what you want correctly, you cannot do it with bysort
>>>>>>>> because each time you do the test the set of returned values (the
>>>>>>>> "r()"'s) will be replaced and the old ones lost
>>>>>>>> 
>>>>>>>> you can do this within a -foreach- loop (you may need -levelsof- first)
>>>>>>>> in which you quietly do the -bitest- and then list your results for that
>>>>>>>> test and then do the next -bitest-, etc.
>>>>>>>> 
>>>>>>>> here is an example of how to use the returned values:
>>>>>>>> 
>>>>>>>> . sysuse auto
>>>>>>>> 
>>>>>>>> . bitest foreign=.2
>>>>>>>> 
>>>>>>>>  Variable |        N   Observed k   Expected k   Assumed p   Observed p
>>>>>>>> -------------+------------------------------------------------------------
>>>>>>>>   foreign |       74         22         14.8       0.20000      0.29730
>>>>>>>> 
>>>>>>>> Pr(k >= 22)           = 0.029904  (one-sided test)
>>>>>>>> Pr(k <= 22)           = 0.984075  (one-sided test)
>>>>>>>> Pr(k <= 7 or k >= 22) = 0.041800  (two-sided test)
>>>>>>>> r; t=0.09 8:39:38
>>>>>>>> 
>>>>>>>> . di r(N) _skip(2) r(P_p) _skip(2) r(k)/r(N) _skip(2) r(p)
>>>>>>>> 74  .2  .2972973  .04179963
>>>>>>>> 
>>>>>>>> I have not put headers on the columns and have not done several other
>>>>>>>> things you might want (e.g., print format for results) but this should
>>>>>>>> give the basic idea, assuming I have correctly understood you
>>>>>> 
>>>>>> On 1/23/14, 12:57 AM, Michael McCulloch wrote:
>>>>>> 
>>>>>>>>> I am using bitest for a two-sided test on whether the mean of varB is different than 0.2, and testing on each level of varA:
>>>>>>>>>    bysort varA:    bitest varB=.2
>>>>>>>>> 
>>>>>>>>> varA has ~30 values. I wish to display these in a table (showing N, observed p,
>>>>>>>> expected p, and the two-sided p-value), without manual cut-and-paste, as
>>>>>>>> the test
>>>>>>>> will be used to monitor an ongoing training program.
>>>>>>>>> 
>>>>>>>>> I note that the results of bitest are stored as:
>>>>>>>>>    r(N)           number N of trials
>>>>>>>>>    r(P_p)         assumed probability p of success
>>>>>>>>>    r(k)           observed number k of successes
>>>>>>>>>    r(p_l)         lower one-sided p-value
>>>>>>>>>    r(p_u)         upper one-sided p-value
>>>>>>>>>    r(p)           two-sided p-value
>>>>>>>>> 
>>>>>>>>> However, I do not know how one uses these r(**) values. Can anyone suggest how one
>>>>>>>> would go about this?
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>> 
>> 
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index