Skip to content

Instantly share code, notes, and snippets.

@indapa
Created March 22, 2011 18:06
Show Gist options
  • Save indapa/881702 to your computer and use it in GitHub Desktop.
Save indapa/881702 to your computer and use it in GitHub Desktop.
simulate Wright-Fisher model in R
#simulate Wright Fisher model
#see this for more info: http://www.genetics.wustl.edu/bio5488/lecture_notes_2004/popgen_1.pdf
wright_fisher <- function(N,m,j) {
#Preliminaries
#number of chromosomes
N=N
#number of generations
m = m
x = numeric(m)
#number of minor alleles in the first generation
x[1] = j
#Simulation
#binomial sampling at each generation - determines allele count in next generations
for (i in 2:m)
{
k=(x[i-1])/N
n=seq(0,N,1)
prob=dbinom(n,N,k)
x[i]=sample(0:N, 1, prob=prob)
}
plot(x[1:m], type="o", pch=19)
}
@atefero
Copy link

atefero commented Jan 4, 2022

@indapa Thanks. Could you please simulate the Wright-Fisher model in MATLAB as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment