R, python or octave: empirical quantile (inverse cdf) with confidence intervals?
I'm looking for a built-in function that returns the sample quantile and an estimated confidence interval in something other than MATLAB (MATLAB's ecdf
does this).
I'm guessing R has this built-in and I just haven't found it yet.
If you have any standalone code to do this, you could also point to it h开发者_如何学Pythonere, though I hope to find something that is included as part of a larger open code base.
-Trying to get away from MATLAB.
The survfit
function can be used to get the survival function with confidence intervals. Since it is just 1-ecdf, there is a direct relationship between the quantiles. To use this you have to create a variable that says that each of your observations is complete (not censored):
library(survival)
x <- rexp(10)
ev <- rep(1, length(x))
sf <- survfit(Surv(x,ev)~1)
With output:
>summary(sf)
Call: survfit(formula = Surv(x, ev) ~ 1)
time n.risk n.event survival std.err lower 95% CI upper 95% CI
-1.4143 10 1 0.9 0.0949 0.7320 1.000
-1.1229 9 1 0.8 0.1265 0.5868 1.000
-0.9396 8 1 0.7 0.1449 0.4665 1.000
-0.4413 7 1 0.6 0.1549 0.3617 0.995
-0.2408 6 1 0.5 0.1581 0.2690 0.929
-0.1698 5 1 0.4 0.1549 0.1872 0.855
0.0613 4 1 0.3 0.1449 0.1164 0.773
0.1983 3 1 0.2 0.1265 0.0579 0.691
0.5199 2 1 0.1 0.0949 0.0156 0.642
0.8067 1 1 0.0 NaN NA NA
In fact, survfit
does calculate the median and its confidence interval, but not the other quantiles:
>sf
Call: survfit(formula = Surv(x, ev) ~ 1)
records n.max n.start events median 0.95LCL 0.95UCL
10.000 10.000 10.000 10.000 -0.205 -0.940 NA
The actual work for of the calculation of the confidence interval of the median is well hidden in the survival:::survmean
function, which you could probably use to generalize to other quantiles.
精彩评论