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
Thank you for that neat program! Is there a way to use Newey West t statistics in the regression?
Thanks
Markus
Thanks 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.
Dear Markus, the newey option has been added to asreg now. You can update the version of asreg from ssc by
adoupdate asreg, update
Thank you for your help
I have used asreg in an unblalnced panel data.
How do I use Random Effects?
Dear George, thanks for your inquiry. Actually, asreg calculates OLS objects. However, you can use the xtdata command to convert your data to a form suitable for random-effects estimation. After transformation, you can then apply asreg.
Hi,
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.
Forward looking window is not yet supported by asreg, but I am working on it and expect to add it in a year time.
Dear 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
Xi
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
Hello 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
Dear 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
Peter
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/
Dear 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!
Dear 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
Respected 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
Dear Prof. Attaullah Shah,
I was interested in calculating inflation persistence for multiple countries which is usually calculated by AR(1) coefficients, and I calculated it using asreg wherein I regressed inflation on its first lag for all the countries, “
” however when I use the rolling option in stata for calculating rolling regression which is “
” and doing it for all the countries separately since it does not allow the “by” option, the results are varying, please guide further as to which method should be used or where I might be wrong.
Thank You
For a helpful comparison of
rolling
andasreg
, please follow this link https://fintechprofessor.com/2018/06/11/stata-rolling-command-vs-asreg-for-rolling-regressions-similarities-and-differences/Hi Professor,
Thank you for the beautiful work and kind sharing.
I applied it to my financial data and plan to set a rolling period of 90(daily data), but the _Nobs cannot get 90 and are always different. I don’t know whether it is because the gap due to non-working days and missing data, cause the example of time data of company 1 is continuous. Thanks a lot!
I have already answered it. In financial data, Saturday and Sundays are off, so you get 21 observation in a month instead of 30. asreg does not adjust for financial data. Therefore, you have to adjust the window, and make it 63 instead of 90.
Hi Professor,
Thank you for the beautiful work and kind sharing.
I applied it to my financial data and set a rolling period of 90, but the result _Nobs cannot get the 90(max 60 approximately) and are always different from others. I do not know whether it is because the financial data have the non-working-day gap and other missing data, cause asreg can recognize the gap automatically. So, does asreg have the ready-made command to skip and ignore the gap? Thanks a lot!
In financial data, Saturday and Sundays are off, so you get 21 observation in a month instead of 30. asreg does not adjust for financial data. Therefore, you have to adjust the window, and make it 63 instead of 90.