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: Use egen command with time series operator
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Use egen command with time series operator
Date
Thu, 28 Nov 2013 10:43:30 +0000
I add some further comments to this very good advice from Maarten.
First, one example given by Catharina was
egen mean_x1 = mean(l.x1)
Firing up -egen- to get a single constant, the mean, is a poor idea
compared with
su l.x1
which gives you that constant. If you want the mean to be in a variable then
su l.x1, meanonly
gen mean_x1 = r(mean)
is the way to go.
Very probably that example was just by way of illustrating the -egen-
problem, but I didn't want it to pass by without comment.
Second, Catharina wrote
"Please consider that it is not reasonable for my programming to first
generate the lag/lead and then generate the mean."
I've got to ask: Why do you say that?
Nick
[email protected]
On 28 November 2013 09:55, Maarten Buis <[email protected]> wrote:
> On Thu, Nov 28, 2013 at 9:03 AM, Klepsch, Catharina <[email protected]> wrote:
>> I was wondering whether it is possible to use the egen command with time series operators.
>> For example, I tried to generate the mean value for a lagged variable and used:
>>
>> sort ID YEAR
>> egen mean_x1 = mean(l.x1)
>>
>> Unfortunately, there occurs an error message which states:
>> not sorted r(5)
>>
>> I'm not quite sure why this happens, because I sorted the data before. Even if I type:
>> bys ID YEAR: egen mean_x1 = mean(l.x1)
>>
>> the same error message occurs.
>>
>> Any ideas how I can fix that? Please consider that it is not reasonable for my programming to first generate the lag/lead and then generate the mean.
>
> Many of the -egen- commands sort the data internally, thus overwriting
> your sort prior to calling the -egen- command. This is also true for
> the -mean()- egen command, as you can see when typing -viewsource
> _gmean.ado-. This is why the help-file of -egen- contains the warning:
> "Explicit subscripting (using _N and _n), which is commonly used with
> generate, should not be used with egen". So you cannot do what you
> want with the -egen- command. However, if you look at the source code
> of the mean() egen-command (the -viewsource- command mentioned above),
> you can see tricks on how to compute this from first principle.
>
*
* 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/