.- help for ^movsumm^ (STB-44: sg85) .- Moving summaries ---------------- ^movsumm^ varname [^if^ exp] [^in^ range] [weight], ^g^en^(^newvar^)^ ^r^esult^(^#^)^ [ ^w^indow^(^#^) e^nd ^m^id { ^b^inomial | ^ow^eight^(^string^)^ } ^wrap^ ] Description ----------- ^movsumm^ produces a new variable containing moving summaries of varname for overlapping windows of specified length. varname will usually (but not necessarily) be a time series with regularly spaced values. Possible summaries are those produced by ^summarize^ and saved in ^_result( )^. It is the user's responsibility to place observations in the appropriate sort order first. ^fweight^s and ^aweight^s are allowed. Options ------- ^gen(^newvar^)^ specifies newvar as the name for the new variable. It is in fact a required option. ^result(^#^)^ specifies which ^_result( )^ from ^summarize^ is to be used. It is in fact a required option. See the table below. Note the typo in the Manual for 5.0: ^_result(10)^ contains the 50th percentile (median). 1 number of observations 2 sum of weight 3 mean 4 variance 5 minimum 6 maximum 7 5th percentile 8 10th percentile 9 25th percentile 10 50th percentile (median) 11 75th percentile 12 90th percentile 13 95th percentile 14 skewness 15 kurtosis 16 1st percentile 17 99th percentile 18 sum of variable ^window(^#^)^ specifies the length of the window, which should be an integer at least 2. The default is 3. 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 overridden by ^end^ and ^mid^. ^end^ forces results to be placed at the end of the window. ^mid^ forces results to be placed in the middle of the window, or in the case of windows of even length just after it: in the 2nd of 2, the 3rd of 4, the 4th of 6, and so on. ^binomial^ specifies that binomial weights are used. For a window of length k, these are k-1 k-1 C / 2 for i = 1 to k. i-1 ^oweight(^string^)^ specifies the user's own weights for values in the window. These should be numbers separated by spaces (not commas). The number of weights should equal that specified by ^window( )^. Thus ^ow(0.3 0.4 0.3)^ specifies weights of 0.3, 0.4, 0.3. ^binomial^ and ^oweight^ are mutually exclusive. Neither may be combined with other weights specified by ^[w =^ ...^]^. Each is treated as specifying analytic weights, i.e. they are rescaled to sum to the number of observations. ^wrap^ specifies that beginning and end values should be calculated by wrapping around. Thus with window length 3 and 12 observations the window for observation 1 would be 12, 1, 2 and the window for observation 12 would be 11, 12, 1. The justification for this option would be that the data are periodic, for example time of day or time of year. Remarks ------- The use of a ^summarize^ command for each window to do the hard work will mean that much of the computation time is spent producing results that are not used. Thus it will be often be faster to use some ad hoc one-line command. . ^gen bin3 = (1/4) * (x[_n-1] + 2 * x + x[_n+1])^ or even . ^smooth H x, gen(bin3)^ is much faster than . ^movsumm x, gen(bin3) bin w(3) r(3)^ The advantages of ^movsumm^ lie principally in being able to produce a wide range of moving summaries and in being able to wrap around. Users with non-periodic data may wish to fill in missing beginning and end values by their own favourite surgery following ^movsumm^. Examples -------- binomial smoothing with window length 7, wrapped around: . ^movsumm temp, w(7) r(3) gen(bin7temp) bin wrap^ moving median, 10 and 90% percentiles, window length 7: . ^movsumm temp, w(7) r(10) gen(medtemp)^ . ^movsumm temp, w(7) r(8) gen(p10temp)^ . ^movsumm temp, w(7) r(12) gen(p90temp)^ totals of monthly sales in previous quarter: . ^movsumm monsales, w(4) r(18) gen(qtrsales)^ but this will be slower than . ^gen qtrsales = monsales + monsales[_n-1] +^ ^monsales[_n-2] + monsales[_n-3]^ Author ------ Nicholas J. Cox University of Durham, U.K. n.j.cox@@durham.ac.uk Also see -------- STB: STB-44 sg85 Manual: [R] summarize, [U] 18.1.6 weight On-line: help for @summarize@, @smooth@, @egen@