library(tidyverse)
library(juanr)
library(wakefield)Problem Set 1: Causality and Data-Generating Processes
Due: Monday, October 5 by 11:59 PM. Email both your .qmd and your rendered .html to Max Welch (mwelch@ucdavis.edu).
This set leans on the R you learned at Quant Camp. When you get stuck on filtering, grouping, or plotting, go back to the Quant Camp materials. They’re the fastest way to get unstuck.
Use AI the way people used Google, R’s help pages, and Stack Overflow before AI: ask precise, limited questions to get unstuck on part of a question. Passing off AI-generated work wholesale as your own is not allowed. See the syllabus and the Department’s policy on generative AI.
Format: Write your answers in one Quarto document, saved in the sets/ folder of your course project. Start it with this header, so the HTML renders as one self-contained file:
---
title: "Problem Set 1"
author: "Your name"
format:
html:
embed-resources: true
---Your document should read as a short report: brief prose explaining your reasoning, not just code and output.
Libraries you will need:
Question 3 uses wakefield, a package for making fake data. It’s new, so install it once by running install.packages("wakefield") in the Console (not in your .qmd).
Parts marked Methods portfolio are required if you plan to choose methods as a subfield. They’re optional, and recommended, for everyone else.
Question 1: Warm-up
This question reviews a few tools from Quant Camp. It uses fearon, from my juanr package: the data behind Fearon and Laitin (2003), a classic study of why civil wars start. (If you don’t have juanr yet, the Getting Started page shows how to install it.) Once juanr is loaded, the data are in fearon.
gdp_cap is GDP per capita, in thousands of dollars. polity2 is a democracy score that runs from \(-10\) (most autocratic) to 10 (most democratic). oil is 1 if oil makes up more than a third of the country’s export earnings, and 0 otherwise.
What is the unit of analysis in
fearon?Make a scatterplot of
polity2(y) againstgdp_cap(x) for 1990 only, with labeled axes. Add a smoothing line (geom_smooth()). Why does it make sense to look at one year at a time here? What pattern do you see? Add country names to the plot. Who are the outliers? (Hint:geom_label()orgeom_text())Compute the average
gdp_capand the averagepolity2for oil and non-oil country-years. Describe the pattern.Which countries have the following characteristics: above average mountainous terrain (
mtnest), below average ethnic fractionalization (ef), exactly one language spoken by groups larger than 1% of the population (numlang)?
Question 2: Suicide terrorism
In a well-known article, Pape (2003) argues that suicide terrorism follows a strategic logic.
In Pape’s argument, what is the treatment (\(X\)) and what is the outcome (\(Y\))?
What is one row in Pape’s data (the unit of analysis)? Across all of his data, what values does \(Y\) take?
A causal claim is a comparison. Which two groups would we need to compare to learn whether \(X\) causes \(Y\) (in Pape’s study)? Which of those groups can Pape’s data tell us about, and which can’t it?
To see why this matters, you’ll build two made-up worlds. Each has 10,000 country-years. In both,
xis 1 if a country-year has the treatment you named in (a), andyis 1 if it has the outcome. The table says how many country-years of each kind each world has:Country-years of each kind in two made-up worlds. xyWorld A World B 1 1 180 160 1 0 8,820 840 0 1 20 18 0 0 980 8,982 Here is World A in R.
tribble()lets you type a small table one row at a time: the first line names the columns (each with a~in front), and each line after that is one row. Here each row is one kind of country-year, andnis how many there are.uncount(n)then copies each rowntimes, so the data end up with one row per country-year:world_a = tribble( ~x, ~y, ~n, 1, 1, 180, 1, 0, 8820, 0, 1, 20, 0, 0, 980 ) |> uncount(n)Build
world_bthe same way, using the counts in the table. Check that each world has 10,000 rows. (Hint:nrow().)In each world, among country-years with
y = 1, what share havex = 1? (Hint:filter())In each world, what share of country-years with
x = 1havey = 1? What share of country-years withx = 0do? (Hint:group_by(x)) What is the effect of \(X\) on \(Y\) in each world?
- Build a new world where \(X\) has no effect on \(Y\), but the share from (e) is 50%. (Hint: start from whichever world had no effect in (f).) What did you have to change?
Ashworth et al. (2008) make this critique of Pape formally. It’s short and worth reading.
Question 3: Build your own data-generating process
A data-generating process has a systematic part and a random part. In this question you’ll build one yourself: first with no random part, then with one, so you can see exactly what randomness changes.
Start by making a fake town with wakefield’s r_data_frame(). I won’t show you how it works: figuring out a new package from its documentation is a skill you’ll use all the time. Use it to make a town of 300 people, each with an ID, an age, a sex, and a level of education. (Hint: run ?r_data_frame in the Console and look at the examples at the bottom of the help page, or see the package’s page at https://github.com/trinker/wakefield.) Fill in the parentheses here:
set.seed(211)
town = r_data_frame()set.seed(211) makes the random draws repeatable: run the chunk again and you get the same town. The rest of this question uses the column names r_data_frame() gives you: ID, Age, Sex, and Education.
What is one row in
town? What values doesEducationtake? (Hint:count(town, Education).) Make a variablecollegethat is 1 for anyone with a bachelor’s degree or more, and 0 otherwise. (Hint:case_when().)The systematic part. Everyone uses the same outcome,
hours: the hours per week someone spends watching political news. Write a rule that makeshoursfromAgeandcollege, like this one:\[ \text{hours} = 1 + 0.1 \times \text{Age} + 2 \times \text{college} \]
Read this as: for each year of age, number of hours watched increases by 0.1. College grads (0/1) watch 2 more hours than non-grads.
Keep the same variables, but use your own numbers, and say in a sentence why you picked them. Add
hourstotownwithmutate().Show that
hoursis perfectly predictable:- Find two people with the same age and the same
college. Do they have the samehours? - Plot
hoursagainstAge, colored bycollege. What do you see?
- Find two people with the same age and the same
Try
rnorm(). To add a random part, we need random numbers.rnorm()makes them. Try each of these in the Console:Run
rnorm(5)three times. What changes each time? What do the numbers look like?Compare
rnorm(5, mean = 0, sd = 1)withrnorm(5, mean = 0, sd = 10). What doessdseem to do?Make 10,000 draws below and plot them in a histogram:
draws = tibble(draw = rnorm(10000, mean = 0, sd = 2))Where is the middle of the histogram? What share of the draws fall between \(-2\) and \(2\)? (Hint: use
case_when()to define if an observation falls between 2 and -2, then summarise.)
Noternorm()in plain wordsThink of each draw as a random nudge. Nudges up are as common as nudges down, small nudges are common, and big ones are rare.
sdsets how big a typical nudge is. We’ll cover the math behind this later in the quarter; for now, this is all you need.The random part. Give everyone their own nudge:
town = town |> mutate(hours_noisy = hours + rnorm(n(), mean = 0, sd = 2))(
n()is the number of rows, so each person gets one nudge.) Now redo part (c) withhours_noisy. Do two people with the same traits still have the same outcome? How does the plot change?In 3–4 sentences: what does the random part change about (1) how well you can predict the outcome, and (2) the data you get each time you run the process?
- Turn up the noise. For
sdvalues of 0, 1, 2, 5, and 10, for what share of people is the noisy version ofhourswithin 1 hour ofhoursitself?expand_grid()makes this easy: it makes one copy of the data for each value you give it.expand_grid(sd = c(0, 1, 2, 5, 10), town)stacks five copies oftown, 1,500 rows in all, with a columnsdthat says which value each copy gets. Give every row a nudge with its ownsd, usingrnorm(n(), mean = 0, sd = sd), thengroup_by(sd)to score each copy. Plot the share againstsd.