Statistics and Statistical Programming (Fall 2020)/pset3: Difference between revisions

From CommunityData
No edit summary
No edit summary
Line 1: Line 1:
<small>[[Statistics_and_Statistical_Programming_(Fall_2020)#Week_5_.2810.2F13.2C_10.2F15.29|← Back to Week 5]]</small>
<small>[[Statistics_and_Statistical_Programming_(Fall_2020)#Week_5_.2810.2F13.2C_10.2F15.29|← Back to Week 5]]</small>


For this problem set, the programming challenges investigate the relationship between vehicle searches and driver attributes (especially race as recorded by police officers conducting traffic stops) in Illinois. Doing so will involve some more advanced data wrangling, visualization, and analysis. We'll use data from [https://openpolicing.stanford.edu The Stanford Open Policing Project] (SOPP) that looks at records of traffic stops in Illinois between 2012-2017. The full SOPP dataset for Illinois is about 12 million rows, so I've created a 1% random sample for us to work with here. Overall, the dataset is well-documented and pretty "clean," but there are still a number of features that may be confusing, weird, and/or ill-organized to help answer the questions I've asked you below. Luckily, you know how to use R to solve these problems...
Do police in the United States engage in discriminatory behavior on the basis of race and ethnicity? For this problem set, the programming challenges investigate the relationship between traffic stops, vehicle searches and driver attributes (especially race as recorded by police officers conducting traffic stops) in Illinois. Doing so will involve some more advanced data wrangling, visualization, and analysis. We'll use data from [https://openpolicing.stanford.edu The Stanford Open Policing Project] (SOPP) that looks at records of traffic stops in Illinois between 2012-2017. The full SOPP dataset for Illinois is about 12 million rows, so I've created a 1% random sample for us to work with here. Overall, the dataset is well-documented and pretty "clean," but there are still a number of features that may be confusing, weird, and/or ill-organized to help answer the questions I've asked you below. Thank goodness you know how to use R to address these issues...


== Programming Challenges ==
== Programming Challenges ==


=== PC1. Investigate the provenance of the data ===
=== PC1. Learn about the data ===


Review the project description on the SOPP website, the codebook provided for the project as a whole as well as for the Illinois data specifically, as well as any ancillary materials that help you get oriented with the data. For the questions below we'll focus on the following measures recorded for each stop: `date`, `vehicle_year`, `subject_race`, `subject_sex`, and  `search_conducted`. Note any questions or issues you might notice related to these measures as you review the information about the project and dataset.
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: `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.


=== PC2. Import, explore, clean ===
=== PC2. Import, explore, clean ===


As I noted 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. That subset lives here. 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`). What data (if any) is missing? Inspect a random sample of rows to get a sense of the data. 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 (`search_conducted`) and each of the predictor variables (`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.
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 ===
 
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 ===
 
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 within each `subject_race` 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 `subject_race` category each month.


=== PC4. Summarize relationships between outcome and predictor variables ===
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)'').
# 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.


=== PC5. Analyze relationships between driver race and vehicle searches over time ===
=== PC6. Calculate baseline population proportions for relevant race/ethnicity categories ===


=== PC6. Estimate population baselines for relevant racial 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 `openintro` library!


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 ==


=== SQ1. Interpret the results of PC3 ===
=== SQ1. Interpret the Illinois traffic stop analysis (PCs 3-5) ===
=== SQ2. Interpret the results of PC4 ===
=== SQ3. Interpret the results of PC5 ===
=== SQ4. Compare and interpret salient results of PC4 and PC6 ===
=== SQ5. Reflect on the limitations of your analysis ===


==OLD ==
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?
PC3. Using the gov domains data, create a new data frame where one column is each month (as described in the data) and a second column is the total number of views made to all pages in the dataset over that month.


PC4. Using the mobile data, create a new data frame where one column is each month described in the data and the second is a measure (estimate?) of the total number of views made by mobile devices (all platforms) over each month. This will involve at least two steps since total views are not included. You'll need to first use the data there to create a measure of the total views for each line in the dataset.
=== SQ2. Contextualize traffic stop data in relation to population data ===


PC5. Merge your two datasets together into a new dataset with columns for each month, total views (across the gov domain pages) and total mobile views. Make sure that month, in your merged dataset, is a date or datetime object in R. Are there are missing data? Can you tell why?
Consider the results of PCs 3-5 in relation to the results of PC 6. Do the results of PC 6 impact your interpretation of PCs 3-5 in any way? How do you relate the results of the traffic stop and vehicle search analysis to the population baseline proportions?  


PC6. Create a new column in your merged dataset that describes your best estimate of the proportion of total views that come from mobile. Be able to talk about any assumptions/decisions you've made in constructing this measure.
=== SQ3. Reflect on the limitations and possible extensions of your analysis ===


PC7. Graph the proportion over time and be ready to describe: (a) your best estimate of the proportion of views from mobile devices to the Seattle City website over time and (b) an indication of whether it's going up or down.
Identify and briefly explain key limitations of the data, your analysis, and the results in relation to the questions and concepts that motivated the exercise. Where possible, what additional information/data or analysis might you suggest in order to overcome these limitations and answer the questions in a more comprehensive or convincing fashion?

Revision as of 15:34, 6 October 2020

← Back to Week 5

Do police in the United States engage in discriminatory behavior on the basis of race and ethnicity? For this problem set, the programming challenges investigate the relationship between traffic stops, vehicle searches and driver attributes (especially race as recorded by police officers conducting traffic stops) in Illinois. Doing so will involve some more advanced data wrangling, visualization, and analysis. We'll use data from The Stanford Open Policing Project (SOPP) that looks at records of traffic stops in Illinois between 2012-2017. The full SOPP dataset for Illinois is about 12 million rows, so I've created a 1% random sample for us to work with here. Overall, the dataset is well-documented and pretty "clean," but there are still a number of features that may be confusing, weird, and/or ill-organized to help answer the questions I've asked you below. Thank goodness you know how to use R to address these issues...

Programming Challenges

PC1. Learn about the data

Review the project overview on the SOPP homepage, the overview of the data, the description of the standardized data, the codebook/notes for the Illinois data from the 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: `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.

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

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

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

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:

  1. A plot of the number of stops and searches across the entire sample within each month.
  2. A plot of the number of stops within each `subject_race` category each month.
  3. A plot of the number of searches within each `subject_race` category each month.
  4. 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:

  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: (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)).
  2. Use `ggplot2` and the `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.

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 `openintro` library!

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

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 `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

Consider the results of PCs 3-5 in relation to the results of PC 6. Do the results of PC 6 impact your interpretation of PCs 3-5 in any way? How do you relate the results of the traffic stop and vehicle search analysis to the population baseline proportions?

SQ3. Reflect on the limitations and possible extensions of your analysis

Identify and briefly explain key limitations of the data, your analysis, and the results in relation to the questions and concepts that motivated the exercise. Where possible, what additional information/data or analysis might you suggest in order to overcome these limitations and answer the questions in a more comprehensive or convincing fashion?