Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, is already up and running.

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

Re: st: rolling and ts operators

From   Richard Herron <>
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 Mon, Oct 24, 2011 at 08:52, Nuno Soares <> 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?
> Best wishes,
> Nuno
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

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