开发者

R: Function that finds the range of 95% of all values?

Is there a function or an elegant way in the R language, to get the minimum range, 开发者_StackOverflowthat covers, say 95% of all values in a vector?

Any suggestions are very welcome :)


95% of the data will fall between the 2.5th percentile and 97.5th percentile. You can compute that value in R as follows:

x <- runif(100)
quantile(x,probs=c(.025,.975))

To get a sense of what's going on, here's a plot:

qts <- quantile(x,probs=c(.05,.95))
hist(x)
abline(v=qts[1],col="red")
abline(v=qts[2],col="red")

Note this is the exact/empirical 95% interval; there's no normality assumption.

R: Function that finds the range of 95% of all values?


It's not so hard to write such function:

find_cover_region <- function(x, alpha=0.95) {
    n <- length(x)
    x <- sort(x)
    k <- as.integer(round((1-alpha) * n))
    i <- which.min(x[seq.int(n-k, n)] - x[seq_len(k+1L)])
    c(x[i], x[n-k+i-1L])
}

Function will find shortest interval. If there are intervals with the same length first (from -Inf) will be picked up.

find_cover_region(1:100, 0.70)
# [1]  1 70
find_cover_region(rnorm(10000), 0.9973) # three sigma, approx (-3,3)
# [1] -2.859  3.160 # results may differ

You could also look on highest density regions (e.g. in package hdrcde, function hdr). It's more statistical way to find shortest intervals with given cover probability (some kernel density estimators are involved).


The emp.hpd function in the TeachingDemos package will find the values in a vector that enclose a given percentage of the data (95%) that also give the shortest range between the values. If the data is roughly symmetric then this will be close to the results of using quantile, but if the data are skewed then this will give a shorter range.


If the values are distributed approximately like the normal distribution, you can use the standard deviation. First, calculate the mean µ and standard deviation of the distribution. 95% of the values will be in the interval of (µ - 1.960 * stdev, µ + 1.960 * stdev).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