## Rolling window regressions in Stata

Category:BlogRolling 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**

## 10 Comments

## Markus

September 23, 2017at 8:57 pmThank 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 pmThanks 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 pmDear 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 amThank 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 pmDear George, thanks for your inquiry. Actually,

asregcalculates OLS objects. However, you can use thextdatacommand 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 pmHi,

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 pmForward 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 amDear 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 pmXi

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

## Xi

February 14, 2019at 2:40 amHello 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