Lab 2

NC Counties…again!

Lab
Due: End of lab on Thurs, Jan 22

Introduction

Today you will double down on your data visualization skills, and you will also start playing with the pipe operator |>.

Learning objectives

  • Gain practice with
    • the data science workflow using R, RStudio, Git, and GitHub,
    • writing a reproducible report using Quarto, and
    • version control using Git and GitHub.
  • Create data visualizations with the tidyverse, specifically using ggplot2.
  • Describe in words what those visualizations teach us.
  • Write a simple data transformation pipeline with the tidyverse, specifically using dplyr.

Getting started

To get started, follow the instructions below.

  • Go to https://cmgr.oit.duke.edu/containers and login with your Duke NetID and Password.
  • Click STA199 under My reservations to log into your container. You should now see the RStudio environment.
  • Go to the course organization at github.com/sta199-s26 organization on GitHub. Click on the repo with the prefix lab-2. It contains the starter documents you need to complete the homework.
  • Click on the green CODE button, select Use SSH. Click on the clipboard icon to copy the repo URL.
  • In RStudio, go to FileNew ProjectVersion ControlGit.
  • Copy and paste the URL of your assignment repo into the dialog box Repository URL. Again, please make sure to have SSH highlighted under Clone when you copy the address.
  • Click Create Project, and the files from your GitHub repo will be displayed in the Files pane in RStudio.

Then, click lab-2.qmd to open the template Quarto file and update the authors field to add your name first (first and last) and then your teammates’ names (first and last). Render the document. If you get a popup window error, click “Try again”. Examine the rendered document and make sure your name is updated in the document. Commit and push your changes with a meaningful commit message and push to GitHub.

Guidelines

Code

Code should follow the tidyverse style. Particularly,

  • there should be spaces before and line breaks after each + when building a ggplot,
  • there should also be spaces before and line breaks after each |> in a data transformation pipeline,
  • code should be properly indented,
  • there should be spaces around = signs and spaces after commas.

Additionally, all code should be visible in the PDF output, i.e., should not run off the page on the PDF. Long lines that run off the page should be split across multiple lines with line breaks.1

Plots

  • Plots should have an informative title and, if needed, also a subtitle.
  • Axes and legends should be labeled with both the variable name and its units (if applicable).
  • Careful consideration should be given to aesthetic choices.

Workflow

Continuing to develop a sound workflow for reproducible data analysis is important as you complete the lab and other assignments in this course.

  • You should have at least 3 commits with meaningful commit messages by the end of the assignment.
  • Final versions of both your .qmd file and the rendered PDF should be pushed to GitHub.

Packages

In this lab we will work with the tidyverse package, which is a collection of packages for doing data analysis in a “tidy” way.

  • Run the code cell by clicking on the green triangle (play) button for the code cell labeled load-packages. This loads the package to make its features (the functions and datasets in it) be accessible from your Console.
  • Then, render the document which loads this package to make its features (the functions and datasets in it) be available for other code cells in your Quarto document.

Refresher: tidyverse

The tidyverse is a meta-package. When you load it you get nine packages loaded for you:

  • dplyr: for data wrangling
  • forcats: for dealing with factors
  • ggplot2: for data visualization
  • lubridate: for dealing with dates
  • purrr: for iteration with functional programming
  • readr: for reading and writing data
  • stringr: for string manipulation
  • tibble: for modern, tidy data frames
  • tidyr: for data tidying and rectangling

Data

For this lab you will use a dataset on counties in North Carolina.

The dataset contains information on North Carolina counties retrieved from the 2020 Census as well as from myFutureNC Dashboard maintained by Carolina Demography at the University of North Carolina at Chapel Hill.

This dataset is stored in a file called nc-county.csv in the data folder of your project/repository.

You can read this file into R with the following code:

nc_county <- read_csv("data/nc-county.csv")

This will read the CSV (comma separated values) file from the data folder and store the dataset as a data frame called nc_county in R.

The variables in the dataset and their descriptions are as follows:

  • county: Name of county.
  • land_area_m2: Land area of county in meters-squared, based on the 2020 census.
  • land_area_mi2: Land area of county in miles-squared, based on the 2020 census.
  • pop_2020: Population of county, based on the 2020 Census.
  • pop_dens_2020: Population density calculated as population (pop_2020) divided by land area in miles-squared (people per mile-squared).
  • county_type: Peer county type classification based on population characteristics, socioeconomic status, and geographic features used for grouping counties with similar demographic, social, and economic characteristics, allowing them to be compared and benchmarked against one another.
  • median_hh_income: Median household income.
  • p_foreign_born: Percentage of population that is foreign-born.
  • p_child_poverty: Percentage of children living in poverty.
  • p_single_parent_hh: Percentage of households with children that are single-parent households.
  • p_broadband: Percentage of households with broadband internet access.
  • p_home_ownership: Percentage of households that are owner-occupied.
  • p_family_sustaining_wage: Percentage of adults that earn a family-sustaining wage – typically a wage that covers essential costs like housing, food, childcare, transportation, and healthcare for a family’s basic needs within a specific geographic area
  • p_edu_lths: Percentage of 25-44-year-olds with less than a high school diploma.
  • p_edu_hsged: Percentage of 25-44-year-olds with a high school diploma or equivalent.
  • p_edu_scnd: Percentage of 25-44-year-olds with some college or an associate degree.
  • p_edu_ndc: Percentage of 25-44-year-olds with non-degree credentials – certifications, licenses, or other credentials that demonstrate specific skills or knowledge but do not confer a formal academic degree.
  • p_edu_assoc: Percentage of 25-44-year-olds with an associate degree.
  • p_edu_ba: Percentage of 25-44-year-olds with a bachelor’s degree.
  • p_edu_mapl: Percentage of 25-44-year-olds with a master’s, professional, or doctoral degree.
  • p_edu_hs_grad_rate: High school graduation rate.
  • p_edu_chronic_absent_rate: Chronic absenteeism rate.

Questions

Question 1

  1. Make two well-labeled plots: a histogram and a boxplot of the median_hh_income variable.

Render, commit, and push your changes to GitHub with the commit message “Added answer for Q1, part a”.

Make sure to commit and push all changed files so that your Git pane is empty afterward.

  1. Provide written responses to the following questions related to the plots created in part a:
  • Is the distribution of median household income in NC counties right-skewed, left-skewed, or approximately symmetric? Which plot(s) helped you determine this?

  • Is the distribution of median household income in NC counties unimodal, bimodal, multimodal or uniform? Which plot(s) helped you determine this?

Render, commit, and push your changes to GitHub with the commit message “Added answer for Q1, part b”.

Make sure to commit and push all changed files so that your Git pane is empty afterward.

  1. In a single pipeline, identify the counties with unusually high median household income (according to the boxplot) and display their names and median household income in descending order of income

Render, commit, and push your changes to GitHub with the commit message “Added answer for Q1, part c”.

Make sure to commit and push all changed files so that your Git pane is empty afterward.

Question 2

  1. Guess what the relationship the percentage of 25-44-year-olds with a bachelor’s degree and median household income might be – positive? negative? no relationship? Explain your reasoning.

  2. Make a scatter plot of median household income (median_hh_income on the y-axis) vs. percentage of 25-44-year-olds with a bachelor’s degree (p_edu_ba on the x-axis). Make sure to set an informative title and axis labels for your plot. Describe the relationship. Was your guess correct?

  3. Again, make a scatter plot of median household income (median_hh_income on the y-axis) vs. percentage of 25-44-year-olds with a bachelor’s degree (p_edu_ba on the x-axis), this time coloring the points by county type (county_type). Based on the resultant plot, does the relationship between these two variables appear to differ by county type?

  4. (Optional) In a single pipeline, compute the average percentage of 25-44-year-olds with a bachelor’s degree and the average median household income across NC counties classified as “Suburban”. Similarly, in a separate pipeline compute the average percentage of 25-44-year-olds with a bachelor’s degree and the average median household income across NC counties classified as “Rural - Metro”. Comment on whether the results are what you would have expected.

Render, commit, and push your changes to GitHub with the commit message “Added answer for Q2”.

Make sure to commit and push all changed files so that your Git pane is empty afterward.

Wrap-up

Warning

Before you wrap up the assignment, make sure that you render, commit, and push one final time so that the final versions of both your .qmd file and the rendered PDF are pushed to GitHub and your Git pane is empty. We will be checking these to make sure you have been practicing how to commit and push changes.

Submission

Submit your PDF document to Gradescope by the end of the lab to be considered “on time”:

  • Go to http://www.gradescope.com and click Log in in the top right corner.
  • Click School Credentials \(\rightarrow\) Duke NetID and log in using your NetID credentials.
  • Click on your STA 199 course.
  • Click on the assignment, and you’ll be prompted to submit it.
  • Mark all the pages associated with question. All the pages of your lab should be associated with at least one question (i.e., should be “checked”).
ImportantChecklist

Make sure you have:

  • attempted all questions
  • rendered your Quarto document
  • committed and pushed everything to your GitHub repository such that the Git pane in RStudio is empty
  • uploaded your PDF to Gradescope

Grading and feedback

  • This lab is worth 30 points:
    • 10 points for being in lab and turning in something – no partial credit for this part.
    • 20 points for:
      • answering the questions correctly – there is partial credit for this part.
      • following the workflow – there is partial credit for this part.
  • The workflow points are for:
    • committing at least three times as you work through your lab,
    • having your final version of .qmd and .pdf files in your GitHub repository, and
    • overall organization.
  • You’ll receive feedback on your lab on Gradescope within a week.

Good luck, and work together!

Footnotes

  1. Remember, haikus not novellas when writing code!↩︎