Statalist


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

Re: AW: st: Programming stata using egen functions


From   Anne Resende <A.C.C.Resende@rhul.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: AW: st: Programming stata using egen functions
Date   Tue, 28 Apr 2009 20:48:09 +0100 (BST)

I swear that this is my last email:
I trying to calculate bounds on E[y(t)].  So I need to calculate the 
lower bound and also a confidence interval on it using boostrap.
So I am trying to calculate:  
                    (1)	                        (2)
SUM    max( E[y|z=s] + max {(E[y|z=s']-E[u|z=u]/(s'-u))*(t-s')}) 
(s>=t)  (s'|s>=s'>=t}    {u|u<t}


(Okumura and Usui, 2006 pag.4).  So here we have s,s’,t and u: every 
letter goes from 8 to 20. I decide to do a program for each, 
t=8,...,20. (So I am trying to calculate the lower bound for each t). I 
also have tried to use all of t together (also create forvalues for t) 
but stata gives a message “No room to add more variables”.  In the 
program that I already sent to you I am considering that t is equal 9, 
so u=8 because u<9  (I do this in line 7 : qui sum `1' if u==8 ). So I 
have tried to use rowmax() to calculate the maximum of the first max 
parenthesis (lines 9 and 11- part (1) + (2)), doesn’t  matter if I 
calculate (2) and take the max or sum (1) and (2) and take the max, as 
I have to take the max again). Then in the reminding lines I have tried 
to multiply  (3) {(1)+(2)}, the whole equation by P(z=s) and then use 
rowtotal to sum all of these observations, as  I have to sum over s>=t. 
(lines 15 and 16 of the programe)

I am afraid that this could be less clear than before but is exactly 
what I trying to do.  Of course that I do not expect you create an ado 
file for me. I just want some tips or some help as you all know much 
more about stata programming. So I really need to move the calculation 
of the maximum outside the loop? 


Many thanks 




Nick Cox wrote:


>Quite so, and not a problem. It's my point 1 that is more important. 
>
>Nick 
>n.j.cox@durham.ac.uk 
>
>
>-----Original Message-----
>From: Anne Resende [mailto:A.C.C.Resende@rhul.ac.uk] 
>Sent: 28 April 2009 19:02
>To: Nick Cox
>Subject: RE: st: Programming stata using egen functions
>
>
>2. "You don't know enough Stata to do what you want without help."
>That is why I decided to send an email to statalist. 
>Thanks anyway
>Anne 
>
>
>


Martin Weiss wrote:


>
><> 
>
>True, I was too focused on the issue with the -scalar- and -local- 
problem to notice that -rowtotal()- was not the best example.
>
>Overall, I am sensing an overwhelming willingness to help Anne, but she 
should try to describe _verbally_ what she wants to achieve, and I am 
sure no more than a couple of lines will be necessary to get there. 
Maarten has already had a stab at it...
>
>
>HTH
>Martin
>
>
>-----Ursprüngliche Nachricht-----
>Von: owner-statalist@hsphsun2.harvard.edu 
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Nick Cox
>Gesendet: Dienstag, 28. April 2009 19:43
>An: statalist@hsphsun2.harvard.edu
>Betreff: RE: st: Programming stata using egen functions
>
>But then the -rowtotal()- is pointless. As Kit pointed out several 
posts ago, the row total of one item is precisely that item. 
>
>Nick 
>n.j.cox@durham.ac.uk 
>
>Martin Weiss
>
>Better example
>
>*************
>tempvar ep_`s'
>gen `ep_`s''=exem2_`s'*`P1_`s'' 
>qui egen `ep2_`s''=rowtotal(`ep_`s'')
>*************
>
>Martin Weiss
>
>" So in line 9 I substitute `scalar’ for `local’ hoping 
>that rowmax() could now recognize the varlist."
>
>Well, you have not passed a -varlist- to -egen, rowmax()- so it 
complains about its syntax statement not being respected, and rightly 
so. -local-s and -scalar-s are equally bad, in this respect. If that is 
all that is standing between you and success, then let the -gen- and 
-egen- statements refer to -tempvar-s. In this fashion, at least they 
will be legal...
>
>
>So instead of
>
>**
>qui summarize `1' if `sp'==u
>local mean1_`s'`sp' =r(mean)
>**
>
>say -egen mean1_ssp=mean(`1') if `sp'==u-
>
>Anne Resende
>
>Thanks a lot for all answers!!
>Just to clarify some points:
>I understood that the egen functions rowmax() and rowtotal() did not 
>accept scalar. So in line 9 I substitute `scalar’ for `local’ hoping 
>that rowmax() could now recognize the varlist. But stata returns de 
>same error message. I need the rowmax() function to returns the maximum 
>values between the varilist. (the maximum values within each `s’)
>
>So I have tried:
>program mymean, rclass
>  1.   syntax [varlist] [if]
>  2.   tokenize "`varlist'"
>  3.   forvalues s=9(1)20 {
>  4.   forvalues sp=9(1)`s' {
>  5.         qui summarize `1' if `sp'==u
>  6.         local mean1_`s'`sp' =r(mean)
>  7.         qui sum `1' if u==8
>  8.         local mean2_`s'`sp' =r(mean)
>  9.           local m_`s'`sp'=`mean1_`s'`sp'' + ((`mean1_`s'`sp'' - 
>`mean2_`s'`sp'')/(`sp'-8))*(9-`sp')    
> 10.         tempvar exem_`s' P_`s' ep_`s' ep2_`s'
> 11.        qui egen `exem_`s''=rowmax(m_`s'`sp')       
> 12.        qui gen `P_`s''=sum(P) if `s'==u & id==1
> 13.        qui sum `P_`s'', meanonly
> 14.        local P1_`s'=r(max)
> 15.          scalar ep_`s'=exem2_`s'*`P1_`s'' 
> 16.        qui egen `ep2_`s''=rowtotal(ep_`s')
> 17.          return scalar eq2=`ep2_`s''    
> 18. }
> 19. }
> 20. end
>
>end of do-file
>. mymean loghw
>variable m_99 not found
>r(111);
>
>1) I understand the differences between egen sum( )and stata fuctions 
>sum(), but neither gives me what I need. I intend to use rowtotal() 
>because it sums the observations on the rows not on the columns as egen 
>sum() and sum() do.
>
>2) Sorry about:  return scalar eq29=rowtotal(ep_`s') that I wrote in 
>the other email.. I know that it’s wrong
>
>3)What is my aim? As I told I need to end up with one variable in order 
>to run the bootstrap. 
>I want to the create the program mymean and then run: bootstrap r(eq2), 
>reps(#) seed(#)  : mymean loghw  (I am following the idea on: 
>http://www.stata.com/capabilities/boot.html)
>
>4)I think that I cannot use a do.file because I will end up with a 
>constant (I also tried) and so I could not run the bootstrap. Creating 
>and ado file the program allows me to use the boots after.
>
>So all I want to do is to write a program to calculate my statistics of 
>interest and then apply the bootstrap. And my problem is how I can 
>construct a varlist in order to run my program using rowmax() and 
>rowtotal(). Or maybe there is another way to do that without having to 
>use rowmax() and rowtotal() functions.
>I hope that is clearer now,
>Thanks 
>Anne
>
>Thanks
>Anne
>
>
>Richard Ochmann wrote:
>
>
>>...
>>not entirely clear what you want to do there,
>>but maybe you can borrow from this code on generating a list of scalars 
>>(which might be inefficient either):
>>
>>***
>>#delimit;
>>sysuse auto;
>>local meanlist = "0";
>>forvalues i = 1/2       {;
>>forvalues j = `i'/3     {;
>>quietly sum price, meanonly;
>>scalar s`i'`j' = r(mean)/runiform();
>>local meanlist = " `meanlist', `=s`i'`j'' ";
>>di "`meanlist'";
>>scalar mymax = max(`meanlist');
>>di mymax;
>>                                                };
>>                                        };
>>***
>>
>>best, rich
>>
>>
>>owner-statalist@hsphsun2.harvard.edu schrieb am 28.04.2009 02:05:25:
>>
>>> Dear All
>>> I am having some problems with my ado.file whenever I try to include 
>>> some egen functions like rowmax and rowtotal. I am currently using 
>>> Stata 10.0
>>> 
>>> My ado program is:
>>> 
>>>  program mymean, rclass
>>>   1.   syntax [varlist] [if]
>>>   2.   tokenize "`varlist'"
>>>   3.   forvalues s=9(1)20 {
>>>   4.   forvalues sp=9(1)`s' {
>>>   5.         qui summarize `1' if `sp'==u
>>>   6.         local mean1_`s'`sp' =r(mean)
>>>   7.         qui sum `1' if u==8
>>>   8.         local mean2_`s'`sp' =r(mean)
>>>   9.           scalar m_`s'`sp'=`mean1_`s'`sp'' + ((`mean1_`s'`sp'' - 
>>> `mean2_`s'`sp'')/(`sp'-8))*(9-`sp')
>>>  10.         tempvar exem_`s' P1_`s' P_`s' ep_`s' ep2_`s'
>>>  11.         qui egen `exem_`s''=max(m_`s'`sp')
>>>  12.           return scalar exem2_`s'=`exem_`s''*1
>>>  13.           qui gen `P1_`s''=sum(P) if `s'==u & id==1
>>>  14.           qui egen `P_`s'' =max(`P1_`s'') 
>>>  15.           scalar ep_`s'=exem2_`s'*`P_`s'' 
>>>  16.         qui gen `ep_`s''=exem2_`s'*`P_`s''
>>>  17.         return scalar eq29=sum(ep_`s')
>>>  18. }
>>>  19. }
>>>  20. end
>>> This program is running well. But in lines 11 and 17, I in fact need 
>to 
>>> use the commands rowmax rather than max and rowtotal rather sum. So 
>>> when I use max and sum the program runs well but when I substitute 
>this 
>>> commands whith rowmax and rowtotal stata gives me the following error 
>>> message after I typed mymean loghw: 
>>> 
>>> 
>>>  program mymean, rclass
>>>   1.   syntax [varlist] [if]
>>>   2.   tokenize "`varlist'"
>>>   3.   forvalues s=9(1)20 {
>>>   4.   forvalues sp=9(1)`s' {
>>>   5.         qui summarize `1' if `sp'==u
>>>   6.         local mean1_`s'`sp' =r(mean)
>>>   7.         qui sum `1' if u==8
>>>   8.         local mean2_`s'`sp' =r(mean)
>>>   9.           scalar m_`s'`sp'=`mean1_`s'`sp'' + ((`mean1_`s'`sp'' - 
>>> `mean2_`s'`sp'')/(`sp'-8))*(9-`sp')
>>>  10.         tempvar exem_`s' P1_`s' P_`s' ep_`s' ep2_`s'
>>>  11.         qui egen `exem_`s''=rowmax(m_`s'`sp')
>>>  12.           return scalar exem2_`s'=`exem_`s''*1
>>>  13.           qui gen `P1_`s''=sum(P) if `s'==u & id==1
>>>  14.           qui egen `P_`s'' =max(`P1_`s'') 
>>>  15.           scalar ep_`s'=exem2_`s'*`P_`s'' 
>>>  16.         qui gen `ep_`s''=exem2_`s'*`P_`s''
>>>  17.         return scalar eq29=rowtotal(ep_`s')
>>>  18. }
>>>  19. }
>>>  20. end
>>> . 
>>> end of do-file
>>> . mymean loghw
>>> variable m_99 not found
>>> r(111);
>>> 
>>> So I would like to know why the program recognizes the max and sum 
>egen 
>>> functions but do not recognize (or do not find my variable) when I use 
>>> the rowmax and rowtotal egen functions. Is there any programming 
>>> differences between these two kind of egen functons?
>
>*
>*   For searches and help try:
>*   http://www.stata.com/help.cgi?search
>*   http://www.stata.com/support/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/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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