Thanks to Kit Baum, I have shared a new package on SSC, to describe and install, type
TITLE
'ASROL': module to generate rolling-window descriptive statistics in time series or panel data
DESCRIPTION
This program calculates descriptive statistics in a user's
defined rolling-window. For example, in time series or panel
data, a user might be interested in knowing standard deviation
or coefficient of variation of a variable in a rolling window of
last 4 years. asrol provides efficient and simple way of
finding such statistics. It also offers various options to
specify minimum number of observations required for
calculation of desired statistics in a rolling window.
KW: descriptive statistics
KW: rolling window
Requires: Stata version 11
Distribution-Date: 20150905
Author: Attaullah Shah, Institute of Management Sciences
Support: email [email protected]
Title
asrol - Generates rolling-window descriptive statistics in
time series or panel data
Syntax
asrol varlist [if] [in] , gen(newvar) stat(statistic) window(#) [ nomiss minmum(#)]
Syntax Details
The program has 3 required options: They are
1. gen : to generate new variable, where the variable name is enclosed in parenthesis after gen
2. stat: to specify required statistics. The following statistics are allowed;
3. window :is to specify the length of rolling window for calculation of the
required statistics. The length of window should be less than or equal to the total number
of time-series observations per panel.
Other Options
1. nmiss
The option nomiss forces asrol to find required statistics
with all available observations, which results in no missing values at the
start of the panel. Compare results of Example 1 below where n is not used with the results of
the Example 3 where n is used.
In the example 1, 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.
2. minmum
The option minmum 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.
Example 1: Find Rolling Mean
This command calculates mean for the variable invest using
a four years rolling window and stores the results in a new variable called
mean_4.
Example 2: Find Rolling Standard Deviation
This command calculates standard deviation for the
variable invest using a six years rolling window and stores the
results in a new variable called sd_6
Example 3: For Rolling Mean with no missing values
This command calculates mean for the variable invest using
a four years rolling window and stores the results in a new variable
called mean_4. The nomiss option forces asrol to find mean with
all available observation, which results in no missing
values at the start of the panel. Compare results where
nomiss is not used in example 1 above.
In the example 1, 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.
Example 4: Rolling mean with minimum number of observaton
Example 5: Rolling mean with minimum number of observation including the start of the panel
This command forces asrol to calculate mean for the variable
invest using a four years rolling window and stores the results in a new variable
called mean_4. The n option and the min(3) force asrol to find mean with at least 3
available observations even at the start of each panel i.e. asrol will not wait
until 4 observations are are available, it will start calculation when at least
three observations are available.
Code:
ssc desc asrol ssc install asrol
'ASROL': module to generate rolling-window descriptive statistics in time series or panel data
DESCRIPTION
This program calculates descriptive statistics in a user's
defined rolling-window. For example, in time series or panel
data, a user might be interested in knowing standard deviation
or coefficient of variation of a variable in a rolling window of
last 4 years. asrol provides efficient and simple way of
finding such statistics. It also offers various options to
specify minimum number of observations required for
calculation of desired statistics in a rolling window.
KW: descriptive statistics
KW: rolling window
Requires: Stata version 11
Distribution-Date: 20150905
Author: Attaullah Shah, Institute of Management Sciences
Support: email [email protected]
Title
asrol - Generates rolling-window descriptive statistics in
time series or panel data
Syntax
asrol varlist [if] [in] , gen(newvar) stat(statistic) window(#) [ nomiss minmum(#)]
Syntax Details
The program has 3 required options: They are
1. gen : to generate new variable, where the variable name is enclosed in parenthesis after gen
2. stat: to specify required statistics. The following statistics are allowed;
Code:
sd : for standard deviation mean : for mean total : for sum or total median : for median pctile : for percentiles min : for minimum max : for maximum
required statistics. The length of window should be less than or equal to the total number
of time-series observations per panel.
Other Options
1. nmiss
The option nomiss forces asrol to find required statistics
with all available observations, which results in no missing values at the
start of the panel. Compare results of Example 1 below where n is not used with the results of
the Example 3 where n is used.
In the example 1, 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.
2. minmum
The option minmum 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.
Example 1: Find Rolling Mean
Code:
. webuse grunfeld . asrol invest, stat(mean) win(4) gen(mean_4)
a four years rolling window and stores the results in a new variable called
mean_4.
Example 2: Find Rolling Standard Deviation
Code:
. webuse grunfeld . asrol invest, stat(sd) win(6) gen(sd_6)
variable invest using a six years rolling window and stores the
results in a new variable called sd_6
Example 3: For Rolling Mean with no missing values
Code:
. webuse grunfeld . asrol invest, stat(mean) win(4) gen(sd_4) nomiss
a four years rolling window and stores the results in a new variable
called mean_4. The nomiss option forces asrol to find mean with
all available observation, which results in no missing
values at the start of the panel. Compare results where
nomiss is not used in example 1 above.
In the example 1, 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.
Example 4: Rolling mean with minimum number of observaton
Code:
. webuse grunfeld . asrol invest, stat(mean) win(4) gen(mean_4) min(3)
Code:
. webuse grunfeld . asrol invest, stat(mean) win(4) gen(mean_4) min(3) nomiss
invest using a four years rolling window and stores the results in a new variable
called mean_4. The n option and the min(3) force asrol to find mean with at least 3
available observations even at the start of each panel i.e. asrol will not wait
until 4 observations are are available, it will start calculation when at least
three observations are available.
Comment