library(tidyverse)
library(juanr)
library(here)Problem set: relationships in data
Overview
Inside your camp project, write a script called code/04_set.R that answers the questions below. It is your script: for any line in it, you should be able to say what it does. New today: for every summary, say how many rows were missing and whether they are in the denominator. For every percent, say what it is a percent of.
For all of your questions, do the following:
- Make the graphs look good (add labels, a theme)
- Make your code look good (comment it, write it in chunks)
- Save each of your plots in
figures/usingggsave()andhere() - Save any cleaned data in
data/clean/. Never overwrite anything indata/ - When you think you’re done: restart R and run the script top to bottom, from a fresh session. If it works then, it works
Nothing is turned in. Your figures/ and data/clean/ folders are the record of what you did.
Questions
You’ll need the following packages:
An average per year.
leaderhas one row per leader per year. Calculate the average age of a world leader in each year, and plot it as a time series. What is the trend?Some years come back
NA. How many leader-years are missingage? (Useis.na()and add up the missing cells.) Fix the summary so every year has a number, and say in a comment what the fix does to the denominator.Ask your agent to add the number of leaders behind each yearly average, and to plot how that number changes over time. Read the
summarize()it wrote: which function counts the rows? Which years rest on the fewest leaders, and do you trust those averages?
Grouped totals. Using
bonica: where did Democrats and Republicans raise the most money in the 2016 election cycle? Calculate the sum (sum()) of all money raised (total_receipts) in 2016, broken down by state and party (partyisD,R, orI).Which state-party combination raised the most? Use
arrange()orslice_max().Ask your agent for the top three states for each party. Read its code: does it
group_by(party)before it takes the top three? Check that you got three rows per party, not three rows total.
From counts to percents. In
bot, what percent of Americans support “increasing spending on border security by $25 billion, including building a wall between the U.S. and Mexico” (wall)?Run
count(bot, wall)first. There is a missing category. Compute the percent twice, once with the missing rows in the denominator and once with them dropped (drop_na()). Which number would you report, and why?Now compute support for the wall by party (
pid7). Each party’s percents should sum to 100. Say in a comment which function makes that happen.
A gap between groups. Also in
bot: how big is the gender gap between men and women (sex) in support for abortion rights (prochoice)?Cross-tabulate
sexandprochoice, then turn the counts into percents within each sex. Report the gap in support as a number of percentage points.Ask your agent to compute the same gap. Read its code: is its percent out of all respondents, or within each sex? Only one of those is a gender gap.
Dummies are proportions.
wealthis a survey of households in Honduras. The columns are coded; the codebook is in?wealth. Among urban households (ur), about how many percentage points more likely is a household to own a television (r1) than a washing machine (r6)?- Both columns are 0/1 dummies, so their averages are proportions. Before you trust them, count the missing values in each column (
is.na()). Does either one affect your urban answer? Say how you know.
- Both columns are 0/1 dummies, so their averages are proportions. Before you trust them, count the missing values in each column (
Your own question. Pick another dataset in
juanr, and come up with a question that compares groups. Answer it with a graph, and save the graph tofigures/. In a comment, say what every percent or average in your answer is a percent or average of.