-
Notifications
You must be signed in to change notification settings - Fork 66
/
11-distributions.Rmd
123 lines (95 loc) · 3.25 KB
/
11-distributions.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Distribution playground
# Goals
- Review the most common probability distributions used in GLMs and GLMMs
- Get a feeling for how the parameters affect the distribution shapes
- Have an interactive resource to use in the future
# Continuous distributions
## Normal
```{r, eval=FALSE}
library(manipulate)
x <- seq(-10, 10, length.out = 300)
manipulate({
y <- dnorm(
x = x,
mean = mean,
sd = sd)
plot(x = x, y = y, type = "l", ylim = c(0, max(y)),
main = "Normal")},
mean = slider(-10, 10, 0),
sd = slider(0.1, 10, 2))
```
## Log normal
```{r, eval=FALSE}
x <- seq(0, 100, length.out = 300)
manipulate({
y <- dlnorm(
x = x,
meanlog = meanlog,
sdlog = sdlog)
plot(x = x, y = y, type = "l", xlim = c(0, 100), ylim = c(0, max(y)),
main = "Lognormal")},
meanlog = slider(0.1, 10, 3),
sdlog = slider(0.01, 2, 1))
```
## Gamma
What happens if the Gamma shape parameter is large and the scale parameter
is small?
What happens if the Gamma *scale* parameter is large and the *shape* parameter
is small?
What distributions do these resemble? Why is that useful?
```{r, eval=FALSE}
x <- seq(0, 100, length.out = 300)
manipulate({
y <- dgamma(
x = x,
shape = shape,
scale = scale)
plot(x = x, y = y, type = "l", ylim = c(0, max(y)),
main = "Gamma")},
shape = slider(1, 10, 3),
scale = slider(0.1, 30, 2))
```
# Discrete distributions
## Poisson
What happens to the width of the Poisson as the mean (lambda) increases?
When might you expect to observe a Poisson distribution?
```{r, eval=FALSE}
x <- seq(0, 100)
manipulate({
y <- dpois(x,
lambda = lambda)
plot(x = x, y = y, type = "h", xlim = c(0, 60), ylim = c(0, max(y)),
main = "Poisson")},
lambda = slider(0.1, 30, 3))
```
## Negative binomial
There are 2 main parameterizations of the negative binomial distribution. The most common, and the one represented by `dnbinom` is one in which the variance scales as a quadratic function of the mean. `variance = mu + mu^2/size`. This is often referred to as the NB2 parameterization. Note there is an alternative parameterization of NB2: <http://stats.stackexchange.com/a/103579>
The alternative, NB1, allows the variance to scale linearly with the mean. See Hardin and Hilbe "Generalized Linear Models and Extensions".
When might you expect to observe a negative binomial distribution?
What happens when the `size` parameter gets really small? What distribution does this resemble if the `size` parameter gets big?
```{r, eval=FALSE}
x <- seq(0, 100)
manipulate({
y <- dnbinom(
x = x,
size = size,
mu = mu)
plot(x = x, y = y, type = "h", ylim = c(0, max(y)),
main = "Negative binomial")},
size = slider(0.1, 10, 1),
mu = slider(0.1, 60, 4))
```
## Binomial
Read the help `?dbinom` and play with the sliders. What do `size` and `prob` arguments mean?
Think in terms of flipping a coin. What would the `prob` parameter be for a typical coin? If I flipped a coin 6 times what would the parameter `size` be?
```{r, eval=FALSE}
x <- seq(0, 10)
manipulate({
y <- dbinom(x,
size = size,
prob = prob)
plot(x = x, y = y, type = "h", xlim = c(0, 10), ylim = c(0, max(y)),
main = "Binomial")},
size = slider(1, 10, initial = 1, step = 1),
prob = slider(0, 1, initial = 0.5, step = 0.1))
```