Lab 1: solutions
© 2005 Ben Bolker
Exercise 1:
-
> 2^7/(2^7 - 1)
[1] 1.007874
> (1 - 1/2^7)^-1
[1] 1.007874
-
> 1 + 0.2
[1] 1.2
> 1 + 0.2 + 0.2^2/2
[1] 1.22
> 1 + 0.2 + 0.2^2/2 + 0.2^3/6
[1] 1.221333
> exp(0.2)
[1] 1.221403
-
> x = 1
> 1/sqrt(2 * pi) * exp(-x^2/2)
[1] 0.2419707
> dnorm(1)
[1] 0.2419707
> x = 2
> 1/sqrt(2 * pi) * exp(-x^2/2)
[1] 0.05399097
> dnorm(2)
[1] 0.05399097
Exercise 2:
(nothing to do!)
Exercise 3:
> X = read.table("ChlorellaGrowth.txt")
> Light = X[, 1]
> rmax = X[, 2]
> logLight = log(Light)
> op <- par(cex = 1.5, cex.main = 0.9)
> plot(logLight, rmax, xlab = "Log light intensity (uE/m2/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 4:
explained in text
Exercise 5:
> plot(Light, rmax, xlim = c(0, 120), ylim = c(1, 4))
Exercise 6:
> X = read.table("ChlorellaGrowth.txt")
> 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 7:
> 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 8:
(nothing to say)
Exercise 9:
> seq(1, 13, by = 4)
[1] 1 5 9 13
> seq(1, by = 4, length = 4)
[1] 1 5 9 13
> seq(1, 5, by = 0.2)
[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 10:
> z = c(1, 3, 5, 7, 9, 11)
> z[c(2, 1, 3)]
[1] 3 1 5
Exercise 11:
> x = 1:10
> y = (x - 1)/(x + 1)
> plot(x, y, type = "b")
Exercise 12:
> r = 0.5
> n = 10
> G = r^(0:n)
> sum(G)
[1] 1.999023
> 1/(1 - r)
[1] 2
> n = 50
> sum(r^(0:n))
[1] 2
> 2 - sum(r^(0:n))
[1] 8.881784e-16
Exercise 13:
> x = runif(20)
> x[x < mean(x)]
[1] 0.006096034 0.081215761 0.288657362 0.442924182 0.138363040 0.439876188
[7] 0.266660742 0.182081694 0.362768221 0.055500135 0.368313897 0.267021385
Exercise 14:
> which(x < mean(x))
[1] 1 2 3 5 6 8 9 11 12 13 16 19
or
> p = 1:length(x)
> p[x < mean(x)]
[1] 1 2 3 5 6 8 9 11 12 13 16 19
Exercise 15*:
> 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
> x[-seq(2, n, by = 2)]
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Exercise 16*:
> 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 17*:
> v = rnorm(35, mean = 1, sd = 2)
> matrix(v, nrow = 5, ncol = 7)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1.934322 -1.6832689 2.6564640 -0.1636006 5.8214780 -3.2442263
[2,] 3.838532 2.1087967 2.0801288 -2.0904217 -2.8455893 1.5531939
[3,] 1.220530 0.8758584 -0.1898886 1.9576247 4.3258848 -0.2510648
[4,] 4.739258 1.1932033 -2.8016818 1.4586832 5.2427179 2.0921979
[5,] -1.060502 4.1259011 1.3870164 0.7241470 0.6232388 1.3113981
[,7]
[1,] -0.4077326
[2,] 0.9242169
[3,] 2.4103272
[4,] -2.4482055
[5,] 1.4427584
Exercise 18:
nothing to do
File translated from
TEX
by
TTM,
version 3.70.
On 14 Sep 2005, 11:12.