Download The University of Western Ontario Introduction to R Tutorial 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
The University of Western Ontario
Introduction to R
Tutorial 2
1. The following code produces N simulated random samples from a normal population,
and computes their sample averages:
>
>
>
>
>
+
+
+
set.seed(15243)
N <- 1000
n <- 16
xbar <- numeric(N)
for (i in 1:N) {
x <- rnorm(n, mean=20, sd=5)
xbar[i] <- mean(x)
}
(a) Execute the code in R, and examine the contents of xbar by computing its mean,
standard deviation and plotting a histogram.
(b) Modify the code so that n is assigned the value 1. Re-run the code. What is the
relation between xbar and x in this case?
(c) Plot a histogram of xbar. Also, compute the average and standard deviation of
xbar. Compare the observed values with the theoretical values.
(d) Modify the code again so that n takes the value 4. Re-run the code in R, computing
the mean and standard deviation of xbar. How does the observed average compare
with the theoretical mean? How does the observed standard deviation compare
with the theoretical standard deviation? Plot a histogram of xbar, and note the
distributional shape.
(e) Repeat exercise (d) for n = 9, 25, 64 and 100.
2. Repeat the previous exercise, but starting with the following code which is based on
simulated samples coming from the uniform distribution:
>
>
>
+
+
+
n <- 16
xbar <- numeric(N)
for (i in 1:N) {
x <- runif(n, min=2, max=10)
xbar[i] <- mean(x)
}
3. Repeat the exercise again, but starting with the following code which is based on simulated samples coming from the Poisson distribution:
>
>
>
+
+
+
n <- 16
xbar <- numeric(N)
for (i in 1:N) {
x <- rpois(n, lambda=2)
xbar[i] <- mean(x)
}
1
4. Repeat the previous exercise, but starting with the following code which is based on
simulated samples coming from the exponential distribution:
>
>
>
+
+
+
n <- 16
xbar <- numeric(N)
for (i in 1:N) {
x <- rexp(n)
xbar[i] <- mean(x)
}
In this case, the theoretical mean and standard deviation are both 1.
5. In the previous exercises, you simulated samples from a population. Now, you will
obtain resamples from a given sample.
>
>
>
>
>
+
+
+
library(DAAG)
wreneggs <- subset(cuckoos, species=="wren")
n <- length(wreneggs$breadth)
xbar <- numeric(N)
for (i in 1:N) {
x <- sample(wreneggs$breadth, size=n, replace=TRUE)
xbar[i] <- mean(x)
}
Compute the average, standard deviation and histogram of xbar. Compare with
> mean(wreneggs$breadth)
[1] 15.83333
> sd(wreneggs$breadth)/sqrt(n)
[1] 0.0808683
This gives the bootstrap estimate of the standard error for the mean.
6. Repeat the preceding exercise, but use the robin species instead.
7. Repeat the preceding two exercises, but consider length instead of breadth.
8. Modify the code in exercise 5 so that it computes the sample median of the breadth
measurements for the wren species as well as the bootstrap estimate of the standard
error of the median.
9. The following code gives an alternative to using the for loop:
> xbar <- replicate(N, mean(sample(wreneggs$breadth, size=n, replace=TRUE)))
Execute the code and re-calculate the average and standard deviation of the resulting
vector.
2
Related documents