Statalist


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

AW: st: Programming stata using egen functions


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: Programming stata using egen functions
Date   Tue, 28 Apr 2009 19:56:14 +0200

<> 

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: [email protected] [mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Dienstag, 28. April 2009 19:43
An: [email protected]
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 
[email protected] 

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
>
>
>[email protected] 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/



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