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

MarkusSeptember 23, 2017 at 8:57 pmThank you for that neat program! Is there a way to use Newey West t statistics in the regression?

Thanks

Markus

spattahashimSeptember 25, 2017 at 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 ShahDecember 10, 2017 at 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 PEPPASJuly 25, 2018 at 7:17 amThank you for your help

I have used asreg in an unblalnced panel data.

How do I use Random Effects?

Attaullah ShahJuly 27, 2018 at 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 KettunenNovember 29, 2018 at 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 ShahNovember 29, 2018 at 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.

XiFebruary 13, 2019 at 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 ShahFebruary 13, 2019 at 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

XiFebruary 14, 2019 at 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

PeterMay 12, 2020 at 10:25 pmDear professor Attaullah

I have used your asreg command to calculate rolling idiosyncratic volatility by the standard deviation of the residuals, with great succes running rolling CAPM regressions. Is it possible to calculate total volatility using your asreg command aswel?

best regards

Peter

Attaullah ShahMay 12, 2020 at 11:05 pmPeter

I am not sure how you are calculating the total volatility. If you are doing that with a simple standard deviation, then you can use asrol. More on asrol can be read here https://fintechprofessor.com/stata-programs/asrol-rolling-window-and-by-groups-statistics-in-stata/

Angela Torres CoronaMarch 1, 2021 at 4:34 pmDear Prof. Attaullah Shah,

I wonder if there is a way to add Driscoll-Kraay standard errors to perform rolling regressions using fixed effects in Stata?

I know you can use Newey and robust errors with this code, but is there a way to add Driscoll-Kraay standard errors?

clear all

set more off

webuse grunfeld,clear

xtset company year

tab company, gen(company_)

asreg invest mvalue kstock company_*, wind(year -7 0) robust

//or

asreg invest mvalue kstock company_*, wind(year -7 0) newey(2)

Thank you very much for your help!

Mattias BachmannApril 24, 2021 at 11:33 amDear Prof. Attaullah Shah,

I am interested in a ratio of two coefficients. First, I run asreg to compute these two coefficients and the I compute the ratio of these two. Is there a way how I can estimate the standard error of this ratio?

Thank you!

Kind regards,

Mattias

Hassan RazaMay 25, 2021 at 12:17 pmRespected Sir,

Asreg for Fama and Macbeth failed to work when we have the same series of values as independent variable (Market Excess Return) for each firm (Firm excess return as DV) in panel form, how to resolve this issue. Kind Regards. Dr. Hassan Raza