asrol - help
asrol - help
version: 2.0.0
help asrol 21 Feb 2017
------------------------------------------------------------------------------------------------------------------------
Title
asrol - Generates rolling-window descriptive statistics
Syntax
asrol varlist [if] [in], stat(statistic) window([rangevar] # ) [ gen(newvar) smiss by(varlist) minimum( # ) ]
The underlined letters signifies that users can abbreviate the full words only to the underlined letters.
Description
asrol calculates descriptive statistics in a user's defined rolling-window. asrol efficiently handles all types of
data structures such as data declared as time series or panel data, undeclared data, or data with duplicate
values, missing values or data having time series gaps.
asrol uses efficient codings in the Mata language which makes this version extremely fast as compared to other
available programs. The speed efficiency matters more in large data sets. This version also overcomes limitation
of th r version of asrol which could calculate statistics in a rolling window of 104. asrol can accoomodate any
length of the rolling window.
Syntax Details
The program has 2 required options: They are:
1. stat: to specify required statistics. The following statistics are allowed;
sd for standard deviation
mean for mean
sum for sum or total
median for median
count for counting number of non-missing observations in the given window
missing for counting number of missing observations in the given window
min for finding minimum value in the given window
max for finding maximum value in the given window
first for finding the first observation in the given window
last for finding the last observation in the given window
2. window: 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, asrol 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 time
series or panel, then we have to specify the time variable as first argument of the option window. For example, if
our time variable is year and we want a rolling window of 5, then option window will look like: window(year 5)
Optional Options
1. gen: This is an optional option to generate new variable, where the variable name is enclosed in paranthesis
after gen. If we do not specify this option, arsol2 will automatically generate a new variable with the name
format of stat_varname.
2. smiss
The option smiss forces asrol to omit required statistics at the start of the rolling window. Compare results of
Example 1 below with the results of Example 3 where we use option smiss. In Example 3, asrol finds mean starting
with the fourth observation of each panel, i.e. the rolling window does not start working unless it reaches the
required level of 4 observations.
3. minmum
The option min forces asrol to find required statistics where the minimum number of observations are available. If
a specific rolling window does not have that many observations, values of the new variable will be replaced with
missing values.
4. by( varlist )
asrol is byable and hence the rolling statistics can be calculated using a single variable as sorting filter or
using multiple variables. For example, we can find mean profitability for each company in a rolling window of 5
years. Here, we use a single filter, that is company. Imagine that we have a data set of 40 countries, each one
having 60 industries, and each industry has 1000 firms. We might be interested in finding mean profitability of
each industry within each country in a rolling window of 5 years. In that case, we shall use the option by as
shown below:
asrol profitability, window(year 5) stat(mean), by(country industry)
Example 1: Find Rolling Mean
. webuse grunfeld
. asrol invest, stat(mean) win(4)
This command calculates mean for the variable invest using a four years rolling window and stores the results in a
new variable, mean4_invest.
Example 2: Find Rolling Standard Deviation
. webuse grunfeld
. asrol invest, stat(sd) win(6)
This command calculates standard deviation for the variable invest using a six years rolling window and stores the
results in a new variable , sd4_invest
Example 3: For Rolling Mean with missing values at start of each panel
. webuse grunfeld
. asrol invest, stat(mean) win(4) smiss
This command calculates mean for the variable invest using a four years rolling window and stores the results in a
new variable , mean4_invest. The smiss option forces asrol to skip calculation of mean at the start of each panel,
unless the legnth of the rolliwng window is reached. Compare these results with the results reported in Example 1
above. In Example 1, asrol calculates mean values right from the first observation, adds new observation to the
rolling window untill the length of the rolling window is reached.
Example 4: Rolling mean with minimum number of observaton
. webuse grunfeld
. asrol invest, stat(mean) win(4) min(3)
Example 5: Rolling mean with minimum number of observaton including the start of the panel
. webuse grunfeld
. asrol invest, stat(mean) win(4) min(3) smiss
Example 6: Using by option for two or three variables
We shall generate a dummy data of 5 countries, 50 industries, 50 years, and 5000 firms for further examples.
clear
set obs 50
gen industry=_n
gen year=_n+1917
expand 5
bys industry: gen country=_n
expand 1000
bys ind: gen company=_n
gen profit=uniform()
Mean by country and industry in a rolling window of 10 years
. asrol profit, stat(mean) win(year 10) by(country industry)
NOTE: Since the data cannot be declared as panel data on the basis of country and industry, we have to specify the
range variable in the window option.
Author
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *
* *
* Dr. Attaullah Shah *
* Institute of Management Sciences, Peshawar, Pakistan *
* Email: attaullah.shah@imsciences.edu.pk *
* www.OpenDoors.Pk *
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
Also see
rolling, mvsumm, ascol, asreg, astile
Acknowledgements
For creating group identifiers, I could have used egen's function, group. But for speed efficiency, Nick Cox's
solution of creating group idnetifier was preffered( See here). For finding median in the Mata language, I used
the approach suggested by Daniel Klein, ( See here)