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

## Leave a Reply

## 3 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