help rollstat



rollstat -- Rolling-window statistics for time series or panel data


rollstat [varlist] [if] [in] , statistic(statname) [ options ]

options Description ------------------------------------------------------------------------- Main statistic(statname) report specified statistic (required option).

Options w(#) number of consecutive data points in each sample (it should be an integer grater than one), default w(3). force forces results to be computed when some of a particular window's values are missing. ------------------------------------------------------------------------- User must tsset for time series or panel data before using rollstat; see [TS] tsset.

statname definition --------------------------------------------------------------------- mean mean sum sum sd standard deviation Var variance min minimum max maximum N count of nonmissing observations ---------------------------------------------------------------------


rollstat generates a new variable named _`statistic´`w´_varname (i.e. _sd3_gdp for the 3 period standard deviation of GDP) containing the rolling calculation of the specified statistic (under the aegis of tsset) with window size defined in option w. Results are placed at the end of the window. Although rollstat works with unbalanced panels (where the start and/or end points differ across units), rollstat does not allow gaps within the observations of a time series; that is, the value of an observation for a given period may be missing, but the observation itself must be defined. Gaps in time series may be dealt with via tsfill; see [TS] tsfill.

+---------+ ----+ Options +----------------------------------------------------------

w(#) defines the window size. If there are missing data, the actual number of observations used by rollstat may be less than w(#).

force forces results to be computed when some of a particular window's values are missing (and data has at least two observations). when force is set, rollstat starts accumulating from the first two observations of the sample until it reaches the desired window size. Then it continues with the defined window size to the end of sample.


. tsset year, y

. rollstat gdp cpi, s(sd) . rollstat gdp, s(mean) w(5) . rollstat gdp, s(sd) force


We acknowledge useful advice of Christopher F. Baum on previous version of this > routine.


Maximo Sangiacomo & Demian Panigo Email: Email: