transform(deprecated)/moving - Help

stats[transform, moving]

replace each data item by a function of its neighborhood

 Calling Sequence stats[transform, moving[size, fctn, weighing]](data) transform[moving[size, fctn]](data)

Parameters

 data - statistical list fctn - (optional, default=mean) descriptive statistics function size - integer indicating the size of the moving window weighing - (optional, default= all ones) list of numbers indicating how to weigh each element of the neighborhood

Description

 • Important: The stats package has been deprecated. Use the superseding package Statistics instead.
 • The function moving of the subpackage stats[transform, ...] applies the requested descriptive statistics to a neighborhood of each of the items of the given data.
 • This is useful for smoothing the data, thus eliminating cyclic and irregular patterns and therefore enhancing the long term trends.
 • Missing data are included in the size of the neighborhood. Weights do not affect the size of the neighborhood.
 • The number of items in the answer is less than the number of items in data. Only complete neighborhoods are included, so the number of items is reduced by size-1. This is one disadvantage of using moving averages. Another disadvantage is that moving averages can produce periodicities that are not present in the original data. A third disadvantage is that moving averages are strongly influenced by extreme values. This last disadvantage can be reduced somewhat by using weighted moving averages, in which the central values are given a higher weight.
 • Note that moving averages with an even number of items produce values that are centered between the original items. For example, the first 2-month moving average of 12 monthly data items collected at the beginning of each month is centered in mid-January. The next 2-month moving average is centered in mid-February. If this is not suitable, then a further 2-month moving average is usually done. In our example, the first such second-order moving average, called 2-month centered moving averages,  will be centered at the beginning of February.
 • In order that the results be  valid, one must make sure that each successive items are comparable. For example, if the data represents total monthly precipitation (in a non-leap year), an adjustment probably needs to be made since February has 28 days whereas March has 31 days.

Examples

Important: The stats package has been deprecated. Use the superseding package Statistics instead.

 > $\mathrm{with}\left(\mathrm{stats}\right):$
 > $\mathrm{data}≔\left[2,6,1,5,3,7,2,1,2,2,4\right]$
 ${\mathrm{data}}{≔}\left[{2}{,}{6}{,}{1}{,}{5}{,}{3}{,}{7}{,}{2}{,}{1}{,}{2}{,}{2}{,}{4}\right]$ (1)

Replace each data point by the mean of itself and its two neighbors (so the size of the neighborhood is three).

 > $\mathrm{transform}[\mathrm{moving}[3]]\left(\mathrm{data}\right)$
 $\left[{3}{,}{4}{,}{3}{,}{5}{,}{4}{,}\frac{{10}}{{3}}{,}\frac{{5}}{{3}}{,}\frac{{5}}{{3}}{,}\frac{{8}}{{3}}\right]$ (2)

Give more weight to the central item.

 > $\mathrm{transform}[\mathrm{moving}[3,\mathrm{mean},\left[1,4,1\right]]]\left(\mathrm{data}\right)$
 $\left[\frac{{9}}{{2}}{,}\frac{{5}}{{2}}{,}{4}{,}{4}{,}\frac{{11}}{{2}}{,}\frac{{8}}{{3}}{,}\frac{{4}}{{3}}{,}\frac{{11}}{{6}}{,}\frac{{7}}{{3}}\right]$ (3)

The first point is calculated by

 > $\mathrm{describe}[\mathrm{mean}]\left(\left[\mathrm{Weight}\left(2,1\right),\mathrm{Weight}\left(6,4\right),\mathrm{Weight}\left(1,1\right)\right]\right)$
 $\frac{{9}}{{2}}$ (4)

if you replace by the median instead of the mean, you have:

 > $\mathrm{transform}[\mathrm{moving}[3,\mathrm{median}]]\left(\mathrm{data}\right)$
 $\left[{2}{,}{5}{,}{3}{,}{5}{,}{3}{,}{2}{,}{2}{,}{2}{,}{2}\right]$ (5)

Using 4-moving average results in

 > $\mathrm{Four_m}≔\mathrm{transform}[\mathrm{moving}[4]]\left(\mathrm{data}\right)$
 ${\mathrm{Four_m}}{≔}\left[\frac{{7}}{{2}}{,}\frac{{15}}{{4}}{,}{4}{,}\frac{{17}}{{4}}{,}\frac{{13}}{{4}}{,}{3}{,}\frac{{7}}{{4}}{,}\frac{{9}}{{4}}\right]$ (6)

and the 4-centered average is given by

 > $\mathrm{Four_c}≔\mathrm{transform}[\mathrm{moving}[2]]\left(\mathrm{Four_m}\right)$
 ${\mathrm{Four_c}}{≔}\left[\frac{{29}}{{8}}{,}\frac{{31}}{{8}}{,}\frac{{33}}{{8}}{,}\frac{{15}}{{4}}{,}\frac{{25}}{{8}}{,}\frac{{19}}{{8}}{,}{2}\right]$ (7)