R Corner distribution functions BMR 617 February 5
R Corner: distribution functions BMR 617 February 5 th 2020 Marshall University Joan C. Edwards School of Medicine
Distribution functions • For each common probability distribution, R provides four functions • Binomial distribution: • dbinom, pbinom, qbinom, rbinom • Normal (Gaussian) distribution: • dnorm, pnorm, qnorm, rnorm • In each case, these represent the density, cumulative distribution, quantile, and random generator functions, respectively
Density functions: discrete • For discrete distributions, such as the binomial distribution, the density function represents the probability a random variable drawn from that distribution takes a given value • E. g. dbinom(x=2, size=10, prob=0. 5) is the probability of exactly 2 successes from 10 trials with probability of success 0. 5 (2 heads in 10 flips of a fair coin) > dbinom(2, 10, 0. 5) [1] 0. 04394531 > ( factorial(10)/(factorial(2)*factorial(8)) ) * 0. 5^2 * 0. 5^8 [1] 0. 04394531
Distribution functions: continuous • For continuous distributions, such as the normal distribution, the density function is more subtle • Give the relative probability density at a given value • If we plot dnorm(x, mean=0, sd=1) over a range of x values, the area under the graph from x 1 to x 2 is the probability a value drawn from the standard normal distribution lies between x 1 and x 2 > x <- seq(-3, 3, 0. 1) > plot(x, dnorm(x), type='l')
Cumulative distribution functions • The cumulative distribution functions for each distribution give the probability a value from the corresponding distribution is less than or equal to the supplied value • For example: pbinom(8, size=10, prob=0. 5) is the probability of 8 or fewer successes in 10 trials in a binomial experiment with probability of success 0. 5 • And pnorm(2, mean=0, sd=1) is the probability a value drawn from the standard normal distribution has a value less than (or equal to) 2 • By the “standard deviation rules” this will be close to 0. 975…
Quantile functions • The quantile functions are the inverse of the cumulative distribution functions; for a given probability, they give a value x whose probability of a value less than or equal to x is is the given probability • So for example, since ~68% of values in a normal distribution lie within one standard deviation of the mean, 16% lie more than one standard deviation to the right and 16% lie more than one standard deviation to the left. Consequently, qnorm(0. 84, mean=0, sd=1) should be close to 1.
Random generation functions • The random generation functions create values randomly “sampled” from the specified distribution rnorm(20, mean=0, sd=1) will generate 20 values drawn from the standard normal distribution. • Plotting a histogram of such values should give an approximate normal distribution shape: hist(rnorm(20, mean=0, sd=1)) hist(rnorm(10000, mean=0, sd=1), breaks=100)
- Slides: 7