Statalist


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

RE: st: Programming stata using egen functions


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Programming stata using egen functions
Date   Tue, 28 Apr 2009 18:37:58 +0100

I don't see that this change does anything useful; it just makes the calculation more inefficient. 

Nick 
n.j.cox@durham.ac.uk 

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.

*
*   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