Pseudomedian

pMedian(
  x,
  na.rm = FALSE,
  conf.int = 0,
  B = 1000,
  type = c("percentile", "bca")
)

Arguments

x

a numeric vector

na.rm

set to TRUE to exclude NAs before computing the pseudomedian

conf.int

confidence level, defaulting to 0 so that no confidence limits are computed. Set to a number between 0 and 1 to compute bootstrap confidence limits

B

number of bootstrap samples if conf.int > 0

type

type of bootstrap interval, defaulting to 'percentile' for n >= 150 or 'bca' for n < 150

Value

a scalar numeric value if conf.int = 0, or a 3-vector otherwise, with named elements estimate, lower, upper and attribute type. If the number of non-missing values is less than 5, NA is returned for both lower and upper limits.

Details

Uses fast Fortran code to compute the pseudomedian of a numeric vector. The pseudomedian is the median of all possible midpoints of two observations. The pseudomedian is also called the Hodges-Lehmann one-sample estimator. The Fortran code is was originally from JF Monahan, and was converted to C++ in the DescTools package. It has been converted to Fortran 2018 here. Bootstrap confidence intervals are optionally computed.

If n > 250,000 a random sample of 250,000 values of x is used to limit execution time. For n > 1,000 only the percentile bootstrap confidence interval is computed.

Bootstrapping uses the Fortran subroutine directly, for efficiency.

Examples

x <- c(1:4, 10000)
pMedian(x)
#> [1] 3
pMedian(x, conf.int=0.95)
#> estimate    lower    upper 
#>      3.0      1.5   5001.0 
#> attr(,"type")
#> [1] "bca"
# Compare with brute force calculation and with wilcox.test
w <- outer(x, x, '+')
median(w[lower.tri(w, diag=TRUE)]) / 2
#> [1] 3
wilcox.test(x, conf.int=TRUE)
#> 
#> 	Wilcoxon signed rank exact test
#> 
#> data:  x
#> V = 15, p-value = 0.06
#> alternative hypothesis: true location is not equal to 0
#> 95 percent confidence interval:
#>      1 10000
#> sample estimates:
#> (pseudo)median 
#>              3 
#>