Lab #05: Probability and NC Courage

Goals

Merge Conflicts (uh oh)

Before you begin, it’s important to know about merge conflicts.

Merge conflicts occur when two or more people are working on the same file at the same time.

When two collaborators make changes to the same file and push the file to their repository, git merges the two files.

If these two files have conflicting content on the same line, git will produce a merge conflict. Merge conflicts need to be resolved manually, as they require a human intervention:

To resolve the merge conflict, decide if you want to keep only your text, the text on GitHub, or incorporate changes from both texts. Delete the conflict markers <<<<<<<, =======, >>>>>>> and make the changes you want in the final merge.

Ultimately, the goal is to avoid merge conflicts. One way to do this is to ensure only one person is typing in the team’s R Markdown document at any given time.

Click here to see the FAQ with step-by-step instructions for troubleshooting a merge conflict.

Getting started

Workflow: Using git and GitHub as a team

Update YAML

Team Member 1: Change the author to your team name and include each team member’s name in the author field of the YAML in the following format. Team Name: Member 1, Member 2, Member 3, Member 4, (Group Member 5). Knit, commit, and push the changes to GitHub.

Team Members 2, 3, 4, 5: Click the Pull** button in the Git pane to get the updated document. You should see the updated name in the .Rmd file.**

Packages

We will use the tidyverse and knitr packages in this lab. You can also use the viridis for the visualizations.

library(tidyverse)
library(knitr)

Data

Today, we will be working with data from the first three full seasons of the NC Courage, a highly successful National Women’s Soccer League (NWSL) team located near Duke in Cary, NC. The Courage moved to the Triangle from Western New York in 2017 and had three very successful first seasons, culminating in winning the championship game that was held at their stadium in Cary in 2019! (Data for this lab was sourced from the nwslR package on Github, and verified with the NC Courage website by Meredith Brown in a previous semester.)

Use the code below to load the data set.

courage <- read_csv("courage.csv")
courage 
## # A tibble: 78 × 10
##    game_id  game_date game_number home_team away_team opponent home_pts away_pts
##    <chr>    <chr>           <dbl> <chr>     <chr>     <chr>       <dbl>    <dbl>
##  1 washing… 4/15/2017           1 WAS       NC        WAS             0        1
##  2 north-c… 4/22/2017           2 NC        POR       POR             1        0
##  3 north-c… 4/29/2017           3 NC        ORL       ORL             3        1
##  4 boston-… 5/7/2017            4 BOS       NC        BOS             0        1
##  5 orlando… 5/14/2017           5 ORL       NC        ORL             3        1
##  6 north-c… 5/21/2017           6 NC        CHI       CHI             1        3
##  7 north-c… 5/24/2017           7 NC        NJ        NJ              2        0
##  8 chicago… 5/27/2017           8 CHI       NC        CHI             3        2
##  9 north-c… 6/3/2017            9 NC        KC        KC              2        0
## 10 north-c… 6/17/2017          10 NC        BOS       BOS             3        1
## # … with 68 more rows, and 2 more variables: result <chr>, season <dbl>

Exercises

For each exercise, show all relevant code and output used to obtain your response. Write all narrative in complete sentences, and use clear axis labels and titles on visualizations.

Team Member 1: If you haven’t already, change the author to your team name and include each team member’s name in the author field of the YAML in the following format. Team Name: Team member 1, Team member 2, Team member 3, Team member 4.

Team member 1: Type the team’s response to Exercises 1 and 2.

  1. How many observations are in this dataset? What does each observation represent? (You do not need to create a code chunk here)

  2. Each season the Courage play 26 games. We want to find out whether they win more in the early, middle or late season.

Team member 1: Knit, commit and push your changes to GitHub with an informative commit message. Make sure to commit and push all changed files so that your Git pane is empty afterwards.

All other team members: Pull to get the updated documents from GitHub. Click on the .Rmd file, and you should see the responses to exercises 1-3.

Team Member 2: It’s your turn! Type the team’s response to exercises 3 - 4. :::

  1. By default, R will arrange the categories of a categorical variable in alphabetical order in any output and visualizations, but we want the levels for seasonal_category to be in logical order. To achieve this, we will use the factor() function to make both of these variables factors (categorical variables with ordering) and specify the levels we wish to use.

The code to reorder levels for seasonal_category is below.

# seasonal_courage %>%
#   mutate(seasonal_category =
#            factor(seasonal_category,
#                   levels = c("early", "middle", "late"),
#                   ordered = TRUE))
  1. Based on the data,

Team member 2: Knit, commit and push your changes to GitHub with an informative commit message. Make sure to commit and push all changed files so that your Git pane is empty afterwards.

All other team members: Pull to get the updated documents from GitHub. Click on the .Rmd file, and you should see the responses to exercises 3 - 4.

Team Member 3: It’s your turn! Type the team’s response to exercise 5. :::

  1. Independence, contingency tables and ties.

Using the data frame above, create a 3 x 3 contingency table (dimensions are including column/row names) with - columns denoting whether or not a game is home or away for the Courage and - rows denoting whether the Courage win, lose or tie.

Bayes’ theorem tells us that

\[ P(\text{tie} \ | \ \text{home}) = \frac{P(\text{home} \ | \ \text{tie}) P(\text{tie})}{P(\text{home})} \]

Team member 3: Knit, commit and push your changes to GitHub with an informative commit message. Make sure to commit and push all changed files so that your Git pane is empty afterwards.

All other team members: Pull to get the updated documents from GitHub. Click on the .Rmd file, and you should see the responses to exercises 3 - 4.

Team Member 4: It’s your turn! Type the team’s response to exercises 6 and 7. :::

  1. Building on the data frame from the previous exercise, create 3 new columns

Plot - Create a scatter plot of opponent_pts (y) vs courage_pts (x). - Color the scatter plot by whether the Courage are home or away. - Add geom_jitter as well as the line \(y = x\) using the code below. - Facet your plot by season - Be sure to include informative titles for the plot, axes and legend. What do you notice?

# ggplot(...code here...) + 
# geom_jitter(width = 0.1, height = 0.1) + 
# geom_abline(slope=1, intercept=0)

Team member 4: Knit, commit and push your changes to GitHub with an informative commit message. Make sure to commit and push all changed files so that your Git pane is empty afterwards.

All other team members: Pull to get the updated documents from GitHub. Click on the .Rmd file, and you should see the team’s completed lab! :::

Wrapping up

Go back through your write up to make sure you followed the coding style guidelines we discussed in class (e.g. no long lines of code).

Submission

There should only be one submission per team on Gradescope.

Grading

Component Points
Ex 1 2
Ex 2 8
Ex 3 3
Ex 4 8
Ex 5 12
Ex 6 11
Workflow & formatting 6