*! version 1.1.0 30may2015
help tsegen

Title

tsegen -- Invoke an egen function using a tsvarlist as argument

Syntax

tsegen [type] newvar = fcn(tsvarlist[, min_obs]) [if] [in]
[, options]

where fcn is an egen function that expects a
varlist.
Optionally, results based on fewer than min_obs
non-missing observations are replaced with missing values.
options are fcn dependent.
by is not allowed.

Description

A number of egen functions
(both built-in and user-written, e.g. egenmore)
expect a varlist
and perform observation-level calculations based on
the variables provided as argument.
With tsegen, you can invoke any of these egen functions
using a time-series varlist (tsvarlist)
instead.
tsegen converts the tsvarlist
to a varlist
by substituting equivalent temporary variables as necessary
and then invokes the specified egen function.

To use time-series operators,
the data must be declared to be a time series (with or without a panel
variable) using either tsset or xtset.

If the egen function ignores missing values,
you can specify that the results
be based on a minimum of non-missing values using the min_obs option.

Descriptive statistics over a rolling window examples

Calculate the mean of the variable invest over a 5-year
rolling window that includes the current observation

. webuse grunfeld, clear
. tsegen inv_m5 = rowmean(invest L(1/4).invest)

Note that the above can also be written as

. tsegen inv_m5b = rowmean(L(0/4).invest)

Since missing values are ignored, a non-missing mean may be based on a single
observation. To require a 3 year minimum

. tsegen inv_m5m3 = rowmean(L(0/4).invest, 3)

To find the minimum and maximum investment within the 5-year window, again
ignoring windows with fewer than 3 observations

. tsegen inv_min5m3 = rowmin(L(0/4).invest, 3)
. tsegen inv_max5m3 = rowmax(L(0/4).invest, 3)

Calculate the standard deviation over a rolling 3-year window. The
first example includes the value of the current observation in the
rolling window. The second example calculates the standard deviation
for the current observation based on lagged values for the 3 preceding
periods.

. tsegen inv_sd3 = rowsd(L(0/2).invest)
. tsegen inv_sd3L = rowsd(L(1/3).invest)

Coefficient of variation example

Calculate the coefficient of variation in market value over a
rolling window of 5 years (for representation as percent,
multiply further by 100)

. webuse grunfeld, clear
. tsegen mysd = rowsd(L(0/4).mvalue,5)
. tsegen mymean = rowmean(L(0/4).mvalue,5)
. gen mycv = mysd / mymean

Data smoothing example

Smooth data using a uniformly weighted moving average

. webuse sales1, clear
. tsegen sm = rowmean(L(0/2).sales F(1/3).sales)

which is the equivalent of

. tssmooth ma sm1=sales, window(2 1 3)

Geometric mean example

Compare market value over a 3 year window using
arithmetic and geometric means.

. webuse grunfeld, clear

Calculate the arithmetic mean

. tsegen amean = rowmean(L(0/2).mvalue,3)

Calculate the geometric mean

. gen double log_mvalue = log(mvalue)
. tsegen double gmean = rowmean(L(0/2).log_mvalue,3)
. replace gmean = exp(gmean)

Other examples

Identify observations that reflect 3 consecutive years of positive growth in
market value. This requires the rall function
from egenmore

. webuse grunfeld, clear
. gen double diff = D.mvalue
. tsegen pg = rall(L(0/2).diff,3) , c(@ > 0)

Note that the above example can be shortened to

. webuse grunfeld, clear
. tsegen pg2 = rall(L(0/2)D.mvalue,3) , c(@ > 0)

Thanks to Sebastian Kripfganz for pointing this out on
Statalist.

Limitations

All flavors of Stata impose a limit of 100 unique time-series operators
per command (see help limits).
This version of tsegen overcomes this limit. For example,
a rolling window of 200 periods can be specified using L(1/200).return.
See help limits for the maximum number of variables allowed with
your version of Stata.

Authors

Robert Picard

picard@netbox.com

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

Also see

Stata: egen, rolling, tssmooth, tsvarlist, tsrevar

Article: Cox, Nicholas J. 2009. Speaking Stata: Rowwise, Stata Journal 9: 137-157.

SSC: mvsumm, rollstat, egenmore

Others: //sites.google.com/site/imspeshawar" "':asrol