# pmsd: Median scaled difference probabilities and quantiles In metRology: Support for Metrological Applications

## Description

Cumulative lower tail probability and quantile for median of scaled differences.

## Usage

 ```1 2 3 4 5 6 7``` ``` dmsd(q, n, method=c('fast', 'exact', 'even', 'asymp'), max.odd=199) pmsd(q, n, lower.tail = TRUE, method=c('fast', 'exact', 'even', 'asymp'), max.odd=199) qmsd(p, n, lower.tail = TRUE, method=c('fast', 'exact', 'even', 'asymp'), max.odd=199) ```

## Arguments

 `q` Vector of quantiles. `p` Vector of probabilities. `n` Number of observations from which msd was calculated. Unused (and can be missing) for `method="asymp"` `lower.tail` logical; if TRUE (the default), probabilities are P[X <= x]; otherwise, P[X > x]. `method` Calculation method. See details. `max.odd` Highest odd `n` for which exact values are calculated.

## Details

`pmsd`, `dmsd` and `qmsd` return probabilities, densities and quantiles, respectively, for the median scaled difference applied to a single observation in a standard normal distribution, where otehr values are also IID normal.

`n` is the number of observations in the data set of interest and not the degrees of freedom or number of differences (msd for a value x[i] in a set of `n` observations involves `n-1` scaled differences).

`n`, `p` and `q` are recycled to the length of the longest, as necessary.

`method` determines the method of calculation. For `method="fast"`, probabilities are calculated using monotonic spline interpolation on precalculated probabilities. `qmsd` with `method="fast"` is obtained by root-finding on the corresponding spline function using `uniroot`, and densities are estimated from the first derivative of the interpolating spline. This provides fast calculation, and values for most practical probabilities are within 10^-6 of exact calculations. For high probabilites and for low quantiles (below 0.48) at high `n`, fast quantile accuracy is poorer due to the very low function gradients in this regions, but is still guaranteed monotonic with `p`.

For `method="exact"`, probabilities and densities are calculated using quadrature integration for an order statistic. For odd `n`, this requires a double integral. Values for odd `n` accordingly take about an order of magnitude longer to obtain than for even `n`. This can be slow (seconds for a vector of several hundred values of `q` on an Intel x86 machine running at 1-2GHz). `qmsd` with `method="exact"` is obtained by root-finding from `pmsd(..., method="excat")` using `uniroot`, and is over an order of magnitude slower than `pmsd` pmsd.

For `method="exact"`, asymptotic (large n) probabilities, densities and quantiles are returned. `n` is unused and can be missing.

For `method="exact"`, odd `n` above `max.odd` are replaced with the next lower even value. This provides a fair approximation for `n` above 30 (though the fast method is better) and a good approximation above the default of 199. Values of `max.odd` above 199 are not recommended as integration can become unstable at high odd `n`; a warning is issued if `max.odd > 199`.

For `method="even"`, an exact calculation is performed with any odd `n` replaced with the next lower even value. This is equivalent to setting `method="exact"` and `max.odd=0`. This is provided for interest only; the `method="fast"` method provides a substantially better approximation for odd `n` than `method="even"` and is faster.

Note that these functions are appropriate for the distribution of single values. If seeking an outlier test in a data set of size N, either adjust `p` for N comparisons before applying `qmsd` to find a critical value, or adjust the returned p-values using, for example, Holm adjustment.

## Value

A vector of length `length(p)` or `length(q)` (or, if longer, `length(n)`) of cumulative probabilities, densities or quantiles respectively.

## Author(s)

S Ellison s.ellison@lgc.co.uk

`msd` for calculation of MSD values, and `bootMSD` for a parametric bootstrap (MCS) method of obtaining p-values and quantiles for the more general non-IID case.
 ```1 2 3``` ``` data(Pb) msd(Pb\$value) # Uses mad(Pb\$value) as scale estimate msd(Pb\$value, Pb\$u) # Scales differences using standard uncertainties ```