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: Working with row percentiles and row averages
From 
 
Nick Cox <[email protected]> 
To 
 
"[email protected]" <[email protected]> 
Subject 
 
Re: st: Working with row percentiles and row averages 
Date 
 
Tue, 9 Jul 2013 14:27:14 +0100 
Let's imagine that we have -pc10- and we want the average of all
returns less than that. The average is naturally the sum divided by
the count. So, we go
gen count = 0
gen sum = 0
quietly forval j = 1/1000 {
           replace count = count + 1 if r`j' < pc10
           replace sum = sum + r`j' if r`j' < pc10
}
gen ave10 = sum/count
There is a review at
SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
        (help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
        Q1/09   SJ 9(1):137--157
        shows how to exploit functions, egen functions, and Mata
        for working rowwise; rowsort and rowranks are introduced
A .pdf is accessible at
http://www.stata-journal.com/sjpdf.html?articlenum=pr0046
If it's valuable, please cite it.
Nick
[email protected]
On 9 July 2013 14:15, George Murray <[email protected]> wrote:
> abcde
> Dear Statalist,
>
> I am working with time series data, and my variables consist of
> monthly returns for 1000 stocks (so the variables are
> date,r1,r2,r3,...,r1000). I would like to find, for each date, the
> average return of the stocks whose return is below that of the 10th
> percentile. That is, for the 10% of stocks with the lowest returns, I
> want to compute their average, for each date.
>
> I am aware that a command such as -egen p10 = rowpctile(r1-r1000),
> p(10)- generates a variable which computes the return of the 10th
> percentile at each date, but I don’t know how to go from here to find
> the average return below the 10th percentile, for each date.
>
> Any ideas for commands would be very highly appreciated. This is the
> first time I have had to work ‘by rows’, and am very stuck with this.
>
> Thank you.
>
> *
> *   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/