[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
Robin Luo <rluosf@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: very confused - how to implement a dynamic lagged variable |

Date |
Mon, 21 Feb 2005 01:57:16 -0800 |

I am very sorry for the layout, it seems very hard to get it aligned in the e-mail system. But you can simply copy and paste my program into STATA and play with it. Many many thanks! Robin On Mon, 21 Feb 2005 01:50:55 -0800, Robin Luo <rluosf@gmail.com> wrote: > I got really confused by this problem: I want to use a lagged variable > in my analysis but instead of lagging based on some pre-existing > value, this lag actually comes from certain calculation. My data is a > panel data (grouped by "firmid") with a structure like: > > firmid year age_a age_b weight_a weight_b > 100 1960 10 > 100 1961 11 2 .9 .1 > 100 1962 12 > 100 1963 13 10 .7 .3 > 100 1964 14 > 100 1965 15 > 100 1966 16 5 .8 .2 > 101 1940 1 > 101 1941 2 10 .6 .4 > 101 1942 3 > > The goal is to calculate an adjusted age for each firm-year. In the > data, age_a is the natural age of the firm, while age_b comes from > certain event (say, an acquisition). Before there is any event, the > firm's adjusted age is simply the natural age (age_a). When there is > an event, the new adjusted age is the weighted sum of the adjusted age > and age_b (weight_a is the weight for adjusted age while weight_b for > age_b). When there is no event, the firm's adjusted age simply > increase by 1 for each year. I thus wrote the following program: > clear > > input firmid year age_a age_b weight_a weight_b > 100 1960 10 . . . > 100 1961 11 2 0.9 0.1 > 100 1962 12 . . . > 100 1963 13 10 0.7 0.3 > 100 1964 14 . . . > 100 1965 15 . . . > 100 1966 16 5 0.8 0.2 > 101 1940 1 . . . > 101 1941 2 10 0.6 0.4 > 101 1942 3 . . . > end > > sort firmid year > > gen age=0 > > by firmid: replace age=age_a if _n==1 & age_b==. > by firmid: replace age=(age_a*weight_a + age_b*weight_b) if _n==1 & age_b~=. > > by firmid: replace age=(age[_n-1]+1)*weight_a + age_b*weight_b if > _n~=1 & age_b~=. > by firmid: replace age=(age[_n-1]+1) if _n~=1 & age_b==. > > The result, however, came quite strange: > > firmid year age_a age_b weight_a weight_b age > 100 1960 10 10 > 100 1961 11 2 .9 .1 10.1 > 100 1962 12 11.1 > 100 1963 13 10 .7 .3 3.7 > 100 1964 14 4.7 > 100 1965 15 5.7 > 100 1966 16 5 .8 .2 1.8 > 101 1940 1 1 > 101 1941 2 10 .6 .4 5.2 > 101 1942 3 6.2 > > For the 4th and 7th observations (where "year" are 1963 and 1966 > respectively), the correct ages should be about 11 > ([11.1+1]*0.7+10*0.3) and 6 ([5.7+1]*0.8+5*0.2). Obviously, instead of > getting the actual last "age" values, STATA took the very original > ones - the generated 0s. I could not figure it out and really > appreciate any help on this. > > Thanks a lot! > > Robin Luo > * > * For searches and help try: > * http://www.stata.com/support/faqs/res/findit.html > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**st: very confused - how to implement a dynamic lagged variable***From:*Robin Luo <rluosf@gmail.com>

- Prev by Date:
**st: very confused - how to implement a dynamic lagged variable** - Next by Date:
**st: Reference for mlogit with cluster option** - Previous by thread:
**st: very confused - how to implement a dynamic lagged variable** - Next by thread:
**st: Reference for mlogit with cluster option** - Index(es):

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