# Rolling window regressions in Stata

## Rolling window regressions in Stata

Category:Blog

Rolling window statistics are also known as sliding or moving window statistics. Rolling window regressions have special use in Finance and other disciplines. Rolling window calculations require lots of looping over observations. The problem is compounded by different data structures such as unbalanced panel data, data with many duplicates, and data with many missing values. Yet, there might be data sets that have both time series gaps as well as many duplicate observations across groups.

## asreg : A simple and fast solution to rolling window regressions

asreg is a Stata program for estimation of rolling window regressions. To estimate rolling window regressions in Stata, the conventional method is to use the rolling command of Stata. However, that command is too slow, especially for larger data set. asreg is order of magnitude faster than estimating rolling window regressions through conventional methods such as Stata loops or using the Stata’s official rolling command. asreg has the same speed efficiency as asrol. All the rolling window calculations, estimation of regression parameters, and writing the results to Stata variables are done in the Mata language.

## Why asreg is so fast?

Rolling window calculations require lots of looping over observations. The problem is compounded by different data structures such as unbalanced panel data, data with many duplicates, and data with many missing values. Yet, there might be datasets that have both time series gaps as well as many duplicate observations across groups. asreg does not use a static code for all types of data structures. Instead, asreg intelligently identifies data structures and matches one of its rolling window routines with the data characteristics. Therefore, the rolling window regressions are fast even in larger data sets. asreg writes all regression outputs to the data in memory as separate variables. This eliminates the need for writing the results to a separate file and then merging them back to the data for any further calculations.

### Installation

asreg can be installed for free by typing the following command in the Stata’s command window:

ssc install asreg

After the installation is complete, we can directly use asreg from the Stata’s command window. Let us use the grunfeld data set from the web and estimate rolling regressions with asreg. To download the dataset, type the following from the Stata command window:

webuse grunfeld, clear

Please note that the word clear after comma tells Stata to unload an existing data set from its memory. So this option has to be used carefully as this might result in losing any unsaved changes to the data set in memory.

#### Example 1: regression in a 10-years rolling window

bys company: asreg invest mvalue kstock, wind(year 10)

Explanation: Let us discuss the components of the code line that we used above for 10-years rolling regressions.

bys company : forces asreg to estimate the rolling regression separately for each company

asreg invest mvlaue kstock : asreg invokes the asreg program. Right after asreg, we have to type the name of the dependent variable, and then the full list of independent variables. Therefore, in our example, the dependent variable is invest, and we have two independent variables, i.e., mvalue and kstock.

, wind(year 10) : After the comma, the program’s optional options are specified. The phrase wind(year 10) tells Stata to use a rolling window of 10 observation, based on the values of the existing variable year.

#### Example 2: Regression for each company in a recursive window

webuse grunfeld

bys company: asreg invest mvalue kstock, wind(year 10) rec

OR

. bys company: asreg invest mvalue kstock, wind(year 1000)

#### Example 3: Using option minimum

. webuse grunfeld

. bys company: asreg invest mvalue kstock, wind(year 10) min(5)

#### Example 4: Reporting standard errors

. webuse grunfeld

. bys company: asreg invest mvalue kstock, wind(year 10) se

#### Example 5: Reporting standard errors, fitted values and residuals

. webuse grunfeld

. bys company: asreg invest mvalue kstock, wind(year 10) se fit

#### Example 6: Reporting Newey-West standard errors with two lags

. webuse grunfeld

. bys company: asreg invest mvalue kstock, wind(year 10) newey(2)

See also these related posts

by-group regressions

Fama-MacBeth (1973) regressions

Rolling window regression, rolling windows betas

### 10 Comments

#### Markus

September 23, 2017at 8:57 pm

Thank you for that neat program! Is there a way to use Newey West t statistics in the regression?
Thanks
Markus

#### spattahashim

September 25, 2017at 11:59 pm

Thanks for your comments. asreg does not allow Newey West s-statistics right now, but I do plan to add more useful features to this program.

#### Attaullah Shah

December 10, 2017at 5:44 pm

Dear Markus, the newey option has been added to asreg now. You can update the version of asreg from ssc by
adoupdate asreg, update

#### GEORGE PEPPAS

July 25, 2018at 7:17 am

Thank you for your help
I have used asreg in an unblalnced panel data.
How do I use Random Effects?

#### Attaullah Shah

July 27, 2018at 6:30 pm

Dear George, thanks for your inquiry. Actually, asreg calculates OLS objects. However, you can use the xtdata command to convert your data to a form suitable for random-effects estimation. After transformation, you can then apply asreg.

#### Jyri Kettunen

November 29, 2018at 2:10 pm

Hi,

Thank you for the wonderful program. It works like a charm.
I have a question regarding forward-looking moving-window regressions. How can we use asreg to calculate forward-looking moving-window regressions. In other words, for each observation, the next 100 observations are used in the regression.

#### Attaullah Shah

November 29, 2018at 10:13 pm

Forward looking window is not yet supported by asreg, but I am working on it and expect to add it in a year time.

#### Xi

February 13, 2019at 2:49 am

Dear Prof. Attaullah
Thanks for this magnificent work. asreg is the most efficient command for loop syntax I have ever seen!
However, my work requires the demonstration of t-static and p-value based on Newey regression. It there a option to put it off?
Looking forward to your enlightenment.
Respectfully yours
Xi

#### Attaullah Shah

February 13, 2019at 12:39 pm

Xi
Thanks for your kind words. asreg can estimate newey regression when you invoke the option newey(#) after comma, where # refers to an integer value for lag selection. The newey option works great with time series data, I have not tested it using panel data.

Concerning the t-values and p-values, I discuss these in detail in this post. Kindly read it and comment on it.

For newey regression, consider the following example

```webuse grunfeld, clear
keep if company==1
tsset year

asreg invest mvalue kstock, newey(2)

gen tvalue_Cons =  _b_cons / _se_cons

gen p_values_Cons = (2 * ttail(_Nobs-3), abs( _b_cons / _se_cons ))
list _b_cons tvalue_Cons p_values_Cons in 1

+----------------------------------+
|   _b_cons   tvalue_~s   p_valu~s |
|----------------------------------|
1. | -149.7824   -1.268183   .2218258 |
+----------------------------------+

* Compare the results with the newey regression of Stata
newey invest mvalue kstock , lag(2) force

Regression with Newey-West standard errors      Number of obs     =         20
maximum lag: 2                                  F(  2,        17) =      62.22
Prob > F          =     0.0000

------------------------------------------------------------------------------
|             Newey-West
invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
mvalue |   .1192808   .0303107     3.94   0.001     .0553308    .1832308
kstock |   .3714448    .052149     7.12   0.000       .26142    .4814696
_cons |  -149.7824   118.1078    -1.27   0.222    -398.9682    99.40336
------------------------------------------------------------------------------

```

#### Xi

February 14, 2019at 2:40 am

Hello Prof Attaullah Shah
I already construct my panel and I want to apply rolling windows for previous 36 moth on every fund I collected to obtain each fund’s rolling window alpha. I already watched your tutorial of rolling windows on YouTube but the results gave me various values of beta that I do not how to interpret and implement. The commands I applied show as below :
-gen mofd = mofd(date)
-gen year = year(date)
-bys id: asreg (excessive ret) (my proxies), wind (mofd 36) {or year 3}

Looking forward to your reply