Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Re: rolling and ts operators

 From Richard Herron To statalist@hsphsun2.harvard.edu Subject Re: st: Re: rolling and ts operators Date Tue, 25 Oct 2011 06:43:43 -0400

```You're right. Thanks for filling in the other half of the explanation.

With one -f.- operator, the last and next-to-last regressions should
have the same number of observations because Nuno used the -recursive-
option, which provides a growing window.

With one -l.- operator, each window should have one less observation
than without the -l.- operator, again, because of the -recursive-
option. Remove -recursive- and the first window will have six
observations and the remainder will have seven.

On Tue, Oct 25, 2011 at 06:34, Nick Cox <njcoxstata@gmail.com> wrote:
> That's what I thought too, although I'd guess at the problem being a
> missing value rather than something being empty. But how come the use
> of f. doesn't exclude the last observation in the window too? This is
> a lazy post, because I have not looked at the code or done any
> experimentation.
>
> Nick
>
> On Tue, Oct 25, 2011 at 11:19 AM, Richard Herron
> <richard.c.herron@gmail.com> wrote:
>> -rolling- passes seven observations to -regress-, but when -regress-
>> creates the regressors, the one with the lag operator in the first
>> observation for each individual is empty. -regress- still runs the
>> regression, but can only use six observations since the first one is
>> empty.
>>
>> It doesn't have to do with the number or linearity of regressors.
>> Maybe it's illustrative to look at that first seven observation window
>> and see that the first lagged value is missing and that -regress- has
>> N = 6.
>>
>> * begin code
>> webuse grunfeld, clear
>> xtset company year
>> list company year invest mvalue l.mvalue in 1/7
>> regress invest mvalue l.mvalue in 1/7
>> * end code
>>
>> On Tue, Oct 25, 2011 at 05:12, Nuno Soares <ndsoares@gmail.com> wrote:
>>> Dear Richard,
>>>
>>> Thank you so much! I didn't know - rolling - was panel data aware!
>>> It's much more efficient than the code I have.
>>>
>>> However, although your insights solve my problem, the main question
>>> still remains: why does Stata use 6 observations and not 7....
>>>
>>> If I just tweak your code a litle bit (to include 5 indep variables as
>>> in my case I squared mvalue);
>>>
>>> * start code *
>>>
>>> webuse grunfeld, clear
>>> rename company id
>>> gen mvaluesq=mvalue^2
>>> xtset id year, year
>>> gen end = year
>>> tempfile stats
>>> rolling _b N = e(N), window(7) recursive saving(`stats', replace) ///
>>>    : reg invest mvalue l.mvalue f.mvalue kstock mvaluesq
>>> merge 1:1 id end using `stats', nogenerate
>>> xtset id year, year
>>>
>>> * calculating error term *
>>> gen e = invest - (_b_cons + _b_mvalue*mvalue + _stat_2*l.mvalue ///
>>>    + _stat_3*f.mvalue + _b_kstock*kstock+_b_mvaluesq*mvaluesq)
>>>
>>> * end code *
>>>
>>> If we just check the data we'll notice that N starts with 6
>>> observations, which doesn't make sense... I must be missing
>>> something...
>>>
>>>
>>> Best wishes,
>>>
>>> Nuno
>>>
>>>
>>> Re: st: rolling and ts operators
>>>
>>> From      Richard Herron <richard.c.herron@gmail.com>
>>> To        statalist@hsphsun2.harvard.edu
>>> Subject   Re: st: rolling and ts operators
>>> Date      Mon, 24 Oct 2011 09:50:16 -0400
>>>
>>>  It may be easiest to think of -rolling- and -regress- in two steps.
>>> First -rolling- grabs a window of 7 observations and hands off these
>>> data to -regress-. Then -regress- regresses, but is only able to use 6
>>> of these observations because in the first window the lagged value
>>> isn't available.
>>>
>>> If you add -N = e(N)- to your -rolling- command you can see how many
>>> observations -regress- used.
>>>
>>> Also, there is no need to use loops with -rolling-; it is panel aware
>>> (at least in 11.2). The following should help with the -N = e(N)- bit.
>>>
>>> webuse grunfeld, clear
>>> rename company id
>>> keep if id == 2 // just a test firm
>>> xtset id year
>>> gen end = year
>>> tempfile stats
>>> rolling _b N = e(N), window(7) recursive saving(`stats', replace) ///
>>>    : reg invest mvalue l.mvalue f.mvalue kstock
>>> merge 1:1 id end using `stats', nogenerate
>>> xtset id year
>>>
>>> * calculating error term *
>>> gen e = invest - (_b_cons + _b_mvalue*mvalue + _stat_2*l.mvalue ///
>>>    + _stat_3*f.mvalue + _b_kstock*kstock)
>>>
>>>
>>>
>>> On 24 October 2011 13:52, Nuno Soares <ndsoares@gmail.com> wrote:
>>>> Hi everyone,
>>>>
>>>> I'm having a problem with the - rolling - command the the use of time
>>>> series operators. I need to estimate a model for several firms (id)
>>>> and several years in a rolling regression with a window of at least 7
>>>> years and the recursive option.
>>>>
>>>> The code I'm using is the following:
>>>>
>>>> use "test.dta", clear
>>>> keep if id==2 * just a test firm
>>>> xtset id year
>>>> gen end=year
>>>> tempfile stats
>>>> levelsof id, local(ids)
>>>> foreach id of local ids {
>>>>        cap rolling , window(7) recursive saving(`stats', replace): reg
>>>> depvar indepvar1 l.indepvar1 f.indepvar1 indepvar2 indepvar3 if
>>>> id==`id'
>>>>        cap merge 1:1 id end using "`stats'", update replace
>>>>        cap drop _merge
>>>> }
>>>> sort id year
>>>> xtset id year
>>>> * calculating error term *
>>>> gen e=depvar-(_b_cons+_b_indepvar1*indepvar1+_b_stat2*l.indepvar1+_b_stat3*f.indepvar1+_b_indepvar2*indepvar2+_b_indepvar3*indepvar3)
>>>>
>>>>
>>>> Given the l. operator I was expecting that Stata would use 8
>>>> observations given that the first l.indepvar1 would be missing an thus
>>>> it wouldn't have enough observations to estimate the first model with
>>>> only six observations (while the window option is set at 7,
>>>> restricting the sample to the first 7 observations would lead to have
>>>> only 6 eligible observations, and thus not enough data to estimate the
>>>> regression). This however isn't happening with Stata reporting
>>>> estimated coefficient starting from the first 7 observations. Any
>>>> clues on why this is happening?
>>>>
>
> *
> *   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/
>

*
*   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/
```