Calculate the confidence interval for the median.

MedianCI(
  x,
  conf.level = 0.95,
  sides = c("two.sided", "left", "right"),
  method = c("exact", "boot"),
  na.rm = FALSE,
  ...
)

Arguments

x

a (non-empty) numeric vector of data values.

conf.level

confidence level of the interval

sides

a character string specifying the side of the confidence interval, must be one of "two.sided" (default), "left" or "right". You can specify just the initial letter. "left" would be analogue to a hypothesis of "greater" in a t.test.

method

defining the type of interval that should be calculated (one out of "exact", "boot"). Default is "exact". See Details.

na.rm

logical. Should missing values be removed? Defaults to FALSE.

...

the dots are passed on to boot.ci. In particular, the type of bootstrap confidence interval can be defined via this. The defaults are R=999 and type="perc".

Value

a numeric vector with 3 elements:

median

median

lwr.ci

lower bound of the confidence interval

upr.ci

upper bound of the confidence interval

Details

The "exact" method is the way SAS is said to calculate the confidence interval. This is also implemented in SignTest. The boot confidence interval type is calculated by means of boot.ci with default type "perc".
Use sapply, resp.apply, to get the confidence intervals from a data.frame or from a matrix.

Author

Andri Signorell andri@signorell.net

Examples


MedianCI(d.pizza$price, na.rm=TRUE)
#> median lwr.ci upr.ci 
#> 46.764 44.970 47.664 
#> attr(,"conf.level")
#> [1] 0.9506831
MedianCI(d.pizza$price, conf.level=0.99, na.rm=TRUE)
#> median lwr.ci upr.ci 
#> 46.764 44.970 48.070 
#> attr(,"conf.level")
#> [1] 0.990742

t(round(sapply(d.pizza[,c("delivery_min","temperature","price")], MedianCI, na.rm=TRUE), 3))
#>              median lwr.ci upr.ci
#> delivery_min 24.400  23.80 25.200
#> temperature  50.000  49.50 50.700
#> price        46.764  44.97 47.664

MedianCI(d.pizza$price, na.rm=TRUE, method="exact")
#> median lwr.ci upr.ci 
#> 46.764 44.970 47.664 
#> attr(,"conf.level")
#> [1] 0.9506831
MedianCI(d.pizza$price, na.rm=TRUE, method="boot")
#> median lwr.ci upr.ci 
#> 46.764 44.970 47.664 


x <- runif(100)

set.seed(448)
MedianCI(x, method="boot")
#>    median    lwr.ci    upr.ci 
#> 0.4699865 0.4125228 0.5707578 

# ... the same as
set.seed(448)
MedianCI(x, method="boot", type="bca")
#>    median    lwr.ci    upr.ci 
#> 0.4699865 0.4122429 0.5680532 

MedianCI(x, method="boot", type="basic")
#>    median    lwr.ci    upr.ci 
#> 0.4699865 0.3699540 0.5271703 
MedianCI(x, method="boot", type="perc")
#>    median    lwr.ci    upr.ci 
#> 0.4699865 0.4205251 0.5644305 
MedianCI(x, method="boot", type="norm", R=499)
#>    median    lwr.ci    upr.ci 
#> 0.4699865 0.3844799 0.5338981 
# not supported:
MedianCI(x, method="boot", type="stud")
#> Warning: bootstrap type 'stud' is not supported
#>    median    lwr.ci    upr.ci 
#> 0.4699865        NA        NA 

MedianCI(x, method="boot", sides="right")
#>    median    lwr.ci    upr.ci 
#> 0.4699865      -Inf 0.5403481