-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.2-SimulatingNulls.qmd
222 lines (159 loc) · 4.68 KB
/
5.2-SimulatingNulls.qmd
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
---
title: "Simulating null distributions"
subtitle: "Beyond only sampling error"
author:
- Elizabeth King
- Kevin Middleton
format:
revealjs:
theme: [default, custom.scss]
standalone: true
self-contained: true
logo: QMLS_Logo.png
slide-number: true
show-slide-number: all
code-annotations: hover
bibliography: Randomization.bib
csl: evolution.csl
---
## Null hypothesis
```{r}
#| label: setup
#| echo: false
#| warning: false
#| message: false
library(tidyverse)
library(cowplot)
library(viridis)
library(tictoc)
ggplot2::theme_set(theme_cowplot(font_size = 18))
set.seed(6452730)
```
- The baseline expectation
- In statistics, often this is our expectation when only sampling and measurement error are causing variation
- This is the default hypothesis: we require evidence against it to reject it in favor of an alternative
- Hypotheses are never proven true
- Null is rejected or failed to be rejected
## Null Distribution
- We can evaluate evidence in the context of the null hypothesis if we have a null distribution for some parameter of interest
- How to get the null distribution
- Empirically
- Simulation
- From analytical solutions (mathematical formulas)
## When the Null Hypothesis is not only Sampling Error
- Detecting selection
- Null: genetic drift is driving differences
- Similarities and differences between species
- Null: common ancestry is causing similarity
- Patterns of species diveristy
- Null: random extinction, speciation, & dispersal events drive patterns
## Simulated Null Distribution
```{r}
set.seed(736902)
muH <- 1
muL <- 1 / 3
sd1 <- sd2 <- 1
n1 <- n2 <- 20
DD <- tibble(
growthR = c(rnorm(n1, muL, sd1),
rnorm(n2, muH, sd2)),
Temperature = c(rep("Low", times = n1),
rep("High", times = n2))
)
d <- DD |> group_by(Temperature) |>
summarize(xbar = mean(growthR)) |>
pivot_wider(names_from = Temperature, values_from = xbar) |>
mutate(d = High - Low) |>
pull(d)
mu_both <- mean(c(muL, muH))
nreps <- 1e4
diffs.s <- numeric(length = nreps)
for (ii in 1:nreps) {
diffs.s[ii] <- mean(rnorm(n1, mu_both, sd1)) -
mean(rnorm(n2, mu_both, sd2))
}
ps <- ggplot(data.frame(diffs.s), aes(diffs.s)) +
geom_histogram(binwidth = 0.1, fill = "gray75") +
geom_segment(x = d, xend = d,
y = 0, yend = Inf,
linewidth = 2,
color = "firebrick4") +
ylim(c(0, 1500)) +
xlim(c(-1.2, 1.2)) +
labs(x = "Difference (High - Low)", y = "Count")
ps
```
## Identifying Random Processes
- Define the experimental question
- What baseline random processes could produce a similar result?
## A Simple Example: Drift at One Variant in a Population
```{r}
#| echo: true
set.seed(8487264)
snpF <- 0.41
NN <- 100
pop <- tibble("C1" = rbinom(NN, 1, snpF),
"C2" = rbinom(NN, 1, snpF))
newF <- mean(c(pop$C1, pop$C2))
newF
```
## A Simple Example: Drift at One Variant in a Population
```{r}
#| echo: true
ngen <- 10
npops <- 10
snpG <- rep(snpF, npops)
output <- matrix(NA, ngen+1, npops)
output[1,] <- snpG
for(gg in 1:ngen){
snpG <- sapply(snpG, function(x) mean(rbinom(NN*2,1,x)))
output[(gg+1),] <- snpG
}
```
## A Simple Example: Drift at One Variant in a Population
```{r}
outputT <- as_tibble(output)
colnames(outputT) <- paste0("Pop",seq(1:npops))
outputT$Generation <- seq(1, (ngen+1))
outputT |>
pivot_longer(-Generation,names_to = "Population", values_to = "AlleleF") |>
ggplot(aes(Generation, AlleleF, color=Population, group=Population)) +
geom_point() +
geom_line()
```
## A Simple Example: Drift at One Variant in a Population
```{r}
#| echo: true
#| output-location: slide
ngen <- 10
npops <- 1000
snpG <- rep(snpF, npops)
output <- matrix(NA, ngen+1, npops)
output[1,] <- snpG
for(gg in 1:ngen){
snpG <- sapply(snpG, function(x) mean(rbinom(NN*2,1,x)))
output[(gg+1),] <- snpG
}
allD <- abs(apply(combn(output[(ngen + 1),],2), 2, diff))
allD |>
tibble() |>
ggplot(aes(allD)) +
geom_histogram(fill = "grey75") +
xlab("Allele Frequency Difference")
```
## Comparison to Sampling Error Only
```{r}
setF <- rbinom(npops,NN*2,snpF)/(NN*2)
seD <- abs(apply(combn(setF,2), 2, diff))
compD <- tibble("ID" = c(rep("SamplingError", length(seD)),
rep("Drift",length(allD))),
"Diffs" = c(seD,allD))
ggplot(compD, aes(x = Diffs, color = ID, fill = ID)) +
geom_histogram(alpha = 1/2) +
xlab("Allele Frequency Difference")
```
## Adding Complexity
- Random differences in reproductive success
- Replicate populations
- Many loci
- Other evolutionary models