Hi everyone

Thanks to Kit Baum for uploading version 1 of asreg on SSC. The program can be downloaded by typing
. Short description and examples of asreg are presented below:

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 sets. As a speedy alternative, I have written asreg that fits a model of

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.

asreg writes all regression outputs to the data in memory as separate variables. This eliminates the need for writing results to a separate file, and then merging those results back to the data for further processing. New variables from the regression results follow the following naming conventions:

asreg has the following options.

Option

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

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

asreg estimates regressions where number of observations are greater than number of regressors. However, there is a way to limit the regression estimates to a desired number of observations. The option minimum can be used for this purpose. 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 asreg receives the the value of 8 from the option min, the required number of observations will be : max(5,8) = 8. If a specific rolling window does not have that many observations, values of the new variable will be replaced with missing values.

the residuals are calculated withing each group as specified by the option by(varlist) or the bysort varlist:

are written to new variables by default. Therefore, if these statistics are not needed, they can be dropped once asreg is

estimated.

The following examples can be easily run by clicking the clickable links inside the help file of asreg.

.
The grunfeld data set is a panel data set, so we can omit the word year from the option window. Therefore, the command can also be estimated

as shown below:

.

OR

.

.

.

.

.

Please note: The beta version leaves behind a variable TimeDiff, which I shall work upon and upload a revised version soon to SSC .

Since this is a beta version, I would appreciate any bug reporting or suggestions on what other options can be added.

Thanks to Kit Baum for uploading version 1 of asreg on SSC. The program can be downloaded by typing

Code:

ssc install asreg

**Description**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 sets. As a speedy alternative, I have written asreg that 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 (ssc des asrol). All calculations of the rolling windows, 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.

asreg writes all regression outputs to the data in memory as separate variables. This eliminates the need for writing results to a separate file, and then merging those results back to the data for further processing. New variables from the regression results follow the following naming conventions:

**observations**variable containing number of observation is named as**obs_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**standard errors**a prefix of**_se_**is added to the name of each interdependent variables**residuals**variable containing residuals is named as**_residuals****fitted**variable containing fitted values is named as**_fitted**.**Options**asreg has the following options.

**1. window:**Option

__indow(__**w***rangevar #)*specifies length of the rolling window. The window option accepts up to two arguments. If we have already declared our data as panel or time series data, asreg will automatically pick the time variable. In such cases, option window can have one argument, that is the length of the window, e.g.,**window(5)**. If our data is not time series or panel, then we have to specify the time variable as a first argument of the option window. For example, if our time variable is year and we want a rolling window of 24, then option window will look like:Code:

window( year 24)

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

**3. 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) second 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:Code:

. bys month_id: asreg stock_return stock_betas

**4. minimum:**asreg estimates regressions where number of observations are greater than number of regressors. However, there is a way to limit the regression estimates to a desired number of observations. The option minimum can be used for this purpose. 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 asreg receives the the value of 8 from the option min, the required number of observations will be : max(5,8) = 8. If a specific rolling window does not have that many observations, 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, thenthe residuals are calculated withing each group as specified by the option by(varlist) or the bysort varlist:

**serror**reports standard errors for each explanatory variable**other**Most commonly used regression statistics such as number of observations, slope coefficients, r-squared, and adjusted r-squaredare written to new variables by default. Therefore, if these statistics are not needed, they can be dropped once asreg is

estimated.

**Examples**The following examples can be easily run by clicking the clickable links inside the help file of asreg.

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

Code:

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

as shown below:

.

Code:

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

**Example 2: Regression for each company in a recursive window**Code:

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

.

Code:

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

**Example 3: Using option minimum**.

Code:

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

**Example 4: Reporting standard errors**.

Code:

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

**Example 5: Reporting standard errors, fitted values and residuals**.

Code:

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

**Example 6: No window - by groups regressions**.

Code:

webuse grunfeld . bys company: asreg invest mvalue kstock

**Example 7: Yearly cross-sectional regressions**Code:

webuse grunfeld . bys year: asreg invest mvalue kstock

Since this is a beta version, I would appreciate any bug reporting or suggestions on what other options can be added.

## Comment