Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Lag operators on panel data inside -bysort- |

Date |
Mon, 29 Oct 2012 22:34:51 +0000 |

Naturally you can use variable names with subscripts on the RHS of -generate- and -replace-. But that's because they are both expressions, not because they have the same status. On Mon, Oct 29, 2012 at 10:29 PM, Richard Herron <richard.c.herron@gmail.com> wrote: > Thanks for explanation. That subscripts aren't equivalent to variable > names is the concept that I'm missing. Whenever I generate additional > variables I suspect that I'm doing something wrong, but here it's the > best (only) approach. > > On Mon, Oct 29, 2012 at 2:53 PM, Nick Cox <njcoxstata@gmail.com> wrote: >> Working backwards from the bottom: >> >> 1. Variable names with subscripts are not acceptable within Stata as >> equivalent to variable names. >> >> 2. The previous -regress- doesn't work because the sort order implied >> by a reference to L.invest is -sort company year- which clashes with >> the -sort- order you are asking for. >> >> 3. -xtile()- is an -egen- function that must be installed from >> -egenmore- (SSC), as you are asked to explain. >> >> I think to do what you are asking then, as you say, you need to step >> outside the framework of -tsset- by >> >> tsset company year >> gen previnvest = L.invest >> bysort invest_q : regress mvalue previnvest >> >> but even then what sense does this make unless exceptionally companies >> don't change between quantile categories? >> >> Nick >> >> >> On Mon, Oct 29, 2012 at 3:02 PM, Richard Herron >> <richard.c.herron@gmail.com> wrote: >> >>> I would like to use lag operators on panel data inside a -bysort- >>> command, but I get the -not sorted- error. For example, using the >>> following data, >>> >>> * >>> webuse grunfeld, clear >>> xtset company year >>> >>> * generate quantiles with -xtile- from -egenmore- (SSC) >>> egen invest_q = xtile(invest), by(year) >>> * >>> >>> neither of the following two lines work. >>> >>> * >>> bysort invest_q: regress mvalue l.invest >>> bysort invest_q (company year): regress mvalue invest[_n-1] >>> * >>> >>> I thought -xtset-'s effects were persistent, but the error makes >>> sense. Is there an elegant solution? (It would be nice to be able to >>> use lag operators on the fly.) Or is the best option to generate >>> lagged variables as follows. >>> >>> * >>> generate linvest = l.invest >> * >> * 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/ * * 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/

**References**:**st: Lag operators on panel data inside -bysort-***From:*Richard Herron <richard.c.herron@gmail.com>

**Re: st: Lag operators on panel data inside -bysort-***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: Lag operators on panel data inside -bysort-***From:*Richard Herron <richard.c.herron@gmail.com>

- Prev by Date:
**Re: st: Lag operators on panel data inside -bysort-** - Next by Date:
**Re: st: intreg with control for sample selection and endogeneity bias** - Previous by thread:
**Re: st: Lag operators on panel data inside -bysort-** - Next by thread:
**st: Average If with Multiple Conditions** - Index(es):