HW 01 - Pet names

Meet the toolkit

Photo by Jovana Askrabic on Unsplash Photo by Jovana Askrabic on Unsplash

The goal of this assignment is to introduce you to R, Posit Cloud, and Quarto, which you’ll be using throughout the course to learn data science concepts, analyze real data, and communicate your results.

Getting started

Prerequisites

This assignment assumes that you have reviewed the lecture titled “Meet the toolkit: Programming”. If you haven’t yet done so, please pause and complete it before continuing.

Terminology

We’ve already thrown around a few new terms, so let’s define them before we proceed.

Starting slow

As the course progresses, you are encouraged to explore beyond what the assignments dictate; a willingness to experiment will make you a much better programmer! Before we get to that stage, however, you need to build some basic fluency in R and Posit Cloud.

Before you get started, make sure that you are a member of the course Posit Cloud workspace.

Workflow

For assignments in this course, you will work in a project in our Posit Cloud workspace. The basic workflow will be:

Open assignment → Work in Posit Cloud → Run code → Render → Check → Submit

As you work, run your code frequently so that you can see what it does and catch errors early. Render your Quarto document periodically to make sure that your code, output, figures, and written responses appear correctly in the finished document.

Go to Posit Cloud

Go to posit.cloud and navigate to the course workspace via the left sidebar. It’s important that you work in the course workspace so that you have access to the course projects and the R packages that have been set up for you.

Open the project for this assignment.

Hello Posit!

Posit is comprised of four panes.

Warm up

Before we introduce the data, let’s warm up with some simple exercises.

The top portion of your Quarto file (between the three dashed lines) is called YAML. It stands for “YAML Ain’t Markup Language”. It is a human friendly data serialization standard for all programming languages. All you need to know is that this area is called the YAML (we will refer to it as such) and that it contains meta information about your document.

Step 1. Update the YAML

Open the Quarto (Rmd or qmd) file in your project, change the author name to your name, and knit the document.

Step 2. Check your rendered document

After you knit the document, open the rendered output and check that your name appears correctly and that the document renders without errors.

Throughout the assignment, use this same habit: make a change, run your code, render the document, and check the result.

Packages

R is an open-source language, and developers contribute functionality to R via packages. In this assignment we will use the following packages:

We use the library() function to load packages. In your Quarto document you should see an R chunk labelled load-packages which has the necessary code for loading both packages. You should also load these packages in your Console, which you can do by sending the code to your Console by clicking on the Run Current Chunk icon (green arrow pointing right icon).

Note that these packages are also get loaded in your Quarto environment when you Render your Quarto document.

Data

The city of Seattle, WA has an open data portal that includes pets registered in the city. For each registered pet, we have information on the pet’s name and species. The data used in this exercise can be found in the openintro package, and it’s called seattlepets. Since the dataset is distributed with the package, we don’t need to load it separately; it becomes available to us when we load the package.

You can view the dataset as a spreadsheet using the View() function. Note that you should not put this function in your Quarto document, but instead type it directly in the Console, as it pops open a new window (and the concept of popping open a window in a static document doesn’t really make sense…). When you run this in the console, you’ll see the following data viewer window pop up.

View(seattlepets)

You can find out more about the dataset by inspecting its documentation (which contains a data dictionary, name of each variable and its description), which you can access by running ?seattlepets in the Console or using the Help menu in Posit to search for seattlepets.

Exercises

The ✏️ symbol is a reminder to write a written response discussing the questions in the exercises.

  1. ✏️ According to the data dictionary, how many pets are included in this dataset?

🧶 ✅ Write your answer under Exercise 1 and render the document.

  1. ✏️ Again, according to the data dictionary, how many variables do we have for each pet?

🧶 ✅ Write your answer under Exercise 2 and render the document.

  1. ✏️ What are the three most common pet names in Seattle? To do this you will need to count the frequencies of each pet name and display the results in descending order of frequency so that you can easily see the top three most popular names. The following code does exactly that. Don’t just rely on the output, but write our your answer of what the three most common pet names are.

The two lines of code can be read as “Start with the seattlepets data frame, and then count the animal_names, and display the results sorted in descending order. The”and then” in the previous sentence maps to |>, the pipe operator, which takes what comes before it and plugs it in as the first argument of the function that comes after it.

seattlepets |>
  count(animal_name, sort = TRUE)

🧶 ✅ Complete the code in the code chunk provided, run the code to examine the output, and write your answer under Exercise 3. Then render the document and check that your code, output, and written response appear correctly.

Let’s also look to see what the most common pet names are for various species. For this we need to first group_by() the species, and then do the same counting we did before.

Looks like many of those NAs were cats. Poor unnamed kitties…

seattlepets |> 
  group_by(species) |>
  count(animal_name, sort = TRUE)
## # A tibble: 16,823 × 3
## # Groups:   species [4]
##   species animal_name     n
##   <chr>   <chr>       <int>
## 1 Cat     <NA>          406
## 2 Dog     Lucy          337
## 3 Dog     Charlie       306
## 4 Dog     Bella         249
## 5 Dog     Luna          244
## 6 Dog     Daisy         221
## # ℹ 16,817 more rows

But this output isn’t exactly what we wanted. We wanted to know the most common cat and dog names, but there are barely any cats present in this output! This is because there are more dogs than cats in the dataset overall. We can confirm this by counting the various species in the data.

6 pigs in the city? Ok… But we’ll continue with cats and dogs.

seattlepets |>
  count(species, sort = TRUE)
## # A tibble: 4 × 2
##   species     n
##   <chr>   <int>
## 1 Dog     35181
## 2 Cat     17294
## 3 Goat       38
## 4 Pig         6

Let’s search for the top 5 cat and dog names. To do this, we can use the slice_max() function. The first argument in the function is the variable we want to select the highest values of, which is n. The second argument is the number of rows to select, which is n = 5 for the top 5. It may be a bit confusing that both of these are n, but this is because we already have a variable called n in the data frame.

seattlepets |> 
  group_by(species) |>
  count(animal_name, sort = TRUE) |> 
  slice_max(n, n = 5)
## # A tibble: 53 × 3
## # Groups:   species [4]
##   species animal_name     n
##   <chr>   <chr>       <int>
## 1 Cat     <NA>          406
## 2 Cat     Luna          111
## 3 Cat     Lucy          102
## 4 Cat     Lily           86
## 5 Cat     Max            83
## 6 Dog     Lucy          337
## # ℹ 47 more rows
  1. ✏️ Based on the previous output we can easily identify the most common cat and dog names in Seattle, but the output is sorted by n (the frequencies) as opposed to being organized by the species. Build on the pipeline to arrange the results so that they’re arranged by species first, and then n. This means you will need to add one more step to the pipeline, and you have two options: arrange(species, n) or arrange(n, species). You should try both and discuss which one organizes the output by species and then ranks the names in order of frequency for each species.

🧶 ✅ Complete the code in the code chunk provided, run the code to examine the output, and write your answer under Exercise 4. Then render the document and check that your code, output, and written response appear correctly.

The following visualization plots the proportion of dogs with a given name versus the proportion of cats with the same name. The 20 most common cat and dog names are displayed. The diagonal line on the plot is the \(x = y\) line; if a name appeared on this line, the name’s popularity would be exactly the same for dogs and cats.

  1. ✏️ What names are more common for cats than dogs? The ones above the line or the ones below the line? Explain your reasoning.
  2. ✏️ Is the relationship between the two variables (proportion of cats with a given name and proportion of dogs with a given name) positive or negative? What does this mean in context of the data? Explain your reasoning.

🧶 ✅ Render your document one final time. Review the rendered document to make sure all of your answers, code, output, and figures appear correctly before submitting your work.