You've been recruited by the PEZ company to do research on their competition. To do this research, the company gives you their research datasets for inspiration and analysis!
In this lab you’re going to be working with tables!
FiveThirtyEight conducted a survey in which tens of thousands of people were asked to choose between two candies; from the survey's responses, they computed the winning percentage of each candy. Then, they compiled a data-set with Number
attributes like the winning percentage, relative price, and sugar percentage, and also Boolean
attributes such as chocolate, fruity, caramel, and hard. This data has been published in a FiveThirtyEight article called “The Ultimate Halloween Candy Power Ranking," which is definitely worth a read after the lab.
In this lab, you’ll be looking at the relationships between these columns.
First, include this code at the top of your program:
This code loads a Pyret table from a Google Sheet. Press the "Run" button in the top right corner to process the code in the definitions window. Then type candy-data
in the interactions window to see the data as a table.
You’ll want to refer to the CS111 Pyret Tables Documentation for this lab (not the built-in Pyret documentation).
Let's use the powers of filtering to learn more from our candy data.
Suggestion: For each section, separate it from the next part by pasting this line below it.
We want to know which candies have the most sugar. Write an expression in the definitions window that produces a table containing only the candies where sugar-percent
is greater than 0.75.
Hint: Take a look at the filter-with
function, and remember that functions can be passed as inputs to other functions. Call over a TA if you want help using this structure.
Now that we know how to satisfy our sweet tooth, write an expression in the definitions window that produces a table containing only the candies where price-percent
is greater than 0.90.
How many of the candies have chocolate?
Write an expression in the definitions window that outputs this number in the interactions window. You can get the length of a table by writing <table>.length()
where <table>
is the name of a table (or an expression that evaluates to a table).
Of the candies that have chocolate, what proportion also have caramel?
Write an expression in the definitions window that outputs this proportion in the interactions window.
Of the candies that have chocolate, what proportion are also nutty?
Write an expression in the definitions window that outputs this proportion in the interactions window.
What attribute is paired most frequently with chocolate?
To help answer this question, write a function that generalizes the expressions from 1.4 and 1.5. This function will take in a String
representing the name of the ingredient being paired with chocolate and a Table
that is only candies that have the chocolate attribute. This function returns a number (which is the proportion of candies that have the given ingredient out of the candies that have chocolate).
The function declaration should look something like this:
candy-with-attr(table :: Table, attr :: String)-> Number:
Look for commonalities across the code you wrote for caramel (1.4) and nutty (1.5).
Compare the results of your function on the inputs “fruity,” “nutty,” and “caramel.” You don’t have to write code for this comparison – just use your new function to compute all three proportions and compare them manually.
Build a column of Boolean
values that indicates whether a candy is both fruity and hard, but not a pluribus. This should produce a new table with an added column. Write an expression in the definitions window that uses this new column to compute how many candies meet this condition.
(Pluribus means that there are multiple candies in a packet, ex: Skittles, M&M’s, and Smarties)
Hint: Take a look at the build-column
function. Call over a TA if you want help using this.
Of the candies for which this Boolean
is true (fruity and hard, but not a pluribus), which has the highest winning percentage?
Hint: This requires the use of the order-by
function, in addition to the row-n
and filter-with
functions.
An example of the row-n
function to take the fifth row of a table:
Of the candies for which this Boolean
is true, what’s the average winning percentage?
Hint: This requires the use of the mean
function.
What’s the relationship between sugar and winning percentage? Do these two attributes seem correlated? One way to gain intuition on this is to create a scatterplot that puts one attribute on each axis.
The tables documentation includes a scatter-plot function. Look at the documentation and try to figure out how to use it to generate a scatterplot of sugar versus winning percentage (it does not matter which variable goes on each axis). Write an expression in the definitions window that creates the scatterplot. In a comment, summarize the relationship in a sentence or two.
This is a discussion question – you are NOT being asked to write code to do this analysis. In a comment, write a few sentences to summarize your ideas.
The CEO at PEZ wants to understand the relative frequencies of the attributes in the table across the collection of candies studied. They want a function where they can give you names of two attributes (column names in the dataset), and you tell them which is more frequent (your function can return "same"
if the two attributes are in the same number of candies). Design this function.
Discuss with your partner how you might go about testing this function. Write a comment describing your testing plan.
Now imagine that the analysts want to see the relative frequencies across all of the attributes. How would you want to report that information, and what would be needed to produce that report from the given dataset? You can think about the functions in the documentation, or you can think about reorganizing the data in the table to make it easier.
Now think about questions you have about this data, or come up with interesting questions to ask. Investigate (by writing and running expressions), and write a couple sentences in a comment about what you found.
This lab has mostly been about getting you comfortable working with tabular data and practicing some common operators on tables. It also gets you thinking about our course's focus on data: what patterns of manipulating data do we often use in computations? How does the organization of our data impact our ability to answer these questions?
Here, we see that filtering, ordering, and summarizing data are some of the key operations. So far we’ve only looked at these operations on tabular data, but these same building blocks will arise many times through this course. When you have a computation to perform around data, you should start by thinking through what combinations of filtering, sorting and summarizing will help you compute your answer.
Great job! For all of your hardwork you've been rewarded with a lifetime supply of PEZ candy.