---
title: "Problem Set 1: Visualization"
format: pdf
---

**Due: 5pm on Wednesday, January 31.**

Student identifer: [type your anonymous identifying number here]

- Use this template to complete the problem set
- In Canvas, you will upload the PDF produced by your .qmd file
- Put your identifier above, not your name! We want anonymous grading to be possible

This problem set involves both data analysis and reading.

### Data analysis

This problem set uses the data [`lifeCourse.csv`](https://info3370.github.io/data/lifeCourse.csv).

```{r, comment = F, message = F}
library(tidyverse)
library(scales)
lifeCourse <- read_csv("https://info3370.github.io/data/lifeCourse.csv")
```

The data contain life course earnings profiles for four cohorts of American workers: those born in 1940, 1950, 1960, and 1970. Each row contains a summary of the annual earnings distribution for a particular birth cohort at a particular age, among the subgroup with a particular level of education. To prepare these data, we aggregated microdata from the [Current Population Survey](https://cps.ipums.org/cps/), provided through the Integrated Public Use Microdata Series.

The data contain five variables.

1. `quantity` is the metric by which the earnings distribution is summarized: 10th, 50th, or 90th percentile
2. `education` is the educational subgroup being summarized: `r unique(lifeCourse$education)`
3. `cohort` is the cohort (people with a given birth year) to which these data apply: `r unique(lifeCourse$cohort)`
4. `age` is the age at which earnings were measured: `r min(lifeCourse$age)`--`r max(lifeCourse$age)`
5. `income` is the value for the given earnings percentile in the given subgroup. Income values are provided in 2022 dollars

# 1. Visualize (25 points)

Use `ggplot` to visualize these data. To denote the different trajectories,

- make your plot using `geom_point()` or `geom_line()`
- use the x-axis for `age`
- use the y-axis for `income`
- use `color` for `quantity`
- use `facet_grid` to make a panel of facets where each row is an education value and each column is a cohort value

You should prepare the graph as though you were going to publish it. Modify the axis titles so that a reader would know what is on the axis. Use appropriate capitalization in all labels. Try using the `label_dollar()` function from the `scales` package so that the y-axis uses dollar values.

Your code should be well-formatted as defined by [R4DS](https://r4ds.hadley.nz/workflow-style.html). In your produced PDF, no lines of code should run off the page.

Many different graphs can be equally correct. You will be evaluated by

- having publication-ready graph aesthetics
- code that follows style conventions

```{r}
# your code goes here

```

# 2. Interpret (10 points)

Write 2-3 sentences summarizing the trends that you see in the data.

**2.1 (3 points).** Focus on those born in 1970. For those with a college degree, how do the top and bottom of the income distribution change over the life course?

> Type your answer here.

**2.2 (3 points).** Focus on those born in 1970. How does the pattern differ for those without college degres differ from your answer in 2.1?

> Type your answer here.


**2.3 (4 points).** How do the patterns you identified in 2.1 and 2.2 change from the 1940 to the 1970 cohort?

> Type your answer here. 

# 3. Connect to reading (15 points)

Read p. 1--7 of following paper. Stop before the section "Analytic Framework for Decomposing Inequality."

> Cheng, Siwei. 2021. [The shifting life course patterns of wage inequality.](https://doi.org/10.1093/sf/soab003). _Social Forces_ 100(1):1--28.

Our data are not the same as Cheng's. But our analysis is able to reproduce many of her findings. Answer each question in two sentences or less.

Cheng discusses period trends, cohort trends, and age trends. 

**3.1 (3 points)** Which dimension of your graph shows a cohort trend?

> Type your answer here.

**3.2 (3 points)** Which dimension of your graph shows an age trend?

> Type your answer here.

**3.3 (3 points)** Cheng discusses education-based cumulative advantage. Describe how you see this in your graph.

> Type your answer here.

**3.4 (3 points)** Cheng discusses within-education trajectory heterogeneity. Describe how your graph shows heterogeneity of outcomes within educational categories.

> Type your answer here.

**3.5 (3 points)** Cheng discusses wage volatility: how wages rise and fall over time for a given person. Why is our data (the Current Population Survey) the wrong dataset to study wage volatility?

> Type your answer here.

# Grad question: Model-based estimates

This question assumes familiarity with Ordinary Least Squares.

- For graduate students, this question is worth 20 points.
- For undergraduate students, this question is optional and worth 0 points.

The data contain nonparametric estimates that contain some noise: the data points provided partly reflect random variation because they are estimatd in a sample.

Model-based estimates reduce noise by pooling information across observations, at the cost of introducing assumptions. Fit an OLS model to the data using `age` as a numeric variable and `education`, `cohort`, and `quantity` as factor variables. Interact all of these predictors with each other.

```{r}
fit <- lm(
  income ~ age * factor(cohort) * education * quantity,
  data = lifeCourse
)
```

Effectively, this estimates a best-fit line through each set of points depicted in your original figure. For each observation, store a prediction from this model (see [`predict()`](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/predict.lm.html)).

Re-create your plot from (1) using

- `geom_point()` for the nonparametric estimates (as above)
- `geom_line()` for your model-based predicted values

# Computing environment

Leave this at the bottom of your file, and it will record information such as your operating system, R version, and package versions. This is helpful for resolving any differences in results across people.

```{r}
sessionInfo()
```