```{r}
#| label: setup
#| message: false
#| warning: false
library(tidyverse)
library(knitr)

# Set a clean theme for all plots
theme_set(theme_light(base_size = 14))

# Turn off scientific notation
options(scipen = 999)
```

# Inference for Means

In this discussion, we cover inference for a single mean and the difference between two means using the $t$-distribution. We will use examples from the OpenIntro Statistics book.

---

# Notation and Concepts

When dealing with numerical data, we use different symbols for sample statistics (estimates) and population parameters.

| Variable | Sample Statistic | Population Parameter |
|:---|:---:|:---:|
| **Mean** | $\bar{x}$ | $\mu$ |
| **Standard Deviation** | $s$ | $\sigma$ |
| **Variance** | $s^2$ | $\sigma^2$ |
| **Size** | $n$ | $N$ |

::: {.callout-note}
### Why the $t$-distribution?
When we don't know the population standard deviation $\sigma$ (which is almost always), we use the sample standard deviation $s$. This introduces extra uncertainty, so we use the **$t$-distribution** instead of the normal distribution. The shape of the $t$-distribution depends on the **degrees of freedom** ($df = n - 1$).
:::

---

# Inference for a Single Mean

## Confidence Intervals

The general form of a confidence interval remains:
$$\text{Point Estimate} \pm \text{Critical Value} \times \text{Standard Error}$$

For a single mean, this becomes:
$$\bar{x} \pm t_{df}^* \times SE_{\bar{x}}$$

Where the standard error is:
$$SE_{\bar{x}} = \frac{s}{\sqrt{n}}$$

### Conditions for Inference
1.  **Independence:** The data should be a random sample (or $<10\%$ of the population).
2.  **Normality:** The population should be nearly normal, OR the sample size should be large ($n \ge 30$).

## Example: UCI Student Heights

Given the heights (in feet) of 50 random students at UCI, construct a 95% confidence interval for the average height.

```{r}
#| label: heights-data
heights <- c(5.75, 6.22, 5.52, 5.55, 5.48, 4.72, 5.84, 5.38, 5.74, 6.20, 
             6.78, 5.74, 5.21, 5.55, 6.70, 4.73, 5.86, 5.38, 4.98, 5.17, 
             6.17, 5.62, 5.98, 5.62, 5.96, 6.18, 5.12, 5.92, 5.38, 4.78, 
             4.46, 5.50, 5.22, 5.38, 5.54, 5.06, 5.66, 5.50, 6.33, 4.68, 
             5.45, 6.69, 5.60, 4.90, 5.18, 5.46, 5.47, 5.43, 5.58, 5.71)

# Summary statistics
n <- length(heights)
x_bar <- mean(heights)
s <- sd(heights)
se <- s / sqrt(n)

# Critical value for 95% confidence (alpha = 0.05)
t_star <- qt(0.975, df = n - 1)

# Confidence Interval
ci_lower <- x_bar - t_star * se
ci_upper <- x_bar + t_star * se

# Display results
tibble(
  Measure = c("Sample Mean", "SD", "SE", "t*", "Lower Bound", "Upper Bound"),
  Value = c(x_bar, s, se, t_star, ci_lower, ci_upper)
) %>% kable(digits = 3)
```

**Interpretation:** We are 95% confident that the true average height of UCI students is between `r round(ci_lower, 3)` and `r round(ci_upper, 3)` feet.

## Hypothesis Testing

**Question:** Is there evidence that the average height of UCI students is different from 6 feet?

### 1. Set Hypotheses
- $H_0: \mu = 6$
- $H_A: \mu \neq 6$

### 2. Calculate the Test Statistic ($t$)
$$t = \frac{\bar{x} - \mu_0}{SE_{\bar{x}}}$$

```{r}
#| label: single-mean-test
t_stat <- (x_bar - 6) / se
p_val <- 2 * pt(-abs(t_stat), df = n - 1) # Two-tailed test

cat("t-statistic:", round(t_stat, 4), "\n")
cat("p-value:", round(p_val, 10), "\n")
```

### 3. Conclusion
Since the p-value (`r format(p_val, scientific=TRUE)`) is significantly less than $\alpha = 0.05$, we **reject the null hypothesis**. There is strong evidence that the average height of UCI students is different from 6 feet.


# Practice: Sleep Habits in NY

::: {.callout-tip}
### Exercise 7.7 (OpenIntro Statistics)
A survey of 25 New Yorkers found that they sleep an average of 7.73 hours per night, with a standard deviation of 0.77 hours. Does this provide evidence that New Yorkers sleep a different amount than the recommended 8 hours per night?
:::

### 1. Identify Hypotheses
- $H_0: \mu = 8$
- $H_A: \mu \neq 8$

### 2. Check Conditions
- **Independence:** Assuming the 25 New Yorkers are a random sample.
- **Normality:** $n = 25$ is less than 30. We would need to check the original data for strong skew or outliers. For this exercise, we will proceed with caution.

### 3. Calculate the Test Statistic and P-value

```{r}
#| label: exercise-sleep
n_ny <- 25
x_bar_ny <- 7.73
s_ny <- 0.77
mu_0 <- 8

se_ny <- s_ny / sqrt(n_ny)
t_ny <- (x_bar_ny - mu_0) / se_ny
p_val_ny <- 2 * pt(-abs(t_ny), df = n_ny - 1) # Two-tailed test

cat("t-statistic:", round(t_ny, 4), "\n")
cat("p-value:", round(p_val_ny, 4), "\n")
```

