---
title: "Polling Memo Assignment"
subtitle: "POL 215: Statistical Methods of Political Science"
author: "Prof. Alauna Safarpour (Gettysburg College)"
output:
  pdf_document: default
  html_document: default
editor_options: 
  markdown: 
    wrap: sentence
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Introduction.

In June 2020, the AP and NORC conducted a nationwide survey on attitudes towards policing and criminal justice reform. Read the article here: <https://apnews.com/ffaa4bc564afcf4a90b02f455d8fdf03>

The code in this document will teach you how to reproduce the results in the article. The polling memo assignment asks you to use the data to write a follow up article exploring relationships you find interesting.

NOTE: This document shows you how to replicate the results of the article primarily by using the survey package in R. If you prefer to use the RCPA3 package instead, be sure to review the example code in this week's lecture script: "POL215_Polling_Memo_Lecture.R"

## Polling Memo Assignment:

For each of the following exercises, you should create either tables or figures with the results and after each table or figure, include a brief, professional write up of the results. If you are stuck on how to talk about your results, you should model your language after the article linked above, or similar polling articles. You will be graded based on your professionalism in creating a polished product (e.g., professional tables/figures and precise writing free from typos/errors.) Your memo should address the following questions:

1.  Examine support for policing reforms by party identification. Which reform shows the most sizable gap in attitudes by party? Create a figure that demonstrates the policy where gaps are the largest.
2.  Examine attitudes towards who is treated more fairly by police (POL5D) by education. What is the difference between individuals with a HS degree and those with a BA or above who say "White people are treated more fairly [by police]"? Create a figure to accompany your discussion of these results.
3.  Explore the codebook for variables that interest you, compute at least 2 weighted cross tabulations, and explain what the results indicate.
4.  Lastly, run an appropriate statistical test to evaluate whether ONE of the relationships you examined in question 3 is statistically significant, or not. The exact statistical test you run will depend on what you are comparing. You will be assessed based on whether the test is appropriate given the data, your accuracy at interpreting the results, and your professionalism in presenting the results.

## Additional Grading Criteria:

The memo should be a maximum of 6 pages double spaced (including text, tables, and figures). You should have at least 5 tables/figures but no more than 7 tables/figures total. Papers that are more than 6 pages double spaced, and do not contain between 5-7 figures/tables will be penalized.

All file submissions should be turned in as either Microsoft Word or PDF documents. At the end of your finished product beginning on a separate page, you should also copy and paste the R script that you used to produce your results as under the heading "Appendix: R script for reproducing results" at the end of the paper. The R script does not count against your page count and it should exactly replicate the results of your memo. This includes any re-codes you performed. If you use the re-code in the code above, be sure to include it in your replication code along with any packages necessary to run your code.

You should include a separate cover page which has a descriptive title for your memo, your name, the email/phone number you would give to a prospective employer, and the date. The title page should also include the following statement:

"The following is an abridged replication and extension of findings from Associated Press polling conducted in Summer 2020 following the death of George Floyd. It was completed as part of my course work in Political Science statistical methods at Gettysburg College and demonstrates my applied data analytics skills. For additional details on the original poll and article, see:" Then include a citation for the AP article linked above. Your citation should be in APSA Style. The cover page does not count against the page limit.

The pages of your memo should be numbered.

The best memo's will be provide a brief introduction/ background to the poll/ survey context, and be clear and self-contained (E.g., doesn't reference the questions by number, but instead flow naturally from one analysis to the next in a cohesive manner). Your memo should look like something you could provide to a potential employer during a job interview.

## Download Data and Codebook.

This assignment uses the June 2020 AP-NORC Center Poll available for download on the Roper Center's website.

Prior to running the below code (or completing the assignment), you will need to download the dataset from the Roper Center's iPoll database: <https://ropercenter.cornell.edu/ipoll/>

HINT: Try searching by the organization (The Associated Press-NORC Center for Public Affairs Research) and filter by the interview dates (e.g., 06/01/2020-07/01/2020).

## Load Packages and Data.

```{r}
rm(list = ls()) #Clean up.

options(scipen = 999) #turn off scientific notation.

#Load packages.
library(survey) #this is the package that lets us work with survey weights by creating survey objects
library(dplyr) #recodes, etc.
library(readstata13) #read in Stata 13 data and later.

#Set working directory- be sure to change the filepath to wherever you saved
#your data after downloading it from the Roper Center website.
setwd(
  "/Users/asafarpo/Dropbox/POL215/Fall2024/Polling Memo Materials/Polling Memo AP NORC 2020/")
  
#Read in Data.
d <-
  read.dta13(
    "31117488.DTA",
    nonint.factors=TRUE
  )
#Make sure your n matches what appears on Roper Center's website for this dataset!
```

