---
title: "Lab 8: More Statistical Inference"
author: "Your name"
format: html
editor: visual
editor_options:
  chunk_output_type: console
---

## Setup

```{r}
library(dplyr)
library(ggplot2)
library(tibble)
```

# Part I: Paired data

```{r}
zinc <- tibble(
  location = 1:10,
  bottom = c(
    0.430, 0.266, 0.567, 0.531, 0.707,
    0.716, 0.651, 0.589, 0.469, 0.723
  ),
  surface = c(
    0.415, 0.238, 0.390, 0.410, 0.605,
    0.609, 0.632, 0.523, 0.411, 0.612
  )
)

zinc <- zinc |>
  mutate(diff = bottom - surface)
```

```{r}
zinc |>
  summarise(
    mean_difference = mean(diff),
    sd_difference = sd(diff),
    n = n()
  )
```

```{r}
t.test(
  zinc$bottom,
  zinc$surface,
  paired = TRUE
)
```

1. Why are these observations paired rather than independent?

> 

2. What does a positive value of `diff` mean?

> 

3. State the null hypothesis in terms of the mean paired difference.

> 

4. Is there evidence that average zinc concentration differs between bottom and surface water?

> 

# Part II: American salaries

```{r}
load(url(
  "https://github.com/GarciaRios/govt_3990/raw/gh-pages/Labs/lab6/acs.RData"
))

glimpse(acs)
```

```{r}
acs |>
  count(employment) |>
  mutate(
    percent = 100 * n / sum(n)
  )
```

5. What percentage of the sample is employed?

> 

```{r}
acs_emp <- acs |>
  filter(employment == "employed")
```

## Gender and income

```{r}
ggplot(
  acs_emp,
  aes(x = gender, y = income)
) +
  geom_boxplot() +
  theme_minimal()
```

```{r}
acs_emp |>
  group_by(gender) |>
  summarise(
    mean_income = mean(income, na.rm = TRUE),
    sd_income = sd(income, na.rm = TRUE),
    n = sum(!is.na(income))
  )
```

6. How do average incomes differ across the two groups?

> 

7. What does the boxplot show that the means alone do not?

> 

```{r}
income_gender_test <- t.test(
  income ~ gender,
  data = acs_emp,
  alternative = "two.sided",
  conf.level = 0.95
)

income_gender_test
```

8. State \(H_0\) and \(H_A\).

> 

9. Report the p-value and 95% confidence interval.

> 

10. Interpret the population difference in context.

> 

11. What does this analysis not establish?

> 

# Part III: Could hours worked matter?

```{r}
acs_emp <- acs_emp |>
  mutate(
    emp_type = case_when(
      hrs_work >= 40 ~ "full time",
      TRUE ~ "part time"
    )
  )
```

```{r}
acs_emp |>
  count(gender, emp_type) |>
  group_by(gender) |>
  mutate(
    percent = 100 * n / sum(n)
  )
```

12. Are the gender groups equally distributed across full-time and part-time work?

> 

```{r}
acs_full <- acs_emp |>
  filter(emp_type == "full time")

acs_part <- acs_emp |>
  filter(emp_type == "part time")
```

### Full-time workers

```{r}
acs_full |>
  group_by(gender) |>
  summarise(
    mean_income = mean(income, na.rm = TRUE),
    sd_income = sd(income, na.rm = TRUE),
    n = sum(!is.na(income))
  )
```

```{r}
t.test(
  income ~ gender,
  data = acs_full
)
```

### Part-time workers

```{r}
acs_part |>
  group_by(gender) |>
  summarise(
    mean_income = mean(income, na.rm = TRUE),
    sd_income = sd(income, na.rm = TRUE),
    n = sum(!is.na(income))
  )
```

```{r}
t.test(
  income ~ gender,
  data = acs_part
)
```

13. Does the income difference remain among full-time workers?

> 

14. Does it remain among part-time workers?

> 

15. What do these results suggest about whether work hours may help explain part of the original difference?

> 

# Part IV: More than two groups

```{r}
acs_emp |>
  count(edu)
```

```{r}
acs_emp |>
  group_by(edu) |>
  summarise(
    mean_income = mean(income, na.rm = TRUE),
    sd_income = sd(income, na.rm = TRUE),
    n = sum(!is.na(income))
  )
```

```{r}
ggplot(
  acs_emp,
  aes(x = edu, y = income)
) +
  geom_boxplot() +
  theme_minimal()
```

16. How many population means are being compared?

> 

17. Why would separate t-tests be a poor first approach?

> 

```{r}
income_edu_model <- aov(
  income ~ edu,
  data = acs_emp
)

summary(income_edu_model)
```

18. What is the overall ANOVA p-value?

> 

19. At \(\alpha=0.05\), reject or fail to reject \(H_0\).

> 

20. What can you conclude from the ANOVA?

> 

21. What can you not yet conclude?

> 

```{r}
TukeyHSD(income_edu_model)
```

First, look at the default Tukey plot:

```{r}
plot(
  TukeyHSD(income_edu_model)
)
```

Now recreate the same information with `ggplot2`:

```{r}
res <- TukeyHSD(income_edu_model)

df_tukey <- res$edu |>
  as.data.frame() |>
  tibble::as_tibble(rownames = "comparison")

ggplot(
  df_tukey,
  aes(
    x = reorder(comparison, diff),
    y = diff,
    ymin = lwr,
    ymax = upr
  )
) +
  geom_pointrange(linewidth = 1) +
  geom_hline(
    yintercept = 0,
    linetype = "dashed"
  ) +
  coord_flip() +
  labs(
    x = NULL,
    y = "Difference in mean income ($)",
    title = "Pairwise differences in mean income",
    subtitle = "Tukey-adjusted 95% confidence intervals"
  ) +
  theme_minimal()
```

22. Which pairwise comparisons show evidence of different population means?

> 

23. How can you tell from the adjusted p-values and confidence intervals?

> 

24. Why do we adjust the pairwise comparisons?

> 

# Group Policy Analysis

Your instructor will assign your group one question.

## Group A
Citizenship and income: `income ~ citizens`

## Group B
Language and commute time: `time_to_work ~ lang`

## Group C
Race and income: `income ~ race`

## Group D
Education and commute time: `time_to_work ~ edu`

## Group E
Education and weekly hours worked: `hrs_work ~ edu`

### Method

Two groups or three-plus groups?

> 

Appropriate method:

> 

Why?

> 

### Visualization

```{r}

```

### Descriptive statistics

```{r}

```

### Hypotheses

> 

### Test

```{r}

```

### Post-hoc comparisons, if needed

```{r}

```

### Two-minute briefing

> **Our question was...**

> 

> **The appropriate method was... because...**

> 

> **We found...**

> 

> **For policy, this may matter because...**

> 

> **One important limitation is...**

> 

# On Your Own

25. Write a policy-relevant research question using one numerical outcome and one categorical grouping variable.

> 

26. Identify the appropriate method and explain why.

> 

27. Create a visualization and descriptive statistics.

```{r}

```

28. State \(H_0\) and \(H_A\).

> 

29. Conduct the appropriate hypothesis test.

```{r}

```

30. If appropriate, conduct adjusted post-hoc comparisons.

```{r}

```

31. Write a short policy interpretation.

> 