`R`

Basics**Lab Goal:** The simple goal of this lab is to get you thinking about `R`

and RStudio again. It is not meant to be an introduction to `R`

.

This lab can be completed using only `R`

and without RStudio, but you should really use RStudio. For reference material, see Applied Statistics with `R`

chapter two through six.

**[Exercise]** Calculate \(e^2\).

**[Exercise]** Calculate the natural log of 3.

`y = c(0, 2, NA, 3, 4, 1, 9, 0)`

**[Exercise]** Calculate the mean of `y`

with the missing values removed. Use **only** the `mean()`

function.

- Hint 1: Check the documentation for
`mean()`

. - Hint 2: If you’re really stuck, there’s always this approach.

**[Exercise]** Run the following code:

```
ggplot(mpg, aes(x = reorder(class, hwy), y = hwy, fill = class)) +
geom_boxplot() +
xlab("class") +
theme(legend.position = "none")
```

To do so, you will need to make sure the `ggplot2`

package is installed, and loaded.

**[Exercise]** Modify the following code to run, but without loading the entire `MASS`

library. The `lda()`

function is from the `MASS`

package.

`lda(Species ~ ., data = iris)$means`

`x = 1:100`

**[Exercise]** Calculate

\[ \sum_{i = 1}^{n} \ln(x_i). \]

That is, sum the natural log of each element of `x`

.

**[Exercise]** After running the following code, how many of the elements of `some_vector`

are larger than `1`

? A good solution will use only one line of code.

```
set.seed(42)
some_vector = rnorm(100)
```

**[Exercise]** Consider a random variable \(X\) that has a normal distribution with a mean of 5 and a variance of 9. Calculate

\[ P[X > c], \]

for \(c = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.\)

The following code is “correct” but **terrible**. Don’t write code like this!

```
set.seed(1337);mu=10;sample_size=50;samples=100000;
x_bars=rep(0, samples)
for(i in 1:samples)
{
x_bars[i]=mean(rpois(sample_size,lambda = mu))}
x_bar_hist=hist(x_bars,breaks=50,main="Histogram of Sample Means",xlab="Sample Means",col="darkorange",border = "dodgerblue")
mean(x_bars>mu-2*sqrt(mu)/sqrt(sample_size)&x_bars<mu+2*sqrt(mu)/sqrt(sample_size))
```

**[Exercise]** Fix this code! You don’t need to change how the code accomplishes the task, but you should update the **style**.