R includes a number of operations that associate distributional families (i.e., normal, binomial) with a set of four functions. These functions are:
d
)p
)q
)r
)These letter prefixes combine with the names of distribution, with
additional arguments. For example, the generate r
andom
norm
al numbers, we take the prefix r
with the
distribution norm
to get the function rnorm
.
He, we demonstrate generating 5 random normal numbers
## Generate 5 random numbers with N(0, 1)
rnorm(n = 5, mean = 0, sd = 1)
## [1] -0.090412 0.263064 0.360291 -0.390299 -2.671451
Note that this function takes as arguments the mean and standard deviation, the two distributional parameters associated with a normal distribution.
You can find the full list of available distributions with
?Distributions
Question 1: Using the documentation effectively is
an important component of improving your efficiency and effectiveness
with R. Copy and paste ?rbinom
into your console and read
the documentation. What does this function do? Explain the difference
between rbinom(5, 1)
and rbinom(1, 5)
. Which
of these would you use to simulate a collection of Bernoulli random
variables?
The density function (d
) is the R implementation of the
PMF/PDF functions for distributions. Given a realized value, along with
distributional parameters, these functions will return either a
probability (for discrete distributions) or a density (for continuous).
For example, suppose we have \(Y \sim Bin(5,
\pi = .25)\). We can find the probability that in 5 trials we
have 3 success is
\[ P(Y = 3) = \binom{5}{3} (0.25)^3(0.75)^{5-3} \] We can find this directly with
dbinom(x = 3, size = 5, prob = 0.25)
## [1] 0.087891
Similarly, we have the cumulative density/mass functions
(p
) that tells use the probability of observing values
greater than or less than our argument (i.e., this function can be used
to compute p-values)
Considering our previous situation with \(Y ~ Bin(5, \pi = .25)\), consider
\[
P(Y \leq 3) = \sum_{i=0}^3 \binom{5}{i} (0.25)^i(0.75)^{5-i}
\] We can find this probability using pbinom
or,
slightly more laboriously, using dbinom
pbinom(3, 5, 0.25)
## [1] 0.98438
## These are the same
# Note the vector argument x = 0:3
sum(dbinom(x = 0:3, size = 5, prob = 0.25))
## [1] 0.98437
It follows from this that we can find \(P(Y > 3)\) with
1 - pbinom(3, 5, 0.25)
## [1] 0.015625
Question 2: Use pbinom
function to
determine \(P(2 \leq Y \leq 4)\) when
\(Y ~ Bin(5, \pi = .25)\)
Question 3: Consider a random variable \(Y \sim Pois(\mu = 2)\). How would you use a probability function to find \(P(Y > 3)\)?
Question 4: What is the q
prefix used
for? Use the appropriate function to find the 95% CI critical values for
a t
distribution with 20 degrees of freedom