### 4. Decision at different $\alpha$ levels

- **At $\alpha = 0.05$:** Since $p \approx 0.096 > 0.05$, we **fail to reject** the null hypothesis. We do not have sufficient evidence to say New Yorkers sleep a different amount than 8 hours.
- **At $\alpha = 0.10$:** Since $p \approx 0.096 < 0.10$, we **reject** the null hypothesis.

::: {.callout-important}
### Connection to Confidence Intervals
- If the null value ($\mu_0 = 8$) is **inside** the 95% CI, we will **fail to reject** $H_0$ at $\alpha = 0.05$.
- If the null value is **outside** the 95% CI, we will **reject** $H_0$ at $\alpha = 0.05$.
:::


# Inference for Difference of Two Means

When comparing two independent groups, we are interested in the difference between their population means ($\mu_1 - \mu_2$).

## Example: Diamond Pricing

Does the price of diamonds change significantly between 0.99 carats and 1 carat? We use two random samples (size 33 each) and look at standardized prices.

### Summary Statistics

```{r}
#| label: diamond-stats
diamond_stats <- tibble(
  Group = c("0.99 carats", "1.00 carat"),
  Mean = c(44.51, 56.81),
  SD = c(13.32, 16.13),
  n = c(33, 33)
)

kable(diamond_stats, caption = "Standardized Diamond Prices")
```

### 1. Hypotheses
- $H_0: \mu_{0.99} - \mu_{1.00} = 0$ (No difference)
- $H_A: \mu_{0.99} - \mu_{1.00} \neq 0$ (There is a difference)

### 2. Check Conditions
- **Independence:** Random samples, and each is $< 10\%$ of the population. Groups are independent of each other.
- **Normality:** Both $n_1, n_2 > 30$, so the sampling distribution of the difference will be nearly normal.

### 3. Calculations

The standard error for the difference in means is:
$$SE_{(\bar{x}_1 - \bar{x}_2)} = \sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}$$

The $t$-statistic is:
$$t = \frac{(\bar{x}_1 - \bar{x}_2) - 0}{SE_{(\bar{x}_1 - \bar{x}_2)}}$$

The degrees of freedom can be approximated using $df = \min(n_1 - 1, n_2 - 1)$.

```{r}
#| label: diamond-test
# Extract stats
m1 <- diamond_stats$Mean[1]; s1 <- diamond_stats$SD[1]; n1 <- diamond_stats$n[1]
m2 <- diamond_stats$Mean[2]; s2 <- diamond_stats$SD[2]; n2 <- diamond_stats$n[2]

# Calculations
diff_means <- m1 - m2
se_diff <- sqrt((s1^2 / n1) + (s2^2 / n2))
t_diam <- diff_means / se_diff
df_diam <- min(n1 - 1, n2 - 1)
p_val_diam <- 2 * pt(-abs(t_diam), df = df_diam) # Two-tailed test

cat("Difference in Means:", diff_means, "\n")
cat("Standard Error:", round(se_diff, 4), "\n")
cat("t-statistic:", round(t_diam, 4), "\n")
cat("p-value:", round(p_val_diam, 4), "\n")
```

### 4. Interpretation
Since $p \approx 0.0019 < 0.05$, we **reject the null hypothesis**. There is strong evidence that the average standardized price is different between 0.99 and 1 carat diamonds.

---

## Confidence Interval for the Difference

$$\text{CI} = (\bar{x}_1 - \bar{x}_2) \pm t_{df}^* \times SE_{diff}$$

```{r}
#| label: diamond-ci
t_star_diam <- qt(0.975, df = df_diam)
ci_low <- diff_means - t_star_diam * se_diff
ci_high <- diff_means + t_star_diam * se_diff

cat("95% CI: (", round(ci_low, 2), ", ", round(ci_high, 2), ")\n", sep="")
```

**Interpretation:** We are 95% confident that the true difference in average standardized prices falls between `r round(ci_low, 2)` and `r round(ci_high, 2)`. Since the entire interval is negative, it indicates that 1 carat diamonds are, on average, more expensive than 0.99 carat diamonds.


# Unpaired vs. Paired Data

It's crucial to identify whether your two samples are independent or dependent (paired).

### Unpaired (Independent)
- Two separate groups (e.g., men vs. women, Group A vs. Group B).
- We compare the difference between two means ($\mu_1 - \mu_2$).

### Paired (Dependent)
- The same individuals measured twice (e.g., before/after) or naturally matched pairs (e.g., twins, husbands/wives).
- We look at the **mean of the differences** ($\mu_{diff}$).
- This effectively becomes a **1-sample mean test** on the difference scores.

**Example of Paired Data:**
Suppose we track exam scores for 4 students before and after a tutoring session.

```{r}
#| label: paired-example
scores <- tibble(
  Student = 1:4,
  Before = c(80, 85, 92, 78),
  After = c(87, 85, 95, 80)
) %>%
  mutate(Difference = After - Before)

kable(scores)

# For a hypothesis test, we would look at the Mean of 'Difference'
mean_diff <- mean(scores$Difference)
cat("Average improvement:", mean_diff, "\n")
```

To test if the tutoring helped, we would test:
- $H_0: \mu_{diff} = 0$
- $H_A: \mu_{diff} \neq 0$

We would then use the 1-sample method learned at the beginning of this discussion.
