The rnorm() function generates a vector of random normal variables, with first argument n; the sample size. Here, we create two correlated sets of numbers, x and y, and use the cor() function to compute the correlation between them.

> x = rnorm(50)
> y = x+rnorm(50, mean = 50, sd = .1)
> cor(x, y)
[1] 0.995

By default, rnorm() creates standard normal random variables with a mean of 0 and a standard deviation of 1. However, the mean and standard deviation can be altered using the mean and sd arguments. The mean() and var() functions can be used to compute the mean and variance of a vector of numbers. Applying sqrt() to the output of var() will give the standard deviation. Or we can simply use the sd() function.

Each time we call the rnorm() function, we will get a different answer. Sometimes we want our code to reproduce the exact same set of random numbers; we can use the set.seed() function to do this. The set.seed() function takes an (arbitrary) integer argument.

set.seed(1303)
rnorm(50, mean = 0, sd = 1)
[1] -1.1440 1.3421 2.1854 0.5364 0.0632 0.5022 -0.0004

We use set.seed() throughout the labs whenever we perform calculations involving random quantities. In general, this should allow the user to reproduce our results. However, it should be noted that as new versions of R become available it is possible that some small discrepancies may form between the exercises and the output from R.

Question

Use the code below as a starting point.