This is a quick overview of how to use asreg command for running regressions by groups. To know about the installation and other details of asreg, please click here.

Regressions by groups in Stata

A group is a subset of data that has a common identifier. Examples of groups can include families, industries, countries, regions, months, years, portfolios, etc. If a linear regression needs to be estimated separately for each group, asreg is the best option. asreg is blinking fast and writes all regression outputs to the data in memory as separate variables. This eliminates the need for writing the results to a separate file which is usually then merged  back to the data for any further calculations. asreg eliminates all these steps and provides convenient and clean solution to by-group regression and rolling regressions. IF not installed, asreg can be installed from ssc with this line of code:

ssc install asreg

An Example

We shall use the grunfeld data set in our example. Let’s download it first:

webuse grunfeld
Assume that we want to estimate a regression by year (in this example, year is our grouping variable) where the dependent variable is invest and independent variables are mvalue and kstock. Just like regress command, asreg uses the first variable as dependent variable and rest of the variables as independent variables. Using the grunfeld data, asreg command  for estimating yearly regression would be:
bysort year: asreg invest mvalue kstock
Please note that the grouping variable year comes after the bysort prefix which is followed by full colon. Similarly, if we want to estimate the above regression for each company, our grouping variable would be company that will come after the bysort prefix:
bys company: asreg invest mvalue kstock

* List the estimated cofficients

sort year company

list company year _Nobs _R2 _adjR2 _b_mvalue _b_kstock _b_cons in 1/10

results of the regression over groups in Stata fastest asreg

Standard errors and fitted values

If standard errors are needed for each explanatory variable, we shall just add the otpion se. Similarly, we can add option fit if fitted values are required.

bys company: asreg invest mvalue kstock se

bys company: asreg invest mvalue kstock se fit