Homework 7

Due: 2020-10-06, 11:59pm

For this homework you should submit a ZIP archive called firstnameLastnameHW7.zip. When unzipped there should be a single directory/folder called firstnameLastnameHW7 and all files should be within that directory. For this homework that directory should contain:

  • A single document with the answers to all the following items in HTML or IPYNB format. Make sure you include plain English blocks in between the code and its output, to interpret what R is giving you. There should also be comments in the R code blocks prefixed with #.
  • Code file used to generate the HTML file in RMD format (not needed if using IPYNB).

In this homework we will practice writing functions, and using the apply family of functions.

1. Agren data (50%)

Read in the Agren data. We will compare the fitness distributions in different sites/years and then try normalization.

  • Make a stripchart (or violin plot) of the fitness values in Sweden and Italy for all three years (2009-2011). You will be comparing the six distributions of fitness side-by-side in the same plot. Comment on which years or sites have greater fitness.
  • Write a function that takes a vector (say x) as an argument and returns a vector dividing all values by the median (this would be a form of median normalization).
  • Apply this normalization function to all six fitness columns, and plot the resulting distributions (stripchart or voilin plot). Did the median normalization make the distributions more similar?

2. Soccer data (50%)

Read in the soccer data. We will try to make the points table for the league using apply functions.

Recall that in this league, every team plays every other team once at home and once away. The result of each game is indicated as A (away team win), H (home team win) or D (draw) for the variable FTR (full time result). A win is worth 3 points, a draw 1 point, and a loss 0 points.

  • Use the by function to tabulate the home game results for each team. Relabel the results as W, L, or D (win, loss, draw respectively); note that H corresponds to W.
  • Use the by function to tabulate the away game results for each team. Relabel the results as W, L, or D (win, loss, draw respectively); note that A corresponds to W.
  • Write a function to calculate the points for a team based on the number of wins, losses, and draws.
  • Apply the above function to each team to get the points table.

3. Drawing (optional)

Build on the drawing functions in the class notes to sketch a scene.

  • Write a function to draw a isosceles triangle pointing upward. The function should take four arguments: the x and y co-ordinates of the top point, the height of the triangle, and the width of the base.
  • Write a function to draw a tree by putting a circle on top of a slim rectangle.
  • Write a function to draw a house by putting a triangle on top of a rectangle. Make a door by putting a smaller rectangle inside the rectangle.
  • Draw a domestic scene: House with a tree on one side.

4. Acknowledgements

Cite resources or individuals helping you.