1
$\begingroup$

What is the difference between the R-functions quantile and qnorm?

I have normal distributed data and want to calculate rejection areas for my given hypothesis test. In order to do this I found quantile to calculate the .025 and .975 quantiles ($\alpha = 0.05$), as well as qnorm. The difference is that qnorm works with the calculated mean and sd of my data. But why do I sometimes have different values?

I use those function calls for the upper (right-handed) rejection area and alpha <- 0.05:

qnorm(1-alpha/2, mean = mean(data), sd = sqrt(var(data))

quantile(data, .975)

$\endgroup$

1 Answer 1

4
$\begingroup$

quantile does not assume any distribution, in the quantile() function from R, for the case of 97.5 quantile, it finds the two values between which the quantile value should lie, and does an interpolation, so for example in the below example of 100 values, it interpolates between the 97th and 98th sorted value:

set.seed(11)
x = runif(100)
sort(x)[97:98]
[1] 0.8806992 0.9071830
quantile(x,0.975)
    97.5% 
0.8946032

qnorm is quite different. It returns you the value at 97.5 quantile of a normal distribution with mean and variance estimated from your data. So whether this coincides with the above value, depends on how well the normal distribution with estimated mean and variance, can describe your data.

Below I give a simple example, where you have small sample size and causing the two estimates can differ. I simulate draw from samples of 20,200 and 2000 from a normal distribution of mean

norm_data_quantiles = function(i,n){
set.seed(i+n)
alpha = 0.05
data = rnorm(n,2,1)
norm_quantile = qnorm(1-alpha/2, mean = mean(data), sd = sd(data))

h = hist(data,freq=FALSE,main=paste("n=",n,"rep=",i))
lines(h$mids,dnorm(h$mids,mean(data),sd(data)),col="blue")
abline(v=norm_quantile,col="blue",lty=8)
abline(v=quantile(data,0.975),col="red",lty=8)
}

par(mfrow=c(3,3))
D = expand.grid(sim=1:3,n=c(20,200,2000))
for(i in 1:nrow(D)){
norm_data_quantiles(D$sim[i],D$n[i])}

enter image description here

The blue line is meant to show the expected normal probability while the histogram reflects the distribution in the actual data. The red dotted line is the 0.975 quantile while blue dotted is that estimated from the normal.

So you can see when you increase sample size, the two values differ less.

There are other reasons when they can differ, for example, your data is heavy tailed, big outliers, basically anything that can throw off the estimation of the mean and standard deviation.

$\endgroup$
7
  • $\begingroup$ So is it correct to say, if you don't know the exact values of your normal distribution (and they would be approximated), it is better to interpolate rejection areas using quantile ? $\endgroup$
    – nomeal
    Commented Apr 12, 2020 at 17:36
  • $\begingroup$ Before i tell you something wrong, what do you mean by rejection areas? What are you using qnorm or quantile for? Because you question only ask difference between qnorm and quantile $\endgroup$
    – StupidWolf
    Commented Apr 12, 2020 at 17:38
  • $\begingroup$ I have a hypothesis test and want to specify the critical area for a given significance level $\alpha$. As my data is distributed normally and my test is two-sided, my aim is to calculate the $\alpha /2$ and $1- \alpha /2$-quantiles of my distribution. But this distribution is simulated, therefore I don't have exact values of mean and sd. $\endgroup$
    – nomeal
    Commented Apr 12, 2020 at 18:32
  • $\begingroup$ Ok if I guess correctly, you are doing something like a bootstrap or permutation, you simulate what your data would look like under the null hypothesis, then you want to find the 0.975 quantile to reject. Yes in this case you can use quantile. $\endgroup$
    – StupidWolf
    Commented Apr 12, 2020 at 18:35
  • $\begingroup$ Or you can simply calculate the p-value as (sum(t_sim > t_obs)+1)/(n_sim + 1). you can check this paper, arxiv.org/pdf/1603.05766.pdf, hope I guessed correctly $\endgroup$
    – StupidWolf
    Commented Apr 12, 2020 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.