Editing Statistics and Statistical Programming (Winter 2021)/Problem set 7

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 17: Line 17:
=== PC2. Learn about the Open Policing data ===
=== PC2. Learn about the Open Policing 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-wa codebook/notes for the Washington 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 Washington 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 Washington 2009-2018: <code>date</code>, <code>subject_age</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 Washington 2009-2018: <code>date</code>, <code>subject_age</code>, <code>subject_race</code>, <code>subject_sex</code>, and  <code>search_conducted</code>.  
Line 33: Line 33:
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>subject_age</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 (<code>search_conducted</code>) and each of the predictor variables we care about (<code>date</code>, <code>subject_age</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.


=== PC4. Summarize conditional 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>subject_age</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 (<code>date</code>, <code>subject_age</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>).
Line 39: Line 39:
=== 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 a good goal should be to should be to produce the following:
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:
 
# A plot of the number of stops and searches across the entire sample within each month.
# A plot of the number of stops across the entire sample within each month
# A plot of the number of 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 <code>subject_race</code> 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 <code>subject_race</code> 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 <code>subject_race</code> category each month.


You'll need to build a dataset. My suggestion is draw out the dataset you want to build. What are the rows? What are the columns?
Here's a suggestion for how you might approach this:
 
Here's one suggestion for how you might approach this:


1. Create a new data frame that aggregates stop and search data across sub-groups of <code>subject_race</code> per month. This object could include the following columns:  
1. Create a new data frame that aggregates stop and search data across sub-groups of <code>subject_race</code> per month. This object could include the following columns:  
* date as a month/year (should be a date or date-time object which will require a day. I set all of them just to "YYYY-MM-01" by rounding them down)
* date as a month/year (should be a date or date-time object)
* race/ethnicity (from the <code>subject_race</code> variable)
* 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 stops (within the <code>subject_race</code> group identified for the row)
Line 58: Line 54:
* total number of searches that month/year
* total number of searches that month/year
* proportion of total searches (within the <code>subject_race</code> group identified for the row).  
* 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).''
''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).''
If your dataset is wide, you will need to turn it into a long dataset for graphing.


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