qnorm() in R: Definition and 5 Examples

This tutorial explains how you can use qnorm() in R with examples.

qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)

  • p: the probability.
  • mean: The mean of the normal distribution sample data. The default value is 0.
  • sd: The standard deviation. The default value is 1.
  • lower.tail: By default, lower.tail = TRUE. It means that CDF is calculated from left (lower tail) to right (higher tail).
  • log.p: The default value is FALSE. If TRUE, p provided in the function is a log-value.

How is qnorm() calculated in R?

It return the quantile (i.e., value on the x-axis) for the probability of p. For instance, qnorm(0.5) will return the quantile 0 for probability 0.5.

Visually, 0.5 is area of the purple region under the bell-shaped curve, and 0 is the value of x-axis.

As we can see, qnorm() is just the inverse of pnorm().

qnorm in R
qnorm() in R

Example 1

qnorm(0.5) and qnorm(0.5, 0, 1) will return the same value (i.e., 0), since the default values for qnorm are mean = 0 and sd =1. 0 is the quantile corresponding with the probability of 0.5.

> qnorm(0.5)
[1] 0
> qnorm(0.5,0,1)
[1] 0

Example 2: lower.tail = TRUE vs. FALSE

By default, qnorm() uses lower.tail = TRUE. It means that it calculates the probability CDF from left to right.

The following examples assume lower.tail = TRUE. Thus, with and without lower.tail = TRUE will generate the same result.

> qnorm(0.95)
[1] 1.644854
> qnorm(0.95,lower.tail = TRUE)
[1] 1.644854
> qnorm(0.05)
[1] -1.644854
> qnorm(0.05,lower.tail = TRUE)
[1] -1.644854

The following examples set lower.tail = FALSE, which makes the CDF calculated from right to left. You will see they generate opposite results.

> qnorm(0.95,lower.tail = FALSE)
[1] -1.644854
> qnorm(0.05,lower.tail = FALSE)
[1] 1.644854

Example 3: log.p=FALSE

The following examples show the results of log.p=FALSE in qnorm(). Note that, log.p=FALSE is the default for qnorm() and thus they generate the same results.

> qnorm(0.05)
[1] -1.644854
> qnorm(0.05, log.p = FALSE)
[1] -1.644854

Example 4: log.p=TRUE

If log.p=TRUE, it means that p provided in the function is a log-value.

log(0.05)=-2.995732. Thus, if you set p=-2.995732 as the p and set log.p=TRUE, you should get the same quantile value as qnorm(0.05).

> qnorm(-2.995732, log.p = TRUE)
[1] -1.644853

Since log(1) =0 and 1 is the biggest possible p-value, the p provided in function should be a negative value if log.p=TRUE. If you set p=0 and log.p=TRUE, you should get a quantile of infinite.

> qnorm(0, log.p = TRUE)
[1] Inf

Example 5: error of “Warning message: NaNs produced”

If you set p >0 and log.p=TRUE, you will get Warning message: NaNs produced. Below is to reproduce such error. This error occur because p here is actually log(p) and should not be greater than 0.

> qnorm(0.05, log.p = TRUE)
[1] NaN
Warning message:
In qnorm(0.05, log.p = TRUE) : NaNs produced