# Tag Archives: rolling window regressions

## Stata Rolling command vs asreg for rolling regressions: Similarities and differences

Category:Stata Programs

Karina van Kuijk asked the following question:

### Question:

I need to calculate the factor sensitivity of firms to ultimately sort portfolio’s based on this factor. I have found the asreg Stata code on your website and I was wondering if this code would be useful for my purpose. However, if I compare the rolling Stata code with your aserg program on a small dataset, I won’t get the same results.

The key difference between the Stata’s official rolling command and asreg [see this blog entry for installation] is in their speeds. asreg is an order of magnitude faster than rolling.  There are other differences with respect to how these two calculate the regression components in a rolling window.  For example, rolling command will report statistics when the rolling window reaches the required length while asreg reports statistics when the number of observations is greater than the parameters being estimated. Therefore, if we have one independent variable and use a rolling window of 10 periods, rolling will report statistics from the 10th period in the dataset. However, asreg will report statistics from the 3rd observation (two parameters here, the coefficient of the independent variable and the intercept).  To make the results of asreg at par with the rolling command, let us use an example:

### Example

Let us use the grunfeld data that has 10 companies and 20 years of time series for each company. We shall use the variables invest as dependent variable and mvalue as the independent variable.  Therefore, the rolling command will look like:

```webuse grunfeld

rolling _b, window(10) saving (beta, replace): reg invest mvalue```

The results from the rolling command are reported below only for the first company

 company start end _b_cons _b_mvalue 1 1935 1944 186.5406 .0562316 1 1936 1945 196.1084 .0573704 1 1937 1946 106.4769 .0847188 1 1938 1947 53.12083 .1053145 1 1939 1948 364.5426 .0359897 1 1940 1949 372.5457 .0400371 1 1941 1950 360.8489 .04835 1 1942 1951 213.7943 .090357 1 1943 1952 119.8572 .1195415 1 1944 1953 -284.6031 .2229699 1 1945 1954 -496.6066 .2841584

To find similar results with asreg, we shall type:

`bysort company: asreg invest mvalue, wind(year 10)`

asreg generated the following results for the first company:

 company year _Nobs _R2 _adjR2 _b_cons _b_mvalue 1 1935 . . . . . 1 1936 . . . . . 1 1937 3 .98568503 .97137006 192.3812 .04135324 1 1938 4 .91957661 .87936492 129.06727 .05411168 1 1939 5 .86795099 .82393465 129.91674 .05233687 1 1940 6 .69944952 .6243119 108.59266 .06102699 1 1941 7 .54085608 .4490273 91.235677 .06942586 1 1942 8 .31250011 .19791679 182.86065 .05101677 1 1943 9 .25355654 .14692176 197.08754 .05052367 1 1944 10 .24298452 .14835759 186.54064 .05623158 1 1945 10 .20582267 .10655051 196.10839 .05737045 1 1946 10 .29515806 .20705282 106.47691 .0847188 1 1947 10 .3728928 .2945044 53.120829 .10531451 1 1948 10 .05894158 -.05869073 364.54258 .03598974 1 1949 10 .1461912 .0394651 372.54574 .04003715 1 1950 10 .18946219 .08814496 360.84887 .04834995 1 1951 10 .41646846 .34352702 213.79429 .09035704 1 1952 10 .38796888 .31146499 119.85717 .11954148 1 1953 10 .69741758 .65959478 -284.60313 .22296989 1 1954 10 .67138447 .63030752 -496.6066 .28415839

As mentioned above, asreg does not wait for the full window to get the required number of period. Therefore, results from the rolling command and asreg start to match only from the 10th observation,  i.e., the year 1944. If you like asreg to ignore observation unless the minimum number of periods are available, you can use the option min. So to match the results with the rolling command, we can type:

`bysort company: asreg invest mvalue, wind(year 10) min(9)`

and there you go, asreg produces the same coefficients as the rolling command, with blistering speed.

In-text citation

Rolling regressions were estimated using asreg, a Stata program written by Shah (2017).

Bibliography

Shah, Attaullah, (2017), ASREG: Stata module to estimate rolling window regressions. Fama-MacBeth and by(group) regressions, https://EconPapers.repec.org/RePEc:boc:bocode:s458339.

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