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")
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.
For the binomial distribution, we can…
dbinom
function to calculate the mass at a given point (i.e., the probability of obtaining exactly k successes in n independent Bernoulli trials with success probability p)pbinom
function to calculate left-tail probabilities (i.e., the probability of obtaining k or fewer successes)Similar functions exist for the Poisson distribution. We can…
dpois
function to calculate the mass at a given pointppois
function to calculate left-tail probabilities (i.e., the probability of obtaining k or fewer counts, with a given \(\lambda\))For the normal distribution, we will be using slightly different functions.
pnorm
function to calculate left-tail probabilities (i.e., the area under the curve, to the left of the point you choose). This is the integral from \(-\infty\) to the point you specifyqnorm
function to determine the value corresponding to a specific quantile of the normal distribution.Note: when you are using these functions, you must specify the standard deviation, not the variance.