Lab 1: solutions
Ⓒ2005 Ben Bolker
August 25, 2009
Exercise 2.1 :
-
-
> 1 + 0.2 + 0.2^2/2 + 0.2^3/6
-
> x = 1
> 1/sqrt(2 * pi) * exp(-x^2/2)
> x = 2
> 1/sqrt(2 * pi) * exp(-x^2/2)
Exercise 3.1 : (nothing to write down!)
Exercise 5.1 :
> X = read.table("ChlorellaGrowth.txt", header = TRUE)
> Light = X[, 1]
> rmax = X[, 2]
> logLight = log(Light)
> op <- par(cex = 1.5, cex.main = 0.9)
> plot(logLight, rmax, xlab = expression(paste("Log light intensity ",
+ (mu * E/m^2/s))), ylab = "Maximum growth rate rmax (1/d)",
+ pch = 16)
> title(main = "Data from Fussmann et al. (2000) system")
> fit = lm(rmax ~ logLight)
> abline(fit)
> rcoef = round(coef(fit), digits = 3)
> text(3.7, 3.5, paste("rmax=", rcoef[1], "+", rcoef[2], "log(Light)"))
> par(op)
Exercise 5.2 : explained in text
Exercise 5.3 :
> plot(Light, rmax, xlim = c(0, 120), ylim = c(1, 4))
Exercise 5.4 :
> X = read.table("ChlorellaGrowth.txt", header = TRUE)
> Light = X[, 1]
> rmax = X[, 2]
> logLight = log(Light)
> logrmax = log(rmax)
> op <- par(mfcol = c(2, 1))
> plot(Light, rmax, xlab = "Light intensity (uE/m2/sa)", ylab = "Maximum growth rate rmax (1/d)",
+ pch = 16)
> title(main = "Data from Fussmann et al. (2000) system")
> plot(logLight, logrmax, xlab = "Log light intensity", ylab = "Log max growth rate",
+ pch = 16)
> title(main = "Data from Fussmann et al. (2000) system")
> par(op)
Exercise 5.5 :
> x = 3:8
> y = 5 * x + 3
> op = par(mfrow = c(2, 2))
> plot(x, y, lty = 1, col = 1, type = "l")
> plot(x, y, lty = 2, col = 2, type = "l")
> plot(x, y, lty = 3, col = 3, type = "l")
> plot(x, y, lty = 4, col = 4, type = "l")
> par(op)
Exercise 5.6 : (nothing to say)
Exercise 8.1 :
> ## or
> seq(1,by=4,length=4)
[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6
[20] 4.8 5.0
Exercise 8.2 :
> z=c(1,3,5,7,9,11) ## set value of z
> z[c(2,1,3)]
Exercise 8.3 :
> x = 1:10
> y = (x - 1)/(x + 1)
Exercise 8.4 :
> r = 0.5
> n = 10
> G = r^(0:n)
> sum(G)
Exercise 8.5 :
> set.seed(273)
> x = runif(20)
> x[x < mean(x)]
[1] 0.31074019 0.19282041 0.13098994 0.37142339 0.27956770 0.11890541 0.23134014
[8] 0.15844929 0.02412082
Exercise 8.6 :
[1] 1 3 6 9 10 13 15 16 18
or
> p = 1:length(x)
> p[x < mean(x)]
[1] 1 3 6 9 10 13 15 16 18
Exercise 8.7 *:
> x = 1:40
> n = length(x)
> x[seq(1, n, by = 2)]
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Exercise 9.1 *:
> v = c(1, 2, 1, 2, 1, 2, 1, 2)
> X = matrix(v, nrow = 2, ncol = 4)
or
> v = rep(1:2, 4)
> X = matrix(v, nrow = 2, ncol = 4)
or
> v = rep(1:2, each = 4)
> X = matrix(v, nrow = 2, ncol = 4, byrow = TRUE)
Exercise 9.2 *:
> set.seed(273)
> v = rnorm(35, mean = 1, sd = 2)
> matrix(v, nrow = 5, ncol = 7)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.01249330 1.6935916 2.2846427 -0.1355578 -0.148306 -5.0065003
[2,] -0.73509965 -1.3609527 0.2766241 0.2968747 1.178356 2.4682289
[3,] 3.87044093 -0.4688811 -1.8624548 -0.4004704 1.395131 -2.0472483
[4,] 2.23421665 1.2756400 1.0299978 -0.4123120 -1.113879 -2.2713479
[5,] 0.34382836 1.2179006 -2.7554464 4.3327163 2.207769 -0.6704255
[,7]
[1,] 1.4362361
[2,] -1.2505336
[3,] 0.3638702
[4,] 0.2761186
[5,] 3.6694570
Exercise 9.3 : nothing to do