Question 1

  1. 2pts shapiro test
  2. 2pts histogram
  3. 1pt mean and median
  4. 1pt why mean and median differ

Part A

Note that columns 1 thorough 4 are the numeric columns in the iris dataset.

for(i in 1:4){
  print(shapiro.test(iris[,i])$p.value)
}
## [1] 0.01018116
## [1] 0.1011543
## [1] 7.412263e-10
## [1] 1.680465e-08

Part B

library(ggplot2)
qplot(iris$Sepal.Length) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(iris$Sepal.Width) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(iris$Petal.Length) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(iris$Petal.Width) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Part D

mean(iris$Sepal.Length^2)
## [1] 34.82567
median(iris$Sepal.Length^2)
## [1] 33.64

Part E

These values are different because the distribution of iris$Sepal.Length^2 is asymmetric, as shown in the below histogram.

qplot(iris$Sepal.Length^2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

The distribution has a long right tail, which heavily influences the mean, pulling it towards the larger values. The median is less affected by extreme values.

Question 2

  1. 1pt body mass vector
  2. 2pts growth loop
  3. 2pts vargrowth
  4. 2pts environmental vargrowth
  5. 2pts normality test
  6. 1pt questions

Part A

body.sizes <- rnorm(1000, mean=10, sd=3)
## make a copy so we can always go back
body.sizes.original <- body.sizes

Part B

for(year in 1:10) {
  body.sizes <- body.sizes * 1.15
}

#save the results
body.sizes.growth <- body.sizes

Part C

growth.rates <- rnorm(1000, 1.15, 0.2)

#reset the body.sizes variable
body.sizes <- body.sizes.original

for(year in 1:10) {
  body.sizes <- body.sizes * growth.rates
}

body.sizes.vargrowth <- body.sizes

Part D

#reset the body.sizes variable
body.sizes <- body.sizes.original

for(year in 1:10) {
  body.sizes <- body.sizes * growth.rates * rnorm(1, mean=1, sd=0.2)
}

body.sizes.environmentalgrowth <- body.sizes

Part E

Note: I made histograms too because I like them, but I didn’t ask you to.

Original Body sizes

qplot(body.sizes.original, main = "original")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.