version: 4.8
help asreg Dec 23, 2022

Title

asreg - Rolling window regressions and by(group) regressions

Syntax

[bysort varlist:] asreg
depvar indepvars [if] [in]
[, window(rangevar range_low range_high)
noconstant:
recursive:
minimum:(#)
by(varlist)

newey(#)
fmb
save(file name)
first
exclude(rangevar range_low range_high)]

Description

asreg fits a model of depvar on indepvars using linear regression
in a user's defined rolling window or by a grouping variable. 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.

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

What's New in Version 4.0

1. Changes made to the window() option

Version 4.0 of asreg introduces a more flexible window to identify observation
in a given range. Previously, the window() option of asreg
would take two inputs: the first one as the range variable (such as date)
and the second one as the length of the rolling window. In the older versions, the window
would always look backward. This has changed in version 4.0.

The window argument can now take up to three arguments. The window can now look
backward, forward, and both back and forward. More details can be read in the following section
under the option window()

2. Improved algorithm for rolling window indices

This version of asreg (version 4.0) significantly improves the
calculation speed of the required statistics,
thanks to the development of a more efficient algorithm for extracting rolling
window indices. This has resulted in a significant speed advantage for asreg compared
to its previous versions or other available programs. The speed efficiency matters
more in larger datasets.

Web Page

There is a dedicated
web page
of asreg on my website
www.FinTechProfessor.com.
The webpage lists different helping material related to asreg. These materials
include YouTube videos, blog posts on different situations where asreg
can be used, and some questions and answers. Further, I aslo maintain
a users' community forum where questions related to asreg can be answered.

Regression statistics and their names

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. New variables
from the regression results follow the following naming conventions:

Regression statistics and their names

observations variable containing number of observation is named as _N

regression slopes a prefix of _b_ is added to the name of each independent variables

constant variable containing constant of the regression is names as _b_cons

r-squared r-squared and adj. r-squared are named as R2 and AdjR2 , respectively

RMSE root-mean-square error. This variable is named as _rmse.

standard errors a prefix of _se_ is added to the name of each independent variables. asreg can estimate three types of coefficient errors: See Section 6 below

residuals variable containing residuals is named as _residuals

fitted variable containing fitted values is named as _fitted.

Options

asreg has the following options.

1. The window():

The latest version of asreg accepts up to three
arguments and is written like this:

window(rangevar #from #upto)

The rangevar is usually a time variable such as date, monthly date,
or yearly date. However, it can also be any other numeric variable, such as age,
income, industry indicator, etc.
Both #from and #upto are numeric values that set the lower and upper
bounds on the rolling window, using the current value of the rangevar as a benchmark.
Negative values of these inputs mean going back # periods from the current
value of the rangevar. Similarly, positive values of these imply going ahead # periods
of the current value of the rangevar.

Please note:

1. asreg considers the focal observation as part of the past.

2. Given the legacy of asreg, a window() with two inputs is still supported.
However, the following rule should be noted:

If option window() has two arguments, it shall always look back.
In other words, if option window() has two arguments, then the second argument
will always be counted as a negative number. Therefore, the following
window() options mean the same:

window(year 50)

window(year -50 0)

window(year -50)

In the following paragraphs, I present some examples to make it easier to understand
option window().

Window examples and interpretations

The following table presents examples of the window() option and its interpretations.

window examples

window(year -5 0) A rolling window of past 5 observations.
Therefore, if the focal year is 2006, the window will include these years in the
calculations: 2006, 2005, 2004, 2003, 2002.
The window can also be written as window(year 5)

window(year 1 5) A rolling window of 5 lead-observations.
Therefore, if the focal year is 2006, the window will include these years in the
calculations: 2007, 2008, 2009, 2010, 2011.

window(year -13 -2) A rolling window of past 11 observations
from t-12 up to t-2. Therefore, if the focal year is 2006,
the window will include these years in the calculations: 1994, 1995, 1996,
1997 , 1998, 1999, 2000, 2001, 2002, 2003, 2004.
The window can also be written as window(year 5)

window(year -5 5) A rolling window of 10 observations. Therefore,
if the focal year is 2006, the window will include these years in the calculations:
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011. The window
can also be written as window(year 5)

Note on missing values in the rangevar Some posts on the Statalist
show confusion concerning the interpretation of the option window().
For example, using window(year -5), a user might incorrectly assume that
the observation in this range will always be 5. This might not be true in a
case where the rangevar has gaps. So if the current value of the rangevar
(in our case year) is 2006 and its previous values are 2000, 2001, 2003, 2005.
Though the window asks for 5 observations within the range of 2006, the available
values of the rangevar are only 2006, 2005, 2003, which are only 3 observations. The window
range ends at 2002, other values of the year are outside the window bounds.

For more discussion and examples on the option window, see this web page.

2. Cross-sectional regressions(When option window() is not used)

NOTE: Option window is an optional option, and hence it can be dropped
altogether. This is useful when we are interested in by-group or cross-sectional regressions without
using a rolling window. For example, if we have a large cross-section of industries
or firms and we wish to estimate a separate regression for each of the cross-sections,
we shall drop option window. See Example 6 below.

3. recursive: The option recursive specifies that a recursive
window be used. In time series analysis, a recursive window refers to
a window where the starting period is held fixed, the ending period advances,
and the window size grows (see, for example, rolling). asreg
allows a recursive window either by invoking the option recursive or
setting the length of the window greater than or equal to the sample size per group.
For example, if the sample size of our data set is 1000 observation per group, we
can use a recursive analysis by setting the window length equal to 1000
or greater than 1000

4. by: asreg is byable. Hence, it can be run on
groups as specified by option by(varlist) or the bysort
varlist: prefix.
An example of such regression might be
Fama and MacBeth (1973) first stage regression, which is estimated
cross-sectionally in each time period. Therefore, the grouping variable
in this case would be
the time variable. Assume that we have our dependent variable named
as stock_returns, independent variable as stock_betas, and time variable as
month_id, then to estimate the cross-sectional
regression for each month, asreg command will look like this:

. bys month_id: asreg stock_return stock_betas

5. minimum(#): asreg estimates regressions where the number of observations is greater than the number of regressors.
However, the option minimum(#) can be used to specify the minimum number of observations for a given regression, where # is an integer.
If option min(#) is used, asreg then finds the required number of observation for the regression estimated such that :

obs = max(number of regressors (including the intercept), minimum observation as specified by the option min).

For example, if we have 4 explanatory variables, then the number of regressors will be equal to 4 plus 1 i.e. 5.
Therefore, if option min(8) is used, the required number of observations will be : max(5,8) = 8. If a specific
rolling window does not have that many observations, the values of the new variable will be replaced with missing values.

Statistics_Options

fitted reports residuals and fitted values for the last observation in the rolling window.
If option window is not specified, then the residuals are calculated within each group as specified by the option by(varlist) or the bysort varlist:

serror reports standard errors for each explanatory variable

rmse reports root-mean-squared error of OLS regression

noconstant suppresses the constant term (intercept) in the model (this option is not available in the Fama-MacBeth regression i.e., when using option fmb

other Most commonly used regression statistics such as number of observations, slope coefficients, r-squared, and adjusted r-squared are
written to new variables by default. Therefore, if these statistics are not needed, they can be dropped once asreg is estimated.

6. Coefficient errors: asreg can estimate three types
of coefficient errors. The first type is the standard errors. Standard errors are
the default option in asreg. They are reported only when option se is used.
The second type is the
robust standard errors
which are reported when option robust is used (this option is not
available in the Fama-MacBeth regression i.e., when using option fmb.
The third type is the Newey-West standard errors which are
reported when option newey(#) is used, see
further details here.

7. exclude(rangevar #low #high): Option exclude() is
used to exclude specific observations from regressions. This option requires
three arguments, with or without using the comma character. The first is the range variable
such as daily, weekly, monthly or yearly date; the second and third arguments
are the lower and upper bounds of th range variable. The definitions and working
of option exclude(rangevar #low #high) are similar to that of the option
window(rangevar #from #upto).

exclude examples

exclude(year 0 0) Exclude focal observation. Therefore, if
the focal year is 2006, it will be excluded from the regression.
Also note if the focal year has duplicates, they shall also be excluded.

exclude(year 2 2) Exclude 5 observations. Assume that the
focal year is 2020. Observations pertaining to
the years 2018, 2019, 2020, 2021, and 2022 will be excluded.

exclude(year -5 0) Exclude 6 observations. Assume that the
focal year is 1940. Observations pertaining to the
year 1935, 1936, 1937, 1938, 1939, and 1940 will be excluded.

exclude(year 0 5) Exclude 6 observations. Assume that the
focal year is 1940. Observations pertaining to the
years 1940, 1941, 1942, 1943, 1944, 1945, and 1945 will be excluded.

FMB: Fama and McBeth(1973) regression

Option fmb applies a two-step Fama-McBeth procedure.

• The first step involves estimating N cross-sectional regressions.

• The second step involves taking T time-series averages of the coefficients from the N cross-sectional regressions.

The standard errors are adjusted for cross-sectional dependence. This is generally an acceptable solution when there is a large number of cross-sectional units and a relatively small time series for each cross-sectional unit. However, if both cross-sectional and time-series dependence are suspected in the data set, then Newey-West consistent standard errors can be used. asreg uses the first method as a default.

asreg with option fmb is very similar to xtfmb program written by Daniel Hoechle. However, there are
three major differences:

1. asreg estimates the cross-sectional regressions using Mata language, while xtfmb does that using the statsby command. This difference in implementation can have a significant impact on the run time of the two programs, with asreg typically being much faster for large datasets.

2. asreg allows estimation of Newey-West standard errors even if the data set has time-series gaps. xtfmb, on the other hand, will exit with an error message if the data has time-series gaps.

3. asreg allows saving the coefficients of the first stage regression to a file using the save(filename) option. This can be useful for debugging or for further analysis.

6. save(filename): Option save is used only when option fmb is used. Option save saves the first stage regression coefficients of the Fama-McBeth
regression to a file.

7. newey(integer): newey specifies the number of lags for estimation of Newey-West consistent standard errors. asreg allows option newey to be used in both the rolling
regressions and Fama-McBeth regressions. In the rolling regressions, newey will work only when option se is used. Also, please note that without using option newey,
option se estimates normal standard errors of OLS. This option accepts only integers, for example newey(1) or newey(4) are acceptable, but newey(1.5)
or newey(2.3) are not.

8. first: This option will work only when option fmb is invoked. Option first displays on the screen the first-stage regression coefficients of the Fama-McBeth regressions.

Examples

Example 1: Regression for each company in a rolling window of last 10 years

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10)

OR

. bys company: asreg invest mvalue kstock, wind(year -10 0)

Example 2: Regression for each company in a rolling window of 10 years; 5 past and 5 forward

. bys company: asreg invest mvalue kstock, wind(year -5 5)

Example 3: Regression for each company in a rolling window of 10 leading years

. bys company: asreg invest mvalue kstock, wind(year 0 10)

Example 4: Regression for each company in a recursive window

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10) rec

OR

. bys company: asreg invest mvalue kstock, wind(year 1000)

Example 5: Using option minimum - Limit regression to a minimum of 5 observations

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10) min(5)

Example 6: Reporting standard errors

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10) se

Example 7: Newey-West standard errors, lag(1)

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10) se newey(1)

Example 8: Robust standard errors

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10) robust

Example 9: Reporting standard errors, fitted values and residuals

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10) se fit

Example 10: Regressions without constant

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, wind(year 10) noc

Example 11: No window - by groups regressions

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock

Example 12: Yearly cross-sectional regressions

. webuse grunfeld, clear

. bys year: asreg invest mvalue kstock

Example 13: Rolling regression - reporting RMSE

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, w(year 10) rmse

Example 14: Fama and McBeth Regression

. webuse grunfeld, clear

. asreg invest mvalue kstock, fmb

Example 15: Fama and McBeth Regression - using Newey-West errors

. webuse grunfeld, clear

. asreg invest mvalue kstock, fmb newey(1)

Example 16: Fama and McBeth Regression - report first stage regression

. webuse grunfeld, clear

. asreg invest mvalue kstock, fmb first

Example 17: Fama and McBeth Regression - save first stage results

. webuse grunfeld, clear

. asreg invest mvalue kstock, fmb save(FirstStage)

Example 18: Exclude focal observation in cross-sectional regressions

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, exclude(year 0 0)

Example 19: Exclude 5 most recent observations (the focal observation and 4 others from the recent past)

. webuse grunfeld, clear

. bys company: asreg invest mvalue kstock, exclude(year -4 0)

Stored Results from option FMB

asreg returns the following scalars, macros and matrices in eclass for Fama-McBeth regressions.

scalars:
e(N) = number of observation
e(N_g) = number of groups
e(df_r) = degrees of freedom
e(F) = F-test value
e(r2_a) = average r-squared

macros:
e(avgr2) : "average r-squared"
e(cof1) : "coefficient of the first independent variable"
e(cmd) : "command name"
e(method) : "Fama-MacBeth Two-Step procedure"
e(depvar) : "invest"
e(vcetype) : "Fama-MacBeth"
e(properties) : "b V"

matrices:
e(b) : vector of slopes
e(V) : variance-covariance matrix

Author

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *
* *
* Dr. Attaullah Shah *
* Institute of Management Sciences, Peshawar, Pakistan *
* Email: attaullah.shah@imsciences.edu.pk *
* www.OpenDoors.Pk *
* www.StataProfessor.com *
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*

Also see

Program

asdocx Flexible, yet extremely powerful package for publication quality tables

asm for momentum portfolios

asdoc f creating high-quality tables in MS Word from Stata output

asgen for weighted average mean

asrol for rolling window statistics

ascol for coverting share prices and returns from daily to weekly or monthly frequency

searchfor for searching text in data sets

fillmissing Fills missing values in a variable based on a given criterion