------------------------------------------------------------------------------- help formvsumm-------------------------------------------------------------------------------

Generate moving-window descriptive statistics in time series or panel

mvsummtsvar[ifexp] [inrange] [weight],generate(newvar)stat(statistic)[window(#)endforce]

mvsummis for use with time-series data. You musttssetyour data before usingmvsumm; see help tsset.

varnamemay contain time-series operators; see help varlist.

Description

mvsummcomputes a moving-window descriptive statistic fortsvarwhich must be a time series variable under the aegis oftsset. If a panel calendar is in effect, the statistic is calculated for each time series within the panel. The moving-window statistic is placed in a new variable, specified with thegenerate()option. The statistics available include minimum, maximum, other key percentiles, mean and standard deviation: one of these and/or other statistics returned bysummarize, or easily computable from what it returns, may be specified. aweights or fweights may be specified. Althoughmvsummworks with unbalanced panels (where the start and/or end points differ across units),mvsummdoes 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 thetsfillcommand.

Options

stat(statistic)specifies the statistic desired, from the following list. This is a required option.one of statistic ------ --------- n N count number of non-missing observations sum sum sum_w sum of weight mean mean sd SD standard deviation Var var variance se SE semean standard error of the mean skew skewness skewness kurt kurtosis kurtosis min minimum max maximum p1 1st percentile p5 5th percentile p10 10th percentile p25 25th percentile p50 med median 50th percentile (median) p75 75th percentile p90 90th percentile p95 95th percentile p99 99th percentile iqr IQR interquartile range (p75 - p25) range range (max - min)

generate(newvar)specifies the name of a new variable in which the results are to be placed. This is a required option.

window(#)specifies the width of the window for computation of the statistics, which should be an integer at least 2. By default, results for odd-length windows are placed in the middle of the window and results for even-length windows are placed at the end of the window. The defaults can be over-ridden by theendoption. The default is 3.

endforces results to be placed at the end of the window in the case where the window width is an odd number.

forceforces results to be computed when some of a particular window's values are missing.

RemarksOccasionally people want to use

ifand/orinwhen calculating moving summaries, but that raises a complication not usually encountered. What would you expect from a moving summary calculated with either kind of restriction? Let us identify two possibilities:Weak interpretation: I don't want to see any results for the excluded observations.

Strong interpretation: I don't even want you to use the values for the excluded observations.

Here is a concrete example. Suppose as a consequence of some restriction, observations 1-42 are included, but not observations 43 on. But the moving summary for 42 will depend, among other things, on the value for observation 43 if the summary extends backwards and forwards and is of length at least 3, and it will similarly depend on some of the observations 44 onwards in some circumstances.

Our guess is that most people would go for the weak interpretation, which is employed in

mvsumm. If not, you should ignore what you don't want or even set unwanted values to missing afterwards by usingreplace.

Examples. webuse grunfeld . mvsumm invest, stat(mean) win(3) gen(inv3yavg) end

. mvsumm invest, stat(sd) win(5) gen(inv5ysd) end

. mvsumm D.mvalue, stat(median) win(5) gen(meddmval) end

AuthorsChristopher F. Baum, Boston College, USA baum@bc.edu

Nicholas J. Cox, Durham University, U.K. n.j.cox@durham.ac.uk

AcknowledgementsThis routine is based on Cox's

movsummand the authors'statsmat. Its development was inspired by a July 2002 discussion on Statalist. Nick Winter and Vince Wiggins provided helpful comments. Ernest Berkhout helpfully identified some problems with the routine.

Also seeOn-line: summarize, tsset, tsfill