---
title: "Homework 01 Solutions"
date: "8/30/2020"
output:
  pdf_document:
    keep_tex: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, include = TRUE)
```

# Chapter 1 Exercises

```{r}
load("../usingR.RData")
```

## Exercise 1
```{r 1_1}
plot(distance ~ stretch, data = elasticband)
```

## Exercise 2
```{r 1_2}
# i.
snow <- data.frame(year = 1970:1979,
                   snow.cover = c(6.5, 12, 14.9, 10, 10.7, 7.9, 21.9, 12.5, 14.5, 9.2))

# ii.
plot(snow.cover ~ year, data = snow)

# iii. 
hist(snow$snow.cover, xlab = "Snow cover", main = "Histogram of snow cover")

# iv.
plot(log(snow.cover) ~ year, data = snow)
hist(log(snow$snow.cover), xlab = "Log of snow cover", main = "Histogram of snow cover")
```

## Exercise 3
```{r 1_3}
orings <- data.frame(temperature = c(53, 57, 63, 70, 70, 75),
                     erosion = c(3, 1, 1, 1, 1, 0),
                     blowby = c(2, 0, 0, 0, 0, 2),
                     total = c(5, 1, 1, 1, 1, 1))

plot(total ~ temperature, data = orings)
```

# Chapter 2 Exercises

## Exercise 1
```{r 2_1a}
# 2.1(a)
answer <- 0
for (j in 3:5) {answer <- j + answer}
print(answer)
```

```{r 2_1b}
# 2.1(b)
answer <- 10
for (j in 3:5) {answer <- j + answer}
print(answer)
```

```{r 2_1c}
# 2.1(c)
answer <- 10
for (j in 3:5) {answer <- j*answer}
print(answer)
```

## Exercise 2
```{r 2_2}
?prod
prod(c(10, 3, 4, 5))
```

## Exercise 5
```{r 2_5}
radii <- 3:20
spheres <- data.frame(radius = radii, volume = 4*pi*radii^3/3)
spheres
```
When you type `pi` in R and hit `Enter`, R prints the value of $\pi$ to some
number of digits.

## Exercise 6
```{r 2_6}
# Identify which columns are factors
factor_columns <- sapply(tinting, is.factor)
factor_columns

# Get levels of factor columns
sapply(tinting[factor_columns], levels)

# Check which columns are ordered factors
sapply(tinting, is.ordered)
```

# Chapter 3 Exercises

## Exercise 1
```{r 3_1}
# 3.1(a)
plot(mean.height ~ year, data = huron)

# 3.1(b)
# Note: These labels won't necessarily show up in your saved output, which is fine.
identify(huron$year, huron$mean.height, labels = huron$year)

# 3.1(c)
# Note: If you read the docs for lag.plot(), with ?lag.plot or help(lag.plot),
# you'll see that you can't change certain graphical parameters, including xlab and ylab.
lag.plot(huron$mean.height, do.lines = FALSE)
```

## Exercise 3
```{r 3_3}
# 3.3(a)
hist(possum$hdlngth, xlab = "Head length")

# 3.3(b)
# Note: the code in the problem is incorrectly given as stem(qqnorm(possum$hdlength))
stem(possum$hdlngth)

# 3.3(c)
qqnorm(possum$hdlngth)

# 3.3(d)
plot(density(possum$hdlngth), main = "Estimated density of possum head length")
```

These displays provide slightly different types of information. The stem and leaf plot is kind of a sideways histogram, with the bin widths fixed at 1, but it allows you
to see all the individual data points. The histogram
provides more flexibility in that bin widths are adjustable, but it is sensitive to
the bin width chosen. The density plot is a smoothed version of the histogram, which
may give a better sense of the shape of the distribution, but it is sensitive to
the parameters of kernel used to estimate the curve. The normal QQ plot makes it
easy to see deviations from normality, but it is hard to see what the distribution
is if it is not normal. The other displays make it much easier to see the shape of
the distribution.

## Exercise 5
```{r 3_5}
par(mfrow = c(3, 4))
for (i in 1:4) {
  qqnorm(rnorm(10))
}
for (i in 1:4) {
  qqnorm(rnorm(100))
}
for (i in 1:4) {
  qqnorm(rnorm(1000))
}

# Typically you'll want to reset the grid after changing it.
par(mfrow = c(1, 1))
```
The larger the sample size, the more representative the sample (i.e. the closer to
normal), and hence the straighter the line in the QQ plot. 