Homework 4: How the Tables Have Turned!

Handin

After completing the homework, you will submit:

Hand in your work at this Google Form (https://goo.gl/forms/Fkr461qrsm27iZnP2)

The Assignment

Setup

Paste this code into a Pyret file called planet-tables.arr to begin. Put all of your answers in that file using the variable planet-data!

include tables
include gdrive-sheets
include shared-gdrive("cs111-2018.arr", "1XxbD-eg5BAYuufv6mLmEllyg28IR7HeX")

# planet-data spreadsheet id from Google Sheets
ssid = "1VoK2piJbKBPR9paA2-J6B4E4XSgKZuMHXc8rWs-n8WE"
data-sheet = load-spreadsheet(ssid)
planet-data = 
  load-table: planet, radius, distance_from_sun, orbital_speed, color    
    source: data-sheet.sheet-by-name("planet-sheet", true)
  end
  
pi = 3.14

Hints:
The table documentation (https://cs.brown.edu/courses/csci0111/docs/tables-funs-docs.html) will be helpful for all of the problems in this assignment
In particular, you may want to take a look at filter-by, sort-by, build-column, and row-n.

The Jupiter Travel Board

Your spaceship is quickly approaching Jupiter, but since it’s a gaseous planet, you can’t land. You try to turn the ship around but - oh no! Jupiter’s pull is too strong, and you’re getting sucked in! Fortunately, the alien Amy appears and sends you a message. She says she will get you out of there if you can help her with her work for the Jupiter Travel Board. Lucky you!

The Jupiter Travel Board is worried that no one can come to their planet, and is considering opening a Jupiter-themed resort on another planet. They need you and Amy to help choose the best planets for both Earthlings and Jupiterians. They know the following specifications:

Using the data in the table, write two functions called jupiterian-planet and earthling-planet that take a row from the planet-data table as input and return a boolean, indicating whether or not the row represents a planet suited to jupertians / earthlings, based on the above requirements.

However, the Jupiter Travel Board wants to see on which planets both Earthlings and Jupiterians can coexist. Write a function called good-planets that takes a Table as input and returns a Table with the rows for those planets that Jupiterians and Earthlings can both visit.

Make sure to include tests on planet-data for each of these three functions. You can use T.row-n(N) for some table T and row-number N to extract a particular row for testing.

Testing Clarification Added 9/28, 4:30pm: In lecture, we discussed creating a sample table to use for testing functions that return tables. This is a bit painful for the planet data, given the nature of the numeric data (that you’d otherwise have to copy by hand). As an alternative, you can write an expression on the right side of the is that builds the expected output table from rows of your existing table (this is mostly useful for testing filter-by on small tables). Here’s an example of how to create a table with two specific rows from the gradebook example:

add-row(
  add-row(
    gradebook.empty(), gradebook.row-n(1)), 
  gradebook.row-n(3))

gradebook.empty() returns a table with the columns of gradebook, but no rows. The two calls to add-row insert rows from the gradebook table into the empty table (thus building up the table with rows 2 and 4 from the gradebook). You could put this into a where test as follows:

where:
  <some expression> is add-row(add-row ...)

Amy’s getaway

Amy is taking a sabbatical for a year to travel the universe and discover herself. She wants to find the planet with the longest number of days per year so she can have the longest vacation. Help Amy out by creating a function called longest-year that takes a Table as input and returns the name of the planet with the most number of days in its year.

Climate Conundrum

You will learn about plotting in class on Friday. You are welcome to try this on your own beforehand, but you will get more detailed instruction then! :)

Amy is studying climate change data from Earth because she is concerned that Earth will soon become uninhabitable like Jupiter. Copy the code below into ‘climate-tables.arr’ to import the climate change data.

include tables
include gdrive-sheets
include math
include shared-gdrive("cs111-2018.arr", "1XxbD-eg5BAYuufv6mLmEllyg28IR7HeX")

# climate-data spreadsheet id from Google Sheets
ssid = "1vk7Dzm01DDp8uiXL5ToIzxArHyBkBhrO6c-tGE-bB9U"
data-sheet = load-spreadsheet(ssid)
climate-data =
  load-table: foreign-direct-investment, urban-population-growth, urban-population, population, gdp, energy-use-per-capita, co2-emissions
	source: data-sheet.sheet-by-name("climate-sheet", true)
end

We will be making scatter plots to analyze different relationships from the data in the table. First, type climate-data into the bottom of the interactions window (left side) and run the program to take a look at the table you’ve imported.

The factors we will be comparing are:

For each relationship, write a brief hypothesis describing whether you think there will be a correlation between the two things, and what you think the connection will be. Also, write a few sentences predicting which of the relationships will have the strongest correlation. For each of your hypotheses, make sure to provide justification.

Write expressions in the definitions window that generate the three scatter plots above. Use the function ‘scatter-plot’, whose documentation we have provided below:

scatter-plot :: (t :: Table, xs :: String, ys :: String) -> Image
Displays a scatter plot from the given table. xs determines the name of the table column to use for x-axis values, and ys determines the name of the table column to use for y-axis values.

Choose the scatter plot that looks like it has the greatest correlation and perform a linear regression (writing the expression in the definitions window)

Write a few sentences analyzing graphs you have just plotted. How accurate was your prediction? What other methods have you used in the past (if at all) to plot large data? Was it easier or more difficult than what you did above? In what ways?