-
Notifications
You must be signed in to change notification settings - Fork 0
/
WFsim.txt
41 lines (30 loc) · 1.06 KB
/
WFsim.txt
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
#WFsim.r simple simulations of the wright-Fisher model
# Initialize popsize (N), number of samples (samp), number of
# generations (ngen) and starting frequency (startfreq)
# Note this is for a haploid population of size N.
N<-200
nsamp<-8
ngen<-200
startfreq<-.5
pcur<-matrix(1:N)
#Wright-Fisher is simply recurrent binomial sampling over generations
x<-rbinom(nsamp,N,startfreq)
p<-x/N
pcur<-p
for (i in 1:ngen){
x<-rbinom(nsamp,N,p)
p<-x/N
pcur<-rbind(pcur,p)
}
#Now plot these trajectories
gen<-seq(1,ngen+1)
for (k in 1:nsamp){
plot(gen,pcur[,k],type="l",ylim=c(0,1))
par(new=TRUE)
}
# ASSIGNMENT 1: Conduct runs of the above simulation, and calculate the variance
# in allele frequencies across 20 independent sample populations for each of
# generations 20 through 200 in steps of 20. Plot the results.
# Hint: the variance of allele freq at gen i is var(pcur[i,])
# ASSIGNMENT 2: Plot the expected time to fixation (when allele frequency is
# either zero or one) for 20 populations with population sizes 50, 100, 150 and 200.