Thanks to Kit Baum, a new package tsegen (with Nick Cox) is now available from SSC.
With tsegen, you can invoke any egen function that requires a varlist 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.
tsegen requires Stata version 10 or higher. To install, type:
tsegen is notably useful for computing descriptive statistics over a rolling window of time using egen row functions. By using a set of lagged variables, tsegen can compute the statistic directly and is orders of magnitude faster than other approaches that require looping over subsets of observations. For example, you can calculate the mean over a 5-year window that includes the current observation
See help tsvarlist for more information on various ways to specify a set of operated variables. Since missing values are ignored, a non-missing mean may be based on a single observation. With tsegen, you can require, for example, a minimum of 3 non-missing observations
Here's another example that calculates the standard deviation over a 3-year moving window, with and without the current observation
tsegen can also be used with windows of time that span periods before and after the current observation. For example, you can apply a smoothing filter over 5 periods centered on the current observation using
The two commands perform exactly the same computation but preliminary testing suggests tsegen is much faster.
tsegen is not limited to egen functions that ship with Stata. If you have egenmore installed (from SSC), you can, for example, tag observations that reflect 3 consecutive years of positive growth in market value:
With tsegen, you can invoke any egen function that requires a varlist 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.
tsegen requires Stata version 10 or higher. To install, type:
Code:
ssc install tsegen
Code:
webuse grunfeld, clear tsegen inv_m5 = rowmean(L(0/4).invest)
Code:
tsegen inv_m5m3 = rowmean(L(0/4).invest, 3)
Code:
tsegen inv_sd3 = rowsd(L(0/2).invest) tsegen inv_sd3L = rowsd(L(1/3).invest)
Code:
webuse sales1, clear tsegen sm = rowmean(L(0/2).sales F(1/2).sales) tssmooth ma sm1=sales, window(2 1 2)
tsegen is not limited to egen functions that ship with Stata. If you have egenmore installed (from SSC), you can, for example, tag observations that reflect 3 consecutive years of positive growth in market value:
Code:
webuse grunfeld, clear gen double diff = D.mvalue tsegen pg = rall(L(0/2).diff,3) , c(@ > 0)
Comment