Hypothesis testing is a statistical method used to make decisions about a population using sample data.

We start with two statements:

\[ H_0: \mu = \text{Null hypothesis} \]

\[ H_a: \mu = \text{Alternative hypothesis} \]

We use sample evidence to determine whether to reject or fail to reject \(H_0\).

  • Is the true average rating different from a claimed value?
  • Is one movie better rated than others?
  • Did a change (director, cast, genre) affect ratings?

Hypothesis testing allows decisions based on probability, not guesswork.

Example: A fan claims the average IMDb rating for Tarantino movies is 8.5. We want to test if the true mean is higher.

\[ H_0: \mu = 8.5 \]

\[ H_a: \mu > 8.5 \]

We use a one-sample t-test.

Test Statistic Formula: \[ t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}} \]

Where:

  • \(\bar{x}\): sample mean
  • \(s\): sample SD
  • \(n\): sample size
  • \(\mu_0\): hypothesized mean
    The test statistic follows a t-distribution with n-1 degrees of freedom.

We will create:

  • A ggplot histogram
  • A ggplot dot plot
  • A plotly 3D visualization

ggplot(data.frame(ratings), aes(x = ratings)) +
geom_histogram(binwidth = 0.2, fill = "darkred", color = "white") +
geom_vline(xintercept = mu0, color = "black", linetype = "dashed", size = 1) +
labs(title = "Histogram of Tarantino Movie Ratings",
x = "IMDb Rating",
y = "Count")

## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot(data.frame(ratings), aes(x = ratings)) +
geom_dotplot(binwidth = 0.05, fill = "gold") +
geom_vline(xintercept = mu0, color = "black", linetype = "dashed", size = 1) +
labs(title = "Dot Plot: Tarantino Ratings vs Hypothesized Mean",
x = "IMDb Rating",
y = "Density")

t_test_result <- t.test(ratings, mu = mu0, alternative = "greater")
t_test_result
## 
##  One Sample t-test
## 
## data:  ratings
## t = 2.7695, df = 49, p-value = 0.003953
## alternative hypothesis: true mean is greater than 8.5
## 95 percent confidence interval:
##  8.54341     Inf
## sample estimates:
## mean of x 
##      8.61

n_vals <- 1:length(ratings)
mean_vals <- cumsum(ratings)/n_vals
t_stats <- (mean_vals - mu0)/(sd(ratings)/sqrt(n_vals))

plot_ly(
x = ~n_vals,
y = ~mean_vals,
z = ~t_stats,
type = "scatter3d",
mode = "markers",
marker = list(size = 4, color = 'purple')
) %>%
layout(title = "3D View: Sample Size, Mean, and t-statistic")