- Interval estimation provides a range of values to estimate a population parameter.
- It is commonly used for confidence intervals (CIs).
- Example: Estimating the true mean height of students based on a sample.
2025-02-10
The confidence interval (CI) for a population mean is given by:
\[ \bar{x} \pm t^* \left(\frac{s}{\sqrt{n}}\right) \]
where: - \(\bar{x}\) = Sample mean - \(t^*\) = Critical value from the t-distribution - \(s\) = Sample standard deviation - \(n\) = Sample size
# Load libraries
library(ggplot2)
library(plotly)
# Generate a sample dataset (30 values from normal distribution)
set.seed(123)
sample_data <- rnorm(30, mean = 50, sd = 10)
# Calculate sample statistics
sample_mean <- mean(sample_data)
sample_sd <- sd(sample_data)
n <- length(sample_data)
# Compute the 95% Confidence Interval
error_margin <- qt(0.975, df = n-1) * (sample_sd / sqrt(n))
ci_lower <- sample_mean - error_margin
ci_upper <- sample_mean + error_margin
# Print the CI result
cat("95% Confidence Interval: (", round(ci_lower, 2), ",", round(ci_upper, 2), ") \n")
## 95% Confidence Interval: ( 45.87 , 53.19 )
# Create a histogram with Confidence Interval visualization ggplot(data.frame(sample_data), aes(x = sample_data)) + geom_histogram(binwidth = 5, fill = "blue", alpha = 0.5) + geom_vline(xintercept = c(ci_lower, ci_upper), color = "red", linetype = "dashed", size = 1.2) + geom_vline(xintercept = sample_mean, color = "black", linetype = "solid", size = 1.5) + labs(title = "Confidence Interval for Sample Mean", x = "Value", y = "Frequency") + theme_minimal()
# Generate a 3D plot (for visualization) x <- seq(-10, 10, length.out = 30) y <- seq(-10, 10, length.out = 30) z <- outer(x, y, function(x, y) dnorm(x, mean = 0, sd = 1) * dnorm(y, mean = 0, sd = 1)) p2 <- plot_ly(x = ~x, y = ~y, z = ~z, type = "surface") p2