Bookmark and Share

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: In unbalanced panel, make sure individuals begin in month X and end in month Y


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: In unbalanced panel, make sure individuals begin in month X and end in month Y
Date   Mon, 10 Oct 2011 01:46:10 +0100

An overall -if- could constrain any analysis to a the same time interval.

Nick

On Sun, Oct 9, 2011 at 10:30 PM, Richard Herron
<[email protected]> wrote:
> I am not sure that it can be done without a loop or with -if-
> statements and fewer than 20 conditional statements. I am very
> interested to see attempts -- without a loop I could fix beginnings,
> but not endings.
>
> Unfortunately -while- loops aren't byable and -_n- and -_N- don't work
> properly with -foreach-. My -forvalues- solution is not particularly
> efficient, but works.
>
> * ----- begin code -----
> * generate random starting dates
> clear
> set obs 250
> egen int id = seq()
> generate int date_m = 1 + int(12*runiform())
> generate int date_y = 1960 + int(50*runiform())
>
> * add random number of observations to each id
> generate int n_obs = 48 + int(10*12*runiform())
> expand n_obs
>
> * create seq
> tempvar date_m_seq
> bysort id: generate int `date_m_seq' = date_m + _n - 1
> by id: replace date_m = 1 + mod(`date_m_seq' - 1, 12)
> by id: replace date_y = date_y + int((`date_m_seq' - 1) / 12)
> by id: generate int id_n = _n
>
> * generate random variables
> generate y = rnormal()
> generate x = rnormal()
>
> * create panel
> generate int date_ym = ym(date_y, date_m)
> format date_ym %tm
> tsset id date_ym
>
> forvalues i = 1/11 {
>    bysort id: drop if ((date_m != 4 & _n == 1) ///
>        | (date_m != 4 & _n == _N))
> }
>
> * ----- end code -----
>
>
> On Sat, Oct 8, 2011 at 18:08, Nick Cox <[email protected]> wrote:
>> What's wrong with an -if- condition?
>>
>> Nick
>>
>> On Sat, Oct 8, 2011 at 9:59 PM, Richard Herron
>> <[email protected]> wrote:
>>> How can I make sure individuals in a panel begin in one given month
>>> and ends in another given month? For example, I want to make sure that
>>> every firm in an unbalanced monthly panel begins in April and ends in
>>> March. A priori I don't know each firm's first or last month (i.e.,
>>> unbalanced panel).
>>>
>>> My end goal is to use -rolling- to perform regressions with a 48-month
>>> window and a 12-month step size to get regression coefficients each
>>> year. I want to avoid regressing every month and dropping 11 of 12
>>> entries each year, which would be very time-consuming.
>>>
>>> Thanks! Here is some code:
>>>
>>> * ----- begin code -----
>>> * generate random starting dates
>>> clear
>>> set obs 250
>>> egen int id = seq()
>>> generate int date_m = 1 + int(12*runiform())
>>> generate int date_y = 1960 + int(50*runiform())
>>> generate int date_ym = ym(date_y, date_m)
>>> format date_ym %tm
>>>
>>> * add random number of observations to each id
>>> generate int n_obs = 1 + int(20*12*runiform())
>>> expand n_obs
>>> bysort id: replace date_ym = date_ym + _n
>>>
>>> * generate random variables
>>> generate y = rnormal()
>>> generate x = rnormal()
>>>
>>> * I would like to do this rolling regression with a 12 month step-size
>>> * and have them go from April through March four years later
>>> tsset id date_ym
>>> rolling z = (e(rss) / e(N)) ///
>>>    , window(48) stepsize(12) saving(temp, replace) ///
>>>    : regress y x
>>>
>>>  * ----- end code -----
>>> *
>>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index