In today’s lab, you will review using R as a calculator to calculate probabilities from some well-known probability distributions. Before we start, we’ll also learn some R chunk options to create “neater” markdown documents.

The R Markdown template for this assignment may be found by running the following code in your Console.

download.file("https://www2.stat.duke.edu/courses/Spring22/sta102.001/labs/lab-05-template.Rmd", destfile="lab-05.Rmd")

R chunk messages and warnings

Recall that you can create a basic R chunk in an RMarkdown document using the following code:

```{r}
pnorm(2, 0, 1)
```

You may also have noticed that you can name your R chunks (for easier organization later). For instance, the following R chunk would be named “calc-quant”:

```{r calc-quant}
pnorm(2, 0, 1)
```

R chunk names cannot repeat, and they must not contain any spaces.

Through the course of your RMarkdown documents so far, you may have noticed a bunch of “extra” messages and warnings appear, for instance when loading in packages. Although these messages and warnings may be useful for debugging reasons or to verify that packages have successfully loaded, they may not be attractive to include in a final product.

To remove the messages and warnings, we might reference them in the options of the R chunk. Options for R chunks are separated by commas and control the behavior of that specific R chunk. They must be set for each R chunk (well, for now at least). As an example, the following code removes warnings and messages from being displayed. Can you tell which option does what?

```{r calc-quant, message = FALSE, warnings = FALSE}
pnorm(2, 0, 1)
```

Try it yourself! Load in the tidyverse package in an R chunk named “packages.” In this R chunk, turn off messages and warnings, and then knit your document. Do you see a difference from previous documents?

For the rest of today’s lab, you will be practicing the skills learned on the pre-HW activity.

Review: code for distributions

For the binomial distribution, we can…

Similar functions exist for the Poisson distribution. We can…

For the normal distribution, we will be using slightly different functions.

Note: when you are using these functions, you must specify the standard deviation, not the variance.

Exercises

  1. Suppose the probability that a randomly selected American is under 18 years of age is 24%. What is the probability that in a group of six randomly selected Americans, exactly 4 are under 18 years of age?
  2. For a random sample of 30 randomly selected Americans, what is the probability of observing fewer than 10 under the age of 18?
  3. Suppose the number of chanterelle mushrooms in a one square meter area of forest is Poisson distributed with rate \(\lambda\) = 0.6. What is the probability that in a given square meter of forest, we find exactly 2 chanterelles?
  4. What is the probability that we find at least 5 mushrooms in a 3 meter by 3 meter square?
  5. Suppose the mean birthweight of infants in a certain population is 3,500 g., with a standard deviation of 750 g. Underweight babies are those weighing less than 2,500 grams. What percentile of birthweight is this in this population?
  6. What is the probability that an infant has a birthweight between 2 and 4 kg.?