In this report I treat the Diamonds dataset from the Stat2Data package as a population, and I study the variable TotalPrice, measured in dollars. I use simulation and the Central Limit Theorem to study the sampling distribution of the sample mean for samples of size n = 30.
library(Stat2Data)
library(dplyr)
library(ggplot2)
data(Diamonds)
ggplot(Diamonds, aes(x = TotalPrice)) +
geom_histogram(bins = 30, fill = "lightblue", color = "black") +
labs(title = "Diamond Prices (Population)", x = "Total price ($)", y = "Count") +
theme_minimal()
mu <- mean(Diamonds$TotalPrice)
sigma <- sd(Diamonds$TotalPrice)
mu
## [1] 7450.012
sigma
## [1] 7780.894
It is unimodal and skewed right. The mu is 7450.012 and the sigma is 7780.894.
n <- 30
B <- 1000
set.seed(123)
sample_means <- replicate(B, mean(sample(Diamonds$TotalPrice, size = n, replace = TRUE)))
mean(sample_means)
## [1] 7459.631
sd(sample_means)
## [1] 1405.478
ggplot(data.frame(xbar = sample_means), aes(x = xbar)) +
geom_histogram(bins = 30, fill = "lightgreen", color = "black") +
labs(title = "Simulated Sampling Distribution of Mean Diamond Price (n = 30)",
x = "Sample mean price ($)", y = "Count") +
theme_minimal()
They are both unimodal, however, the shape of step 2 is more symmetrical (moving more towards a normal model). Whereas, step 1 is skewed right.
SE <- sigma / sqrt(n)
SE
## [1] 1420.59
ggplot(data.frame(xbar = sample_means), aes(x = xbar)) +
geom_histogram(aes(y = after_stat(density)), bins = 30,
fill = "lightgreen", color = "black") +
stat_function(fun = dnorm, args = list(mean = mu, sd = SE),
color = "red", linewidth = 1) +
labs(title = "Simulated Mean Diamond Prices with the CLT's Normal Model",
x = "Sample mean price ($)", y = "Density") +
theme_minimal()
The CLT’s model matches really well. the mu and SE, the mean and SD, are very similar almost matching.
What is the probability that a random sample of 30 diamonds has a mean price greater than $10,000?
1 - pnorm(10000, mean = mu, sd = SE) # CLT answer
## [1] 0.03632525
mean(sample_means > 10000) # simulation answer
## [1] 0.05
The CLT gives a theoretical sampling distribution of the mean. The simulation produces a calculated empirical approximation.
90% of samples of 30 diamonds will have a mean price above what value?
qnorm(0.10, mean = mu, sd = SE) # CLT answer
## [1] 5629.452
quantile(sample_means, 0.10) # simulation check
## 10%
## 5816.595
$5816.595 is the average price of 30 diamonds.
[3-4 sentences: summarize what the simulation and the CLT each told you about the sampling distribution, and name one thing you would want to investigate further, such as what changes with a different sample size.]