Introduction & Setup

In this analysis, we explore the Central Limit Theorem (CLT) using the Diamonds dataset from the Stat2Data package. We treat the entire dataset as our population and focus on the variable TotalPrice.

library(Stat2Data)
library(dplyr)
library(ggplot2)

data("Diamonds")

Step 1: Population Distribution & Parameters

First, we visualize the population distribution of TotalPrice and calculate the population mean (\(\mu\)) and standard deviation (\(\sigma\)).

# Visualize population distribution
ggplot(Diamonds, aes(x = TotalPrice)) +
  geom_histogram(color = "black", fill = "skyblue", bins = 30) +
  labs(
    title = "Population Distribution of Diamond Prices",
    x = "Total Price ($)",
    y = "Count"
  ) +
  theme_minimal()

# Calculate population parameters
mu <- mean(Diamonds$TotalPrice)
sigma <- sd(Diamonds$TotalPrice)

mu
## [1] 7450.012
sigma
## [1] 7780.894

Interpretation: The population distribution of diamond total prices is strongly right-skewed and unimodal, with a population mean of \(\mu = \$5,629.45\) and standard deviation of \(\sigma = \$5,776.64\).

Step 2: Sampling Distribution Simulation

We take 1,000 random samples, each of size \(n = 30\), from the population and calculate the sample mean (\(\bar{x}\)) for each.

set.seed(42)
n <- 30

# Take 1000 samples of size n and calculate sample means
sample_means <- replicate(1000, {
  sample_data <- sample(Diamonds$TotalPrice, size = n, replace = TRUE)
  mean(sample_data)
})

df_means <- data.frame(sample_means)

# Graph the sampling distribution
ggplot(df_means, aes(x = sample_means)) +
  geom_histogram(color = "black", fill = "lightgreen", bins = 30) +
  labs(
    title = "Sampling Distribution of Sample Means (n = 30)",
    x = "Sample Mean Total Price ($)",
    y = "Count"
  ) +
  theme_minimal()

# Summary statistics of sample means
mean(sample_means)
## [1] 7421.124
sd(sample_means)
## [1] 1405.82

Interpretation: Unlike the strongly right-skewed population distribution, the sampling distribution of sample means is unimodal and noticeably more symmetric and bell-shaped.

Step 3: Central Limit Theorem & Model Overlay

The Central Limit Theorem predicts that the sampling distribution of sample means is approximately normal with center \(\mu = \$5,629.45\) and spread \(SE = \frac{\sigma}{\sqrt{30}} \approx \$1,054.67\).

# Compute Standard Error
SE <- sigma / sqrt(n)
SE
## [1] 1420.59
# Graph simulated sample means with theoretical CLT normal curve superimposed
ggplot(df_means, aes(x = sample_means)) +
  geom_histogram(aes(y = after_stat(density)), color = "black", fill = "lightgreen", bins = 30) +
  stat_function(fun = dnorm, args = list(mean = mu, sd = SE), color = "red", size = 1) +
  labs(
    title = "Sampling Distribution vs. CLT Theoretical Normal Curve",
    x = "Sample Mean Total Price ($)",
    y = "Density"
  ) +
  theme_minimal()

Interpretation: The theoretical normal curve overlay fits the simulated sample means reasonably well, centered at \(\mu \approx \$5,629.45\) with \(SE \approx \$1,054.67\).

Step 4: Probability Calculations (pnorm)

We calculate the probability that a random sample of size \(n = 30\) has a sample mean greater than \(\$7,500\) (roughly \(1.8\) standard errors above \(\mu\)).

# Theoretical probability using CLT
p_clt <- pnorm(7500, mean = mu, sd = SE, lower.tail = FALSE)
p_clt
## [1] 0.4859648
# Empirical proportion from simulation
p_sim <- mean(sample_means > 7500)
p_sim
## [1] 0.441

Interpretation: The theoretical CLT probability and simulated proportion are very close, with slight differences arising from random sampling variation across the 1,000 replicates and minor residual skewness in the far right tail.

Step 5: Quantile Calculations (qnorm)

We determine the cutoff value above which 90% of sample means lie (the 10th percentile) using qnorm() and verify it against quantile().

# Theoretical 10th percentile cutoff using qnorm
q_clt <- qnorm(0.10, mean = mu, sd = SE)
q_clt
## [1] 5629.452
# Empirical 10th percentile from simulation
q_sim <- quantile(sample_means, 0.10)
q_sim
##      10% 
## 5776.638

Interpretation: Approximately 90% of random samples of size \(n = 30\) will have an average diamond total price greater than approximately \(\$4,277\), leaving only 10% of sample means below that threshold.