Name:
Andrew ID:
Collaborated with:

This lab is to be completed in class. You can collaborate with your classmates, but you must identify their names above, and you must submit your own lab as an Rmd file on Blackboard, by 11:59pm on the day of the lab.

There are Homework 3 questions dispersed throughout. These must be written up in a separate Rmd document, together with all Homework 3 questions from other labs. Your homework writeup must start as this one: by listing your name, Andrew ID, and who you collaborated with. You must submit your own homework as a knit HTML file on Blackboard, by 6pm on Sunday September 25. This document contains 14 of the 45 total points for Homework 3.

Important remark on compiling the homework: many homework questions depend on variables that are defined in the surrounding lab. It is easiest just to copy and paste the contents of all these labs into one big Rmd file, with your lab solutions and homework solutions filled in, knit it, and submit the HTML file.

Curves

n.quad = 15
set.seed(0)
x.quad = runif(n.quad, -3, 3)
y.quad = x.quad^2 + rnorm(n.quad)
plot(x.quad, y.quad, xlim=c(-3, 3))

Hw3 Q7 (1 points). Add a rug to your last plot, this time along the y-axis, with tickmarks at the y.quad points. (Hint: read the help file for rug(), to see how to control the side of the plot on which the rug is drawn.)

Hw3 Q8 (3 points). The code below generates 5000 draws from a standard normal distribution. Plot a histogram, on the probability scales, with 50 breaks. Color the histogram bars to be pink, and label the x-axis and title the plot appropriately. Then draw the standard normal density curve on top of your histogram, using curve(), as a thick black line. Do histogram bars conform to the density?

x.norm = rnorm(5000)

Surfaces

Colors

n.trig = 250
x.trig = runif(n.trig, 0, pi)
y.trig = runif(n.trig, 0, pi)
z.trig = cos(x.trig)^2 + sin(y.trig)^2 + 0.5*rnorm(n.trig)

Hw3 Q9 (10 points). Below, we read in a data table on earthquakes from around the world, of magnitude 6+, since 2002. This is a cleaned up version of the data set we looked at in Lab 3w. We then plot a map of the world, using the map() function (you will need to have the package maps installed for this). Building off of this starter code, plot each earthquake in the anss.tab data table on top of the world map according to the geographic location in which it occurred (hint: look at the “Lat” and “Lon” columns of the data table.) Each earthquake should be represented by a small filled circle. Also, color the circles according to earthquake magnitude (hint: the “Mag” column of the data table), using 25 colors from the topographic color palette.

anss.tab = read.table("http://www.stat.cmu.edu/~ryantibs/statcomp-F16/data/anss.dat",
                      sep="\t", header=TRUE)
head(anss.tab)
##         Date        Time     Lat      Lon Depth Mag
## 1 2002/01/01 10:39:06.82 -55.214 -129.000  10.0 6.0
## 2 2002/01/01 11:29:22.73   6.303  125.650 138.1 6.3
## 3 2002/01/02 14:50:33.49 -17.983  178.744 665.8 6.2
## 4 2002/01/02 17:22:48.76 -17.600  167.856  21.0 7.2
## 5 2002/01/03 07:05:27.67  36.088   70.687 129.3 6.2
## 6 2002/01/03 10:17:36.30 -17.664  168.004  10.0 6.6
library(maps)
map("world")
title("Earthquakes colored by magnitude")

Most of the circles appear in a darkish purple/blue color, with only a few in lighter green/yellow colors. Why? To support your explanation, plot a histogram, on the frequency scale, of the earthquake magnitudes in anss.tab, with break locations set from 5.9 to 9.1, in increments of 0.1. Label the x-axis and title the plot appropriate.

Plot another map of the world, with earthquakes drawn on top using filled circles, this time coloring them according to earthquake depth (hint: the “Depth” column of the data table). Set the title appropriately. Where do some of the deepest earthquakes occur?

Plot another histogram, on the frequency scale, this time of the earthquake depths. Set the break locations to be from 0 to 700, in increments of 10, and set the x-axis label and title appropriately.

Finally, fit all 4 of the previous plots into one single graphical display, using a 2 x 2 grid for the plots. The top left plot should be the world map with earthquakes colored by magnitude, and the top right plot should be the world map with earthquakes colored by depth. The bottom two plots should be the magnitude and depth histograms, from left to right. (Hint: recall par() with mfrow().)

Hw3 Bonus. Color the bars in the previous two histograms according to the same color scales you used in the previous two world maps, respectively. For example, in the histogram of earthquake magnitudes, the bar for a histogram of magnitude 6.1 should be colored in the same color you would use for the circle of a magnitude 6.1 earthquake on the world map. And the same for the histogram of earthquake depths.