Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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

Any suggestions are very welcome :)

like image 631
David Molnar Avatar asked Aug 07 '11 22:08

David Molnar


People also ask

How do you find 95% of data in R?

95% of the values will be in the interval of (µ - 1.960 * stdev, µ + 1.960 * stdev).

How do you find the range of values in R?

We can find the range by performing the difference between the minimum value in the vector and the maximum value in the given vector. We can find maximum value using max() function and minimum value by using min() function. Example: R.

How do you find the 2.5th percentile in R?

For this purpose, we can use quantile function in R. To find the 2.5th percentile, we would need to use the probability = 0.025 and for the 97.5th percentile we can use probability = 0.0975.

How do I get quantile in R?

Create Quantiles of a Data Set in R Programming – quantile() Function. quantile() function in R Language is used to create sample quantiles within a data set with probability[0, 1]. Such as first quantile is at 0.25[25%], second is at 0.50[50%], and third is at 0.75[75%].


1 Answers

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.

hist of 95% interval

like image 186
Ari B. Friedman Avatar answered Sep 23 '22 15:09

Ari B. Friedman