| Type: | Package |
| Title: | Hypothesis Tests and Confidence Intervals for Median Absolute Deviations |
| Version: | 0.1.1 |
| Maintainer: | Luke A. Prendergast <luke.prendergast@latrobe.edu.au> |
| Description: | Conducts one- and two-sample hypothesis tests for median absolute deviations (mads) for robust inference of dispersion. Comparisons between two samples uses the ratio of mads. Confidence intervals are also computed. |
| Imports: | gld, stats |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Suggests: | testthat (≥ 3.0.0) |
| Config/testthat/edition: | 3 |
| License: | MIT + file LICENSE |
| NeedsCompilation: | no |
| Packaged: | 2026-01-29 21:56:34 UTC; LPrendergast |
| Author: | Luke A. Prendergast
|
| Repository: | CRAN |
| Date/Publication: | 2026-02-03 12:40:26 UTC |
Test for a single the median absolute deviation (MAD)
Description
Calculates a confidence interval and test the null hypothesis for the median absolute deviation (MAD) for a single sample.
Usage
madci(
x,
constant = 1.4826,
alternative = c("two.sided", "less", "greater"),
conf.level = 0.95,
true.mad = 1,
use.gld = FALSE,
gld.est = "TM",
...
)
Arguments
x |
a numeric vector of data values. |
constant |
a scale factor. Default choice ensures population mad is equal to sd for normal distributions. |
alternative |
a character string specifying the alternative hypothesis to be used. Needs to be one of "two.sided" (default), "greater" or "less". |
conf.level |
confidence level for the confidence interval. The default is 0.95 (for a 95% confidence interval). |
true.mad |
a number which is the nominated value for the population mad under the null hypothesis. Default is 1. |
use.gld |
a logical indicating whether the Generalised Lambda Distribution (GLD) should be used to estimate the density of the data. Default is FALSE in which case the function |
gld.est |
a character string indicating which gld estimator to use if |
... |
additional arguments, if desired, to be passed to function density for density estimation (see help file for |
Details
This function calculates an approximate confidence interval and conducts a hypothesis test for a single mad. The approximate variance used in the construction of the confidence interval and the test statistic is calculated using the varmad() function (see Arachchige and Prendergast (2024) and the help file for varmad() for further details).
Note that the interval and test statistic are approximate and may be unreliable for small sample sizes. Arachchige and Prendergast (2024) showed that reliable results
can be achieved for sample sizes of 50 or more when simulating data from various distributions. Additionally, the confidence interval is a Wald-type interval (estimate +/- crit*SE)
and so it may be possible that the domain of the interval contains negative values. Since negative values are not possible for the mad, thought could be given to truncation (setting the lower bound to zero), although
this may undermine interval coverage.
Value
Hypothesis test results comparing two mads from independent samples and associated confidence interval for the ratio of mads (a list with class "htest"). This list includes values
statisticthe value of the Z-statistic.
p.valuethe p-value for the test.
conf.inta confidence interval for the MAD that is appropriate for the alternative hypothesis.
estimatethe estimated MAD.
null.valuethe specified hypothesized value of the MAD under the null hypothesis.
alternativea character string describing the alternative hypothesis.
data.namea character string giving the names of the data.
References
Arachchige, C.N.P.G., & Prendergast, L.A. (2026) Confidence intervals for median absolute deviations. Communications in Statistics-Simulation and Computation, 55(1), 13-22.
Examples
# Create some data
set.seed(1234)
x <- rlnorm(100)
# Calculate the mad, 95% confidence interval and test the hypothesis
mad.est <- madci(x)
mad.est
Hypothesis Test to Compare Two Median Absolute Deviations (MADs)
Description
Performs hypothesis tests and calculates confidence intervals to compare MADs of two samples.
Usage
madtest(
x,
y,
ratio = 1,
conf.level = 0.95,
use.squared = FALSE,
use.gld = FALSE,
gld.est = "TM",
...
)
Arguments
x, y |
numeric vectors of data values. |
ratio |
a number representing the value of the ratio of the two MADs (or squared MADs) under the null hypothesis. |
conf.level |
confidence level for the confidence interval. The default is 0.95 (for a 95% confidence interval). |
use.squared |
a logical indicating whether the squared MADs should be used. Default is |
use.gld |
a logical indicating whether the Generalised Lambda Distribution (GLD) should be used to estimate the density of the data. Default is |
gld.est |
a character string indicating which GLD estimator to use if |
... |
additional arguments, if desired, to be passed to function |
Details
This function carries out hypothesis tests and calculates confidence intervals for comparing the MADs between two populations. Two independent samples are required, and the intervals are constructed for the ratio of mads (or squared mads as an analog to ratio of variances) as described in Arachchige and Prendergast (2024). The interval is first computed for the log of the ratio before being back-transformed to the ratio scale. The hypothesis test is carried out in a similar way, based on the log ratio and using asymptotic normality of the test statistic. The default for the ratio under the null hypothesis is one which tests against equality of mads.
Value
Hypothesis test results comparing two mads from independent samples and associated confidence interval for the ratio of mads (a list with class "htest"). This list includes values
statisticthe value of the Z-statistic.
p.valuethe p-value for the test.
conf.inta confidence interval for the ratio of MADs (or squared MADs).
estimatethe estimated ratio of MADs (or squared MADs).
null.valuethe specified hypothesized value of the ratio under the null hypothesis.
alternativea character string describing the alternative hypothesis. Note, for this test only two-sided alternatives are allowed.
data.namea character string giving the names of the data.
References
Arachchige, C.N.P.G., & Prendergast, L.A. (2026) Confidence intervals for median absolute deviations. Communications in Statistics-Simulation and Computation, 55(1), 13-22.
Examples
# Create some data
set.seed(1234)
x <- rlnorm(100)
y <- rlnorm(50, meanlog = 0.5)
# Calculate the mad, 95% confidence interval and test the hypothesis
mads.est <- madtest(x, y)
mads.est
Population Median Absolute Deviation (MAD)
Description
This function calculates the population Median Absolute Deviation (MAD) for a user-specified distribution.
Usage
pop.mad(qf, pf, upper, constant = 1.4826, ...)
Arguments
qf |
the quantile function for the distribution. |
pf |
the distribution function for the distribution. |
upper |
an optional number specifying an upper limit for the MAD. This can be missing although the user may want to specify an upper limit if there are computation issues. |
constant |
a scale factor. Default choice ensures population MAD is equal to SD for the normal distribution. |
... |
additional parameter values for the distribution, otherwise default choices are used for the specific quantile and distribution functions. |
Details
This function calculates the Median Absolute Deviation (MAD) for a distribution specified by the user. The user is required to input the quantile and
distribution functions, as well as any parameters required for the nominated distribution. If the function is unable to find
the MAD using function root, the user may specify another upper limit for the MAD to help with computation. If upper is missing, upper
is chosen to be the maximum of the third quartile minus the median and the median minus the first quantile (the MAD cannot be greater than this upper limit). For more information in the MAD see, e.g., Hampel et al. (1986).
Value
A value representing the MAD for the distribution.
References
Hampel, F. R., Ronchetti, E. M., Rousseeuw, P. J. & Stahel, W. A. (1986). Robust statistics: The approach based on influence functions. New York, NY: John Wiley & Sons.
Examples
# Population MAD for the normal distribution
pop.mad(qnorm, pnorm, sd = 1.5)
# Note that due to the scaling constant used, the mad is equal to the SD for the normal distribution
Approximate Variance of the Median Absolute Deviation (MAD) Estimator.
Description
This function calculates the approximate (large-sample) variance of the MAD estimator.
Usage
varmad(x, constant = 1.4826, use.gld = FALSE, gld.est = "TM", ...)
Arguments
x |
a numeric vector of data values. |
constant |
a scale factor. Default choice ensures population mad is equal to the SD for normal distributions. |
use.gld |
a logical indicating whether to use the flexible Generalised Lambda Distribution (GLD) to estimate the density and distribution function. |
gld.est |
a character string indicating which estimator of the GLD parameters is to be used if |
... |
additional arguments, if desired, to be passed to function density for density estimation (see help file for |
Details
This function calculates an approximate variance for the traditional Median of Absolute Deviations (MAD) estimator.
The variance requires estimation of the probability density function and the distribution function which, by default, is carried out
using the density() and ecdf() functions respectively. An alternative is to use the flexible four-parameter Generalised Lambda Distribution (GLD)
to estimate these functions. This is done by using use.gld = TRUE and the default estimator is gld.est = TM for
Titterington's Method (Titterington, 1985). Other estimators are available and further details can be found in the gld::fit.fkml documentation from the gld package
(King et al., 2022). For further details on the approximate variance see Arachchige and Prendergast (2024).
Value
A number which is the estimated approximate variance of the mad estimator
References
Arachchige, C.N.P.G., & Prendergast, L.A. (2026). Confidence intervals for median absolute deviations. Communications in Statistics-Simulation and Computation, 55(1), 13-22.
King R, Dean B, Klinke S, & van Staden, P (2022). gld: Estimation and Use of the Generalised (Tukey) Lambda Distribution. R package version 2.6.6, https://CRAN.R-project.org/package=gld.
Titterington, D. M. (1985), Comment on ‘Estimating Parameters in Continuous Univariate Distributions', Journal of the Royal Statistical Society, Series B, 47, 115–116.
Examples
# Create some data
set.seed(1234)
x <- rlnorm(100)
# Calculate the mad and its variance
mad.est <- mad(x)
mad.est
v.est <- varmad(x)
v.est
# Calculate an approximate 95% confidence interval for the true mad
mad.est + c(-1, 1) * qnorm(0.975) * sqrt(v.est)