##Applying Survey Weights. First, we will be applying survey weights using the "survey" package we loaded. R will create a weighted survey object which we will then use to compute cross tabulations. As you will see, the syntax for cross tabulations is different using our weighted survey object.

```{r}
d.w <- svydesign(ids = ~ 1,
                 data = d,
                 weights = d$finalwt)
```

##Percentage who say the criminal justice system needs.... Let's recreate the cross tabulations used in the first figure in the article. Remember, when reading R code with "nested" commands you read from the inside out.

```{r}
#Specify decimal places (we want to round to the nearest tenth).
options(digits = 1)
```

Cross Tab: All adults.

```{r}
addmargins(prop.table(svytable( ~POL6, design = d.w))) *
  100 
```

svytable() is the command to create a cross tab using the survey package and the weighted survey object we created earlier.

prop.table() says we want to compute the proportions (as opposed to raw frequencies).

addmargins() adds the totals ("Sum values).

\*100 multiplies the proportions by 100 so we get percentages rather than proportions.

Cross Tab: By Race.

```{r}
addmargins(prop.table(svytable( ~POL6+raceth, design = d.w),2)) *
  100 
```

Note that this time, we are computing percentages by race ("raceth"), which is added to the svytable() command. Also, in the prop.table() command, we specify prop.table( ,2) to tell R how we want the proportions calculated- by rows or by columns. ",2" specifies that we want each racial group to add up to 100%.

Cross Tab: By Party Affiliation.

```{r}
addmargins(prop.table(svytable( ~POL6+politics, design = d.w),2)) *
  100 
```

The numbers don't match what is shown in the article. Often, party id is re-coded by grouping Independents who report leaning towards a party with their respective party. There is no party id variable in the data that does this, so let's create it by recoding those Independent leaners with their respective party.

First, examine a cross tab of the politics and indep variables to see what we need to recode.

```{r}
table(d$politics, d$indep, exclude=NULL) 
```

Create a new variable called "pidwleaners", copying the variable "politics"

```{r}
d$pidwleaners<-d$politics 
```

Replace cases where the R was an independent who leaned Democrat as being a Democrat in the pidwleaners variable.

```{r}
d$pidwleaners[d$indep=="(1) Lean Democrat"]<-"(1) Democrat" 
```

Replace Republican-leaners as Republicans.

```{r}
d$pidwleaners[d$indep=="(2) Lean Republican"]<-"(2) Republican" 
```

Check our new variable against the "indep" variable to ensure our recoding worked properly.

```{r}
table(d$pidwleaners, d$indep, exclude=NULL) 
```

When you create a new variable you have to recreate the weighted survey object.

```{r}
d.w <- svydesign(ids = ~ 1,
                 data = d,
                 weights = d$finalwt)
```

Try the Cross Tab by Party ID again.

```{r}
addmargins(prop.table(svytable( ~POL6+pidwleaners, design = d.w),2)) *
  100 
```

Perfect- now the numbers match!

##Support for Policing Reforms. Now let's examine attitudes towards policing reforms. This will re-create the results in the second figure in the article.

Requiring Use of Body Cameras.

```{r}
addmargins(prop.table(svytable( ~POL7A, design = d.w))) *
  100 
```

Note that we remove the ",2" from the prop.table() command since we are no longer looking at the percentages conditional on some group.

Establishing clear standards for use of force.

```{r}
addmargins(prop.table(svytable( ~POL7H, design = d.w))) *
  100 
```

Requiring officers to report peer misconduct.

```{r}
addmargins(prop.table(svytable( ~POL7I, design = d.w))) *
  100 
```

Prosecuting officers who use excessive force.

```{r}
addmargins(prop.table(svytable( ~POL7B, design = d.w))) *
  100 
```

Penalizing officers for racially biased policing.

```{r}
addmargins(prop.table(svytable( ~POL7G, design = d.w))) *
  100 
```

Reducing focus on policing low-level offenses.

```{r}
addmargins(prop.table(svytable( ~POL7D, design = d.w))) *
  100 
```

Reducing funding for law enforcement.

```{r}
addmargins(prop.table(svytable( ~POL7E, design = d.w))) *
  100 
```

Polling Memo Assignment © 2020 by Alauna Safarpour is licensed under CC BY-NC 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc/4.0/