R - Normal Distribution
R - The Normal Distribution
Section titled “R - The Normal Distribution”In statistics, many random processes demonstrate a normal distribution. When plotted, this distribution forms a symmetrical, bell-shaped curve. The center of the curve represents the mean (average) of the data, and its spread is determined by the standard deviation. The normal distribution is a cornerstone of statistical inference and modeling.
R provides a suite of four powerful functions for working with the normal distribution.
dnorm(x, mean = 0, sd = 1)pnorm(q, mean = 0, sd = 1)qnorm(p, mean = 0, sd = 1)rnorm(n, mean = 0, sd = 1)Following is the description of the parameters used in the above functions:
- x, q: A vector of quantiles (the values on the x-axis).
- p: A vector of probabilities (values between 0 and 1).
- n: The number of observations (sample size) to generate.
- mean: The mean of the distribution. Defaults to 0.
- sd: The standard deviation of the distribution. Defaults to 1.
Modern Visualization with ggplot2
Section titled “Modern Visualization with ggplot2”For all plotting examples, we will use the ggplot2 package, which is the modern standard for data visualization in R. It provides a more powerful and flexible system called the Grammar of Graphics. You can install it with install.packages("ggplot2"), or as part of the tidyverse.
dnorm(): The Density Function
Section titled “dnorm(): The Density Function”The dnorm() function calculates the probability density (the height of the bell curve) at specific points x for a given mean and sd.
library(ggplot2)
# Create a sequence of x-valuesx <- seq(-4, 4, by = 0.1)
# Calculate the density (y-values) for a standard normal distributiony <- dnorm(x, mean = 0, sd = 1)
# Create a data frame for plottingdata_dnorm <- data.frame(x, y)
# Plot the bell curve using ggplot2ggplot(data_dnorm, aes(x = x, y = y)) + geom_line(color = "blue", size = 1) + labs( title = "Probability Density Function (PDF) of a Standard Normal Distribution", x = "Z-score", y = "Density" ) + theme_minimal()The code above generates a line plot. You will see the classic bell-shaped curve, centered at a mean of 0 with a standard deviation of 1. The total area under the curve is equal to 1.
pnorm(): The Cumulative Distribution Function (CDF)
Section titled “pnorm(): The Cumulative Distribution Function (CDF)”pnorm() calculates the cumulative probability, which is the area under the curve to the left of a given value q. It tells you the probability that a random draw from this distribution will be less than or equal to q.
library(ggplot2)
# Create a sequence of x-valuesx <- seq(-4, 4, by = 0.1)
# Calculate the cumulative probability (y-values)y <- pnorm(x, mean = 0, sd = 1)
data_pnorm <- data.frame(x, y)
# Plot the S-shaped CDF curveggplot(data_pnorm, aes(x = x, y = y)) + geom_line(color = "darkgreen", size = 1) + labs( title = "Cumulative Distribution Function (CDF) of a Standard Normal Distribution", subtitle = "Probability of Z <= x", x = "Z-score", y = "Cumulative Probability" ) + theme_minimal()This code produces a line plot showing a characteristic ‘S’-shaped curve (an ogive). The y-axis starts at 0 and approaches 1 as x increases, representing the accumulating area under the density curve.
qnorm(): The Quantile Function
Section titled “qnorm(): The Quantile Function”qnorm() is the inverse of pnorm(). You provide a probability p (an area), and it returns the corresponding quantile (the x-axis value).
library(ggplot2)
# Create a sequence of probabilities from 0 to 1x <- seq(0, 1, by = 0.01)
# Find the z-score (quantile) for each probabilityy <- qnorm(x, mean = 0, sd = 1)
data_qnorm <- data.frame(x, y)
# Plot the quantile functionggplot(data_qnorm, aes(x = x, y = y)) + geom_line(color = "purple", size = 1) + labs( title = "Quantile Function of a Standard Normal Distribution", subtitle = "Inverse of the CDF", x = "Probability (Area to the Left)", y = "Z-score (Quantile)" ) + theme_minimal()The plot generated by this code is the mirror image of the CDF plot, flipped along the y=x line. As the probability on the x-axis goes from 0 to 1, the z-score on the y-axis goes from negative infinity to positive infinity.
rnorm(): Random Number Generation
Section titled “rnorm(): Random Number Generation”rnorm() generates n random numbers from a specified normal distribution. This is essential for simulations and statistical experiments. We can visualize the distribution of these random numbers using a histogram.
library(ggplot2)
# Generate 1000 random numbers from a normal distribution with mean=50, sd=10set.seed(42) # for reproducibilityrandom_numbers <- rnorm(n = 1000, mean = 50, sd = 10)
data_rnorm <- data.frame(value = random_numbers)
# Plot a histogram to visualize the distributionggplot(data_rnorm, aes(x = value)) + geom_histogram(aes(y = ..density..), binwidth = 5, fill = "skyblue", color = "black") + geom_density(color = "red", size = 1) + # Overlay a density curve labs( title = "Histogram of 1000 Random Normal Variates", subtitle = "Mean=50, SD=10", x = "Generated Value", y = "Density" ) + theme_minimal()This code generates a histogram where the bars approximate the bell shape of the specified normal distribution. The overlaid red density curve helps to clearly show this underlying shape, which should be centered around 50.