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/ using ggsave() and here()
  • Save any cleaned data in data/clean/. Never overwrite anything in data/
  • 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:

library(tidyverse)
library(juanr)
library(here)
  1. An average per year. leader has 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?

    1. Some years come back NA. How many leader-years are missing age? (Use is.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.

    2. 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?

  2. 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 (party is D, R, or I).

    1. Which state-party combination raised the most? Use arrange() or slice_max().

    2. 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.

  3. 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)?

    1. 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?

    2. 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.

  4. A gap between groups. Also in bot: how big is the gender gap between men and women (sex) in support for abortion rights (prochoice)?

    1. Cross-tabulate sex and prochoice, then turn the counts into percents within each sex. Report the gap in support as a number of percentage points.

    2. 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.

  5. Dummies are proportions. wealth is 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)?

    1. 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.
  6. 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 to figures/. In a comment, say what every percent or average in your answer is a percent or average of.