# runmad: Median Absolute Deviation of Moving Windows In caTools: Tools: Moving Window Statistics, GIF, Base64, ROC AUC, etc

## Description

Moving (aka running, rolling) Window MAD (Median Absolute Deviation) calculated over a vector

## Usage

 ```1 2 3``` ``` runmad(x, k, center = runmed(x,k), constant = 1.4826, endrule=c("mad", "NA", "trim", "keep", "constant", "func"), align = c("center", "left", "right")) ```

## Arguments

 `x` numeric vector of length n or matrix with n rows. If `x` is a matrix than each column will be processed separately. `k` width of moving window; must be an integer between one and n. In case of even k's one will have to provide different `center` function, since `runmed` does not take even k's. `endrule` character string indicating how the values at the beginning and the end, of the data, should be treated. Only first and last `k2` values at both ends are affected, where `k2` is the half-bandwidth `k2 = k %/% 2`. `"mad"` - applies the mad function to smaller and smaller sections of the array. Equivalent to: `for(i in 1:k2) out[i]=mad(x[1:(i+k2)])`. `"trim"` - trim the ends; output array length is equal to `length(x)-2*k2 (out = out[(k2+1):(n-k2)])`. This option mimics output of `apply` `(embed(x,k),1,FUN)` and other related functions. `"keep"` - fill the ends with numbers from `x` vector `(out[1:k2] = x[1:k2])`. This option makes more sense in case of smoothing functions, kept here for consistency. `"constant"` - fill the ends with first and last calculated value in output array `(out[1:k2] = out[k2+1])` `"NA"` - fill the ends with NA's `(out[1:k2] = NA)` `"func"` - same as `"mad"` option except that implemented in R for testing purposes. Avoid since it can be very slow for large windows. Similar to `endrule` in `runmed` function which has the following options: “`c("median", "keep", "constant")`” . `center` moving window center. Defaults to running median (`runmed` function). Similar to `center` in `mad` function. For best acuracy at the edges use `runquantile(x,k,0.5,type=2)`, which is slower than default `runmed(x,k,endrule="med")`. If `x` is a 2D array (and `endrule="mad"`) or if `endrule="func"` than array edges are filled by repeated calls to “`mad(x, center=median(x), na.rm=TRUE)`” function. Runmad's `center` parameter will be ignored for the beggining and the end of output `y`. Please use `center=runquantile(x,k,0.5,type=2)` for those cases. `constant` scale factor such that for Gaussian distribution X, `mad`(X) is the same as `sd`(X). Same as `constant` in `mad` function. `align` specifies whether result should be centered (default), left-aligned or right-aligned. If `endrule`="mad" then setting `align` to "left" or "right" will fall back on slower implementation equivalent to `endrule`="func".

## Details

Apart from the end values, the result of y = runmad(x, k) is the same as “`for(j=(1+k2):(n-k2)) y[j]=mad(x[(j-k2):(j+k2)], na.rm = TRUE)`”. It can handle non-finite numbers like NaN's and Inf's (like “`mad(x, na.rm = TRUE)`”).

The main incentive to write this set of functions was relative slowness of majority of moving window functions available in R and its packages. With the exception of `runmed`, a running window median function, all functions listed in "see also" section are slower than very inefficient “`apply(embed(x,k),1,FUN)`” approach.

Functions `runquantile` and `runmad` are using insertion sort to sort the moving window, but gain speed by remembering results of the previous sort. Since each time the window is moved, only one point changes, all but one points in the window are already sorted. Insertion sort can fix that in O(k) time.

## Value

Returns a numeric vector or matrix of the same size as `x`. Only in case of `endrule="trim"` the output vectors will be shorter and output matrices will have fewer rows.

## Author(s)

Jarek Tuszynski (SAIC) jaroslaw.w.tuszynski@saic.com

## References

About insertion sort used in `runmad` function see: R. Sedgewick (1988): Algorithms. Addison-Wesley (page 99)

• `runmad` - `mad`

• Other moving window functions from this package: `runmin`, `runmax`, `runquantile`, `runmean` and `runsd`

• generic running window functions: `apply```` (embed(x,k), 1, FUN)``` (fastest), `running` from gtools package (extremely slow for this purpose), `subsums` from magic library can perform running window operations on data with any dimensions.

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77``` ``` # show runmed function k=25; n=200; x = rnorm(n,sd=30) + abs(seq(n)-n/4) col = c("black", "red", "green") m=runmed(x, k) y=runmad(x, k, center=m) plot(x, col=col, main = "Moving Window Analysis Functions") lines(m , col=col) lines(m-y/2, col=col) lines(m+y/2, col=col) lab = c("data", "runmed", "runmed-runmad/2", "runmed+runmad/2") legend(0,0.9*n, lab, col=col, lty=1 ) # basic tests against apply/embed eps = .Machine\$double.eps ^ 0.5 k=25 # odd size window a = runmad(x,k, center=runmed(x,k), endrule="trim") b = apply(embed(x,k), 1, mad) stopifnot(all(abs(a-b)

### Example output ```   user  system elapsed
0.051   0.000   0.050
user  system elapsed
5.052   0.056   5.111
```

caTools documentation built on March 28, 2021, 9:07 a.m.