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

rollingStata code with yourasergprogram on a small dataset, I won’t get the same results.

### Answer

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 periods. 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.

Please do cite asreg in your research

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

Karina van KuijkJune 11, 2018 at 10:29 pmDear mr. Shah,

Thank you very much for your detailed post.

Regards,