- Objective: Evaluate if a population mean equals a specified value.
- Use p-values to quantify evidence against the null.
ggplot(mtcars, aes(x = mpg)) + geom_histogram(binwidth = 2, fill = "steelblue", color = "white") + labs(title = "Histogram of MPG", x = "MPG", y = "Count")
n <- nrow(mtcars) df <- n - 1 t_obs <- (mean(mtcars$mpg) - 20) / (sd(mtcars$mpg) / sqrt(n)) t_vals <- seq(-4, 4, length.out = 400) curve_df <- data.frame( t = t_vals, density = dt(t_vals, df) )
graph_mins <- floor(min(mtcars$mpg))
graph_maxs <- ceiling(max(mtcars$mpg))
breaks_seq <- seq(graph_mins, graph_maxs, by = 2)
hist_data <- hist(mtcars$mpg, breaks = breaks_seq, plot = FALSE)
df_hist <- data.frame(mid = hist_data$mids, count = hist_data$counts)
scale_factor <- max(df_hist$count) / max(curve_df$density)
scaled_density <- curve_df$density * scale_factor
fig <- plot_ly(df_hist, x = ~mid, y = ~count, type = 'bar', name = 'Sample') %>%
add_lines(x = curve_df$t * (breaks_seq[2] - breaks_seq[1]) + mean(breaks_seq),
y = scaled_density,
name = 'Scaled t-Distribution', inherit = FALSE)
fig
test <- t.test(mtcars$mpg, mu = 20) print(test)
## ## One Sample t-test ## ## data: mtcars$mpg ## t = 0.08506, df = 31, p-value = 0.9328 ## alternative hypothesis: true mean is not equal to 20 ## 95 percent confidence interval: ## 17.91768 22.26357 ## sample estimates: ## mean of x ## 20.09062