Editing Statistics and Statistical Programming (Fall 2020)/pset3

From CommunityData
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 9: Line 9:
Review the project overview on the [https://openpolicing.stanford.edu/ SOPP homepage], the [https://openpolicing.stanford.edu/data/ overview of the data], the [https://github.com/stanford-policylab/opp/blob/master/data_readme.md#description-of-standardized-data description of the standardized data], the [https://github.com/stanford-policylab/opp/blob/master/data_readme.md#statewide-il codebook/notes for the Illinois data] from the [https://github.com/stanford-policylab/opp/blob/master/data_readme.md data_readme.md], as well as any other ancillary materials that you can find that seem likely to help you get oriented with the data.  
Review the project overview on the [https://openpolicing.stanford.edu/ SOPP homepage], the [https://openpolicing.stanford.edu/data/ overview of the data], the [https://github.com/stanford-policylab/opp/blob/master/data_readme.md#description-of-standardized-data description of the standardized data], the [https://github.com/stanford-policylab/opp/blob/master/data_readme.md#statewide-il codebook/notes for the Illinois data] from the [https://github.com/stanford-policylab/opp/blob/master/data_readme.md data_readme.md], as well as any other ancillary materials that you can find that seem likely to help you get oriented with the data.  


For the questions below we'll focus on the following measures recorded for each traffic stop in Illinois 2012-2017: <code>date</code>, <code>vehicle_year</code>, <code>subject_race</code>, <code>subject_sex</code>, and  <code>search_conducted</code>.  
For the questions below we'll focus on the following measures recorded for each traffic stop in Illinois 2012-2017: `date`, `vehicle_year`, `subject_race`, `subject_sex`, and  `search_conducted`.  


Record any questions or issues you might notice related to these measures as you review the information about the project and dataset.
Record any questions or issues you might notice related to these measures as you review the information about the project and dataset.
Line 15: Line 15:
=== PC2. Import, explore, clean ===
=== PC2. Import, explore, clean ===


As I mentioned above, the full IL SOPP dataset is over 12 million rows, so I have created a random 1% subset for us to work with in this assignment. [https://communitydata.science/~ads/teaching/2020/stats/data/week_05 That subset lives here] (and it's about 26MB). To get started, you'll want to import the data and explore its structure as well as key variables that we'll be focusing on in this analysis (<code>date</code>, <code>vehicle_year</code>, <code>subject_race</code>, <code>subject_sex</code>, and  <code>search_conducted</code>). Inspect a random sample of rows to get a sense of the data. What (if anything) is missing? You may also want to clean/recode some of the key variables. Make sure to explain and justify any data cleanup and/or recoding steps you decide to take.
As I mentioned above, the full IL SOPP dataset is over 12 million rows, so I have created a random 1% subset for us to work with in this assignment. [https://communitydata.science/~ads/teaching/2020/stats/data/week_05 That subset lives here] (and it's about 26MB). To get started, you'll want to import the data and explore its structure as well as key variables that we'll be focusing on in this analysis (`date`, `vehicle_year`, `subject_race`, `subject_sex`, and  `search_conducted`). Inspect a random sample of rows to get a sense of the data. What (if anything) is missing? You may also want to clean/recode some of the key variables. Make sure to explain and justify any data cleanup and/or recoding steps you decide to take.


=== PC3. Summarize outcome and predictor variables ===
=== PC3. Summarize outcome and predictor variables ===


Calculate and report appropriate summary statistics for the outcome (<code>search_conducted</code>) and each of the predictor variables we care about (<code>date</code>, <code>vehicle_year</code>, <code>subject_race</code>, and <code>subject_sex</code>). Include visual and/or tabular summaries where appropriate. Attempt, when possible, to write efficient/elegant code that avoids unnecessary repetition while also retaining clarity.
Calculate and report appropriate summary statistics for the outcome (`search_conducted`) and each of the predictor variables we care about (`date`, `vehicle_year`, `subject_race`, `subject_sex`). Include visual and/or tabular summaries where appropriate. Attempt, when possible, to write efficient/elegant code that avoids unnecessary repetition while also retaining clarity.


=== PC4. Summarize aggregate relationships between outcome and predictor variables ===
=== PC4. Summarize aggregate relationships between outcome and predictor variables ===


The outcome variable we care about here is a dichotomous indicator for whether each traffic stop resulted in a police search (of either the driver or the vehicle) being conducted. Summarize the relationship between each of the predictor variables (<code>date</code>, <code>vehicle_year</code>, <code>subject_race</code>, and <code>subject_sex</code>) and the outcome variable (<code>search_conducted</code>). For continuous predictors, be sure to include visual summaries. For categorical predictors, focus on providing cross-tabulations that report conditional summary statistics within groups (for example, compare the numbers of searches conducted across the two categories of <code>subject_sex</code>).
The outcome variable we care about here is a dichotomous indicator for whether each traffic stop resulted in a police search (of either the driver or the vehicle) being conducted. Summarize the relationship between each of the predictor variables (`date`, `vehicle_year`, `subject_race`, and `subject_sex`) and the outcome variable. For continuous predictors, be sure to include visual summaries. For categorical predictors, focus on providing cross-tabulations that report conditional summary statistics within groups (for example, compare the numbers of searches conducted across the two categories of `subject_sex`).


=== PC5. Analyze relationships between driver race/ethnicity and vehicle searches over time ===
=== PC5. Analyze relationships between driver race/ethnicity and vehicle searches over time ===


Summarize the relationship between the recorded <code>subject_race</code> for each traffic stop and the <code>search_conducted</code> outcome over the time period covered by the dataset. You may do this in a variety of ways, but at minimum, you should produce the following:
Summarize the relationship between the recorded `subject_race` for each traffic stop and the `search_conducted` outcome over the time period covered by the dataset. You may do this in a variety of ways, but at minimum, you should produce the following:
# A plot of the number of stops and searches across the entire sample within each month.
# A plot of the number of stops and searches across the entire sample within each month.
# A plot of the number of stops within each <code>subject_race</code> category each month.
# A plot of the number of stops within each `subject_race` category each month.
# A plot of the number of searches within each <code>subject_race</code> category each month.
# A plot of the number of searches within each `subject_race` category each month.
# A plot of the proportion all searches accounted for within each <code>subject_race</code> category each month.
# A plot of the proportion all searches accounted for within each `subject_race` category each month.


Here's a suggestion for how you might approach this:
Here's a suggestion for how you might approach this:
 
# Create a new data frame (or tibble) that aggregates stop and search data across sub-groups of `subject_race` per month. This object could include the following columns: (a) date as a month/year (should be a date or date-time object); (b) race/ethnicity (from the `subject_race` variable); (c) number of stops (within the group identified in column b); (c) number of searches (within the group identified in column b); (d) total number of searches that month/year; (e) proportion of total searches (within the group identified in column b). (''Note that this will result in a data frame with multiple rows per month/year (as many as one row for each `subject_race` category)'').
1. Create a new data frame (or tibble) that aggregates stop and search data across sub-groups of `subject_race` per month. This object could include the following columns:  
# Use `ggplot2` and the [https://ggplot2.tidyverse.org/reference/geom_path.html `geom_line`] layer to generate each of the plots. Note that you'll want to assign `subject_race` as an aesthetic element (`aes`) for some of the plots so that ggplot2 represents each category as a separate line (maybe distinguished by color?). Make sure to incorporate useful titles, axis labels, and legends for each plot you produce.
* date as a month/year (should be a date or date-time object)
* race/ethnicity (from the <code>subject_race</code> variable)
* number of stops (within the <code>subject_race</code> group identified for the row)
* number of searches (within the <code>subject_race</code> group identified for the row)
* total number of searches that month/year
* proportion of total searches (within the <code>subject_race</code> group identified for the row).  
''Note that this will result in a data frame with multiple rows per month/year (as many as one row for each <code>subject_race</code> category).''
 
2. Use <code>ggplot2</code> and the [https://ggplot2.tidyverse.org/reference/geom_path.html <code>geom_line</code>] layer to generate each of the plots. Note that you'll want to assign <code>subject_race</code> as an aesthetic element (<code>aes</code>) for some of the plots so that ggplot2 represents each category as a separate line (maybe distinguished by color?). Make sure to incorporate useful titles, axis labels, and legends for each plot you produce. Recall that the R tutorials include examples of using <code>aes</code> with <code>ggplot2</code>.


=== PC6. Calculate baseline population proportions for relevant race/ethnicity categories ===
=== PC6. Calculate baseline population proportions for relevant race/ethnicity categories ===


To help interpret the results of the foregoing analysis of the traffic stop data, we should calculate some baseline population proportions of the same race/ethnicity categories in the state of Illinois around the same time that the SOPP data comes from. Luckily, we have access to exactly the data we need to do this via our old friend, the <code>openintro</code> library!  
To help interpret the results of the foregoing analysis of the traffic stop data, we should calculate some baseline population proportions of the same race/ethnicity categories in the state of Illinois around the same time that the SOPP data comes from. Luckily, we have access to exactly the data we need to do this via our old friend, the `openintro` library!  


Use the <code>county_complete</code> dataset from the <code>openintro</code> library to calculate the proportions of the Illinois population in 2010 in each of the categories identified in the <code>subject_race</code> variable of the traffic stop dataset. You will want to use the [https://www.openintro.org/data/index.php?data=county_complete codebook] for the dataset to identify the variables you'll need. You will also need to wrangle the data a bit to produce the proportions we're looking for. Be sure to note and justify any assumptions and/or recoding decisions that you make along the way.
Use the `county_complete` dataset from the `openintro` library to calculate the proportions of the Illinois population in 2010 in each of the categories identified in the `subject_race` variable of the traffic stop dataset. Be sure to note and justify any assumptions and/or recoding decisions that you make along the way.


== Statistical Questions ==
== Statistical Questions ==
Line 56: Line 47:
=== SQ1. Interpret the Illinois traffic stop analysis (PCs 3-5) ===
=== SQ1. Interpret the Illinois traffic stop analysis (PCs 3-5) ===


Return to the questions that motivated this analysis. Based on your results from PCs 3-5, what patterns do you observe in vehicle searches among Illinois drivers between 2012-2017? Specifically, what patterns do you note from your comparisons of stops and searches across the categories identified in the <code>subject_race</code> variable? What patterns do you observe when you compare across groups in aggregate (i.e., summed across the entire dataset)? What patterns do you observe in terms of either the numbers or proportions of stops and searches when you compare across groups over time?
Return to the questions that motivated this analysis. Based on your results from PCs 3-5, what patterns do you observe in vehicle searches among Illinois drivers between 2012-2017? Specifically, what patterns do you note from your comparisons of stops and searches across the categories identified in the `subject_race` variable? What patterns do you observe when you compare across groups in aggregate (i.e., summed across the entire dataset)? What patterns do you observe in terms of either the numbers or proportions of stops and searches when you compare across groups over time?


=== SQ2. Contextualize traffic stop data in relation to population data ===
=== SQ2. Contextualize traffic stop data in relation to population data ===
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see CommunityData:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)