A Guide to dnorm, pnorm, qnorm, and rnorm in R


The normal distribution is the most commonly used distribution in statistics. This tutorial explains how to work with the normal distribution in R using the functions dnorm, pnorm, rnorm, and qnorm.

dnorm

The function dnorm returns the value of the probability density function (pdf) of the normal distribution given a certain random variable x, a population mean μ and population standard deviation σ. The syntax for using dnorm is as follows:

dnorm(x, mean, sd) 

The following code illustrates a few examples of dnorm in action:

#find the value of the standard normal distribution pdf at x=0
dnorm(x=0, mean=0, sd=1)
# [1] 0.3989423

#by default, R uses mean=0 and sd=1
dnorm(x=0)
# [1] 0.3989423

#find the value of the normal distribution pdf at x=10 with mean=20 and sd=5
dnorm(x=10, mean=20, sd=5)
# [1] 0.01079819

Typically when you’re trying to solve questions about probability using the normal distribution, you’ll often use pnorm instead of dnorm. One useful application of dnorm, however, is in creating a normal distribution plot in R. The following code illustrates how to do so:

#Create a sequence of 100 equally spaced numbers between -4 and 4
x <- seq(-4, 4, length=100)

#create a vector of values that shows the height of the probability distribution
#for each value in x
y <- dnorm(x)

#plot x and y as a scatterplot with connected lines (type = "l") and add
#an x-axis with custom labels
plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "")
axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

This generates the following plot:

pnorm

The function pnorm returns the value of the cumulative density function (cdf) of the normal distribution given a certain random variable q, a population mean μ and population standard deviation σ. The syntax for using pnorm is as follows:

pnorm(q, mean, sd) 

Put simply, pnorm returns the area to the left of a given value x in the normal distribution. If you’re interested in the area to the right of a given value q, you can simply add the argument lower.tail = FALSE

pnorm(q, mean, sd, lower.tail = FALSE) 

The following examples illustrates how to solve some probability questions using pnorm.

Example 1: Suppose the height of males at a certain school is normally distributed with a mean of a standard deviation of 

#find percentage of males that are taller than 74 inches in a population with
#mean = 70 and sd = 2
pnorm(74, mean=70, sd=2, lower.tail=FALSE)

# [1] 0.02275013

At this school, 2.275% of males are taller than 74 inches.

Example 2:  Suppose the weight of a certain species of otters is normally distributed with a mean of a standard deviation of 

#find percentage of otters that weight less than 22 lbs in a population with
#mean = 30 and sd = 5
pnorm(22, mean=30, sd=5)

# [1] 0.05479929

Approximately 5.4799% of this species of otters weigh less than 22 lbs.

Example 3:  Suppose the height of plants in a certain region is normally distributed with a mean of a standard deviation of 

#find percentage of plants that are less than 14 inches tall, then subtract the
#percentage of plants that are less than 10 inches tall, based on a population
#with mean = 13 and sd = 2
pnorm(14, mean=13, sd=2) - pnorm(10, mean=13, sd=2)

# [1] 0.6246553

Approximately 62.4655% of plants in this region are between 10 and 14 inches tall.

qnorm

The function qnorm returns the value of the inverse cumulative density function (cdf) of the normal distribution given a certain random variable p, a population mean μ and population standard deviation σ. The syntax for using qnorm is as follows:

qnorm(p, mean, sd) 

Put simply, you can use qnorm to find out what the Z-score is of the pth quantile of the normal distribution.

The following code illustrates a few examples of qnorm in action:

#find the Z-score of the 99th quantile of the standard normal distribution 
qnorm(.99, mean=0, sd=1)
# [1] 2.326348

#by default, R uses mean=0 and sd=1
qnorm(.99)
# [1] 2.326348

#find the Z-score of the 95th quantile of the standard normal distribution 
qnorm(.95)
# [1] 1.644854

#find the Z-score of the 10th quantile of the standard normal distribution 
qnorm(.10)
# [1] -1.281552

rnorm

The function rnorm generates a vector of normally distributed random variables given a vector length n, a population mean μ and population standard deviation σ. The syntax for using rnorm is as follows:

rnorm(n, mean, sd) 

The following code illustrates a few examples of rnorm in action:

#generate a vector of 5 normally distributed random variables with mean=10 and sd=2
five <- rnorm(5, mean = 10, sd = 2)
five
# [1] 10.658117 8.613495 10.561760 11.123492 10.802768

#generate a vector of 1000 normally distributed random variables with mean=50 and sd=5
narrowDistribution <- rnorm(1000, mean = 50, sd = 15)

#generate a vector of 1000 normally distributed random variables with mean=50 and sd=25
wideDistribution <- rnorm(1000, mean = 50, sd = 25)

#generate two histograms to view these two distributions side by side, specify
#50 bars in histogram and x-axis limits of -50 to 150
par(mfrow=c(1, 2)) #one row, two columns
hist(narrowDistribution, breaks=50, xlim=c(-50, 150)) 
hist(wideDistribution, breaks=50, xlim=c(-50, 150))

This generates the following histograms:

Notice how the wide distribution is much more spread out compared to the narrow distribution. This is because we specified the standard deviation in the wide distribution to be 25 compared to just 15 in the narrow distribution. Also notice that both histograms are centered around the mean of 50.

3 Replies to “A Guide to dnorm, pnorm, qnorm, and rnorm in R”

  1. We use dnorm,pnorm,qnorm,rnorm when dealing with Z
    We use dt,pt,qt,rt when dealing with t

    What are the corresponding function when we are dealing with proportion p? I Know that when hypothesis problems use p, we Z.

    Can we use the functions above , when dealing with Z?
    Thank you

    1. Hi Dimitrios…When dealing with proportions in hypothesis testing, you typically use the standard normal distribution (Z-distribution). The functions `dnorm`, `pnorm`, `qnorm`, and `rnorm` in R (or their equivalents in other statistical programming languages) are appropriate when working with proportions because they relate to the normal distribution. Here’s how you can use them:

      1. **`dnorm(x, mean = 0, sd = 1)`**: This function gives the density (height of the probability density function) at a particular point `x` for the normal distribution with specified mean and standard deviation. For proportions, this can be used to find the likelihood of observing a specific proportion if the distribution of the sample proportion is approximately normal.

      2. **`pnorm(q, mean = 0, sd = 1)`**: This function gives the cumulative probability up to a point `q` for the normal distribution with specified mean and standard deviation. For proportions, you use this to find the probability that the sample proportion is less than or equal to a certain value.

      3. **`qnorm(p, mean = 0, sd = 1)`**: This function gives the quantile (inverse of the cumulative distribution function) at a given probability `p` for the normal distribution with specified mean and standard deviation. For proportions, you use this to find the critical value or threshold proportion associated with a given cumulative probability.

      4. **`rnorm(n, mean = 0, sd = 1)`**: This function generates `n` random samples from the normal distribution with specified mean and standard deviation. For proportions, this can be used to simulate possible sample proportions under the assumption of normality.

      In hypothesis testing for proportions, you often standardize the sample proportion to convert it into a Z-score, which then allows you to use the standard normal distribution functions. The process typically involves the following steps:

      1. **Calculate the sample proportion** (\( \hat{p} \)).
      2. **Determine the population proportion** (\( p \)).
      3. **Compute the standard error of the proportion**: \( SE = \sqrt{\frac{p(1 – p)}{n}} \), where \( n \) is the sample size.
      4. **Calculate the Z-score**: \( Z = \frac{\hat{p} – p}{SE} \).

      Once you have the Z-score, you can use `pnorm` to find cumulative probabilities or `qnorm` to find critical values. For example, in a one-tailed test, if you want to find the probability that the sample proportion is greater than a certain value, you can use `1 – pnorm(Z)`.

      In summary, the functions `dnorm`, `pnorm`, `qnorm`, and `rnorm` are appropriate for dealing with proportions when you are working under the normal approximation to the binomial distribution. This approximation is valid when the sample size is large enough such that both \( np \) and \( n(1 – p) \) are greater than 5.

Leave a Reply

Your email address will not be published. Required fields are marked *