Week 2: Writing Code

Author

Joe Ornstein

This week we introduce the statistical software that we will use to work with data this semester (R and RStudio). By the end of this week, you will learn:

Reading

  • DAFSS Chapter 1

Problem Set

This week’s problem set is designed to ensure that you have R and RStudio properly installed on your computer, and that you can write code that loads data, describes it, and performs a few basic analyses.

We will analyze a dataset from an experiment conducted in Michigan in 2006 (Gerber, Green, and Larimer 2008). In that experiment, voters were randomly assigned to receive a postcard with a “social pressure” message, describing the recipient’s voting history and the voting history of their neighbors. The dataset we will use is called voting.csv. The following table shows the names and descriptions of the variables in the dataset.

variable description
birth year of birth of registered voter
message whether the registered voter was assigned to receive the social pressure message: “yes” or “no”
voted whether registered voter voted in the August 2006 election: 1=voted, 0=didn’t vote

Write an R script that performs the following analyses, in order. You can use comments in your script to respond to questions.

  1. Download the voting.csv file (note: you can right click that link to save the file to your computer) and put it into your DSS folder. Read the dataset into R and save it as an object called voting.

  2. Use the function head() to view the first few observations of the dataset.

  3. How many observations are there in total? What is the unit of analysis?

  4. What is the average value of the variable birth? Interpret this number.

  5. What is the average value of the variable voted? Interpret this number.

  6. Bonus. Install the tinytex package by entering the following lines of code into the R console.1

    install.packages('tinytex')
    tinytex::install_tinytex()

    Once this is installed, you can compile your R script and its output as a PDF by clicking “Compile Report” in RStudio. Submit this PDF report to eLC.

Additional Resources

References

Gerber, Alan S., Donald P. Green, and Christopher W. Larimer. 2008. “Social Pressure and Voter Turnout: Evidence from a Large-Scale Field Experiment.” American Political Science Review 102 (1): 33–48. https://doi.org/10.1017/S000305540808009X.

Footnotes

  1. Not the R script! If you install a package in the script then it will reinstall the package every time you run the script, which is unnecessary and time-consuming.↩︎