Chapter 8 - Distributions

8.1 Normal Distribution

library(ggplot2)
# set the mean and standard deviation of the normal distribution
mu <- 0
sigma <- 1

# create a sequence of x values from -4 to 4 with a step size of 0.01
x <- seq(-4, 4, 0.01)

# calculate the probability density function (PDF) of the normal distribution for each x value
pdf <- dnorm(x, mean = mu, sd = sigma)

# combine the x and pdf values into a data frame
df <- data.frame(x, pdf)

# plot the normal distribution using ggplot2
ggplot(df, aes(x, pdf)) + 
  geom_line() +
  labs(x = "x", y = "Probability Density") +
  ggtitle("Normal Distribution with Mean = 0 and Standard Deviation = 1")

Figure 8.1: A normal curve.

8.1.2 Standardizing with Z-scores

# calculate the probability P(Z<0.43) - i.e. what is the probability of standard
# normal variable taking a value less than 0.43?
pnorm(q = 0.43, mean = 0, sd = 1)
## [1] 0.6664022
#> [1] 0.666
# calculate the $Z$-score satisfying P(Z<z)=0.80 - i.e. what value is at the 80th
# percentile of a standard normal distribution?
qnorm(p = 0.8, mean = 0, sd = 1)
## [1] 0.8416212

8.4.2 Normal approximation to the binomial distribution

# calculate the probability P(X<=39) when X has Bin(n=400, p=0.145) distribution
pbinom(q = 39, size = 400, prob = 0.145, lower.tail = TRUE)
## [1] 0.003025954