Skip to content

Penguin Explorer

Cartoon of a Chinstrap, a Gentoo, and an AdΓ©lie penguin
Artwork by @allison_horst

Overview

In this activity you’ll explore measurements of Antarctic penguins (the Palmer Penguins data) using third-party libraries for CSV parsing and statistics, plus the array functions you’ve been practicing.

Learning Objectives

  • Load and clean real data with fetch, async/await, and filter
  • Use libraries you haven’t seen before by reading their documentation (Papa Parse, simple-statistics)
  • Group and summarize data with loops and higher-order array methods
  • Ask and answer your own questions about the data (by writing code!)

Getting Started

Download the starter code here (put both files in the same folder): penguins.html and analysis.js

All the code you write goes in analysis.js. The page, penguins.html, is already finished: it imports your functions, runs them, and displays the results.

Modules don’t load when a page is opened straight from your file system, so open it with Live Server in VS Code (or another local web server). If you see a blank page, check the console.

You’ll use two third-party libraries. You import both, at the top of analysis.js:

Library What it does
Papa Parse Parses CSV text into row objects
simple-statistics Mean, median, correlation, and friends

Each row of the data looks like { species: "Adelie", island: "Torgersen", flipper_length_mm: 181, body_mass_g: 3750, sex: "MALE", ... }. Missing values are null.

Instructions

Fill in the TODO functions in analysis.js in order, testing in the browser after each one. (Open the DevTools console and use console.log to peek at your data.) Each part has a collapsed step-by-step hint. Try it on your own first, then open the hint if you need it.

Part 1: loadData

Fetch the CSV, parse it into rows with a CSV library, and keep only the rows where all four measurements are present.

Step-by-step
  1. await a fetch() of DATA_URL.
  2. await the response’s text() method to get the CSV as a string.
  3. At the top of analysis.js, import Papa Parse as Papa from https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm (the /+esm at the end gives a version meant for import). Read the Papa Parse docs for Papa.parse.
  4. Call Papa.parse(text, { ... }) with options so that the first line is used as the header, numbers become numbers, and blank lines are skipped. (Look for header, dynamicTyping, and skipEmptyLines.) The result’s data property is the array of row objects. console.log it. You should see over 300 rows.
  5. Use filter() to keep a row only if every measurement in MEASUREMENTS is a number. (every() is handy here, and missing values are null, which isn’t a number.)
  6. return the filtered array. Check the page! It should say how many penguins were loaded, and a scrollable table of all the penguins (built automatically from your rows) should appear. Use it to check your work.

Part 2: computeStats

Compute some statistics with simple-statistics, a library you haven’t imported yet! This code also goes in analysis.js.

Step-by-step
  1. At the top of analysis.js, import simple-statistics as ss from https://cdn.jsdelivr.net/npm/simple-statistics@7.8.5/+esm. Check the simple-statistics docs for how to import it.
  2. Fill in the exported computeStats(rows). Use map() to make an array of every penguin’s body mass, and another of every flipper length.
  3. In the docs, look up the functions for mean, median, and sample correlation. Call them on your arrays.
  4. return an object with the keys meanMass, medianMass, meanFlipper, and flipperMassCorrelation.
  5. Reload the page and make sure there are no errors in the console.
  6. The stats should appear in the list on the page. The correlation should be between 0.8 - 0.9.

Part 3: Explore by species

Now let’s compare the three species. Two of the measurements are of the bill: length and depth. They’re measured like this:

Diagram of a penguin's bill length and bill depth
Artwork by @allison_horst
  1. groupBy: Use a loop to turn the array of rows into an object of arrays, one per species: { Adelie: [...], Chinstrap: [...], Gentoo: [...] }.
  2. averageByGroup: Given that object, compute the average of a measurement (like body_mass_g) for each species. Use map() to get the values, and ss to find their mean.

The table of averages is shown for you. Use it, and the bill figure, for the first discussion question below.

Part 4: Ask your own questions! πŸ”Ž

Now, as a group, come up with three questions you have about the penguins. For example, “How much heavier are males than females?” or “Which island has the most Adelie penguins?”, etc. Use the data to answer each one in code, using array methods (filter, map, reduce, …) and functions from the stats module (ss)., and show the answers on the page with addAnswer(question, answer).

Fill in askQuestions(rows, addAnswer) in analysis.js.

For each question your team comes up with, call addAnswer("Your question?", "Your answer"), which adds it to the Our Questions list on the page. Aim to finish at least three.

Additional Discussion

Then, if there’s time, talk about:

  1. Look at the table of averages. Which bill measurement (length or depth) separates Gentoo from the other two? Which separates Adelie? Is there any single measurement that separates all three?
  2. The correlation between flipper length and body mass is strong. Does that mean a longer flipper causes a heavier penguin? What else could explain it?
  3. Why does loadData throw out penguins with missing measurements instead of treating them as 0?

Submission

Submit your completed analysis.js (with your team’s three questions and answers), and your group’s answers to the discussion questions.