Title: | Population Genetic Simulations & Numerical Analysis |
---|---|
Description: | Conducts various numerical analyses and simulations in population genetics and evolutionary theory, primarily for the purpose of teaching (and learning about) key concepts in population & quantitative genetics, and evolutionary theory. |
Authors: | Liam J. Revell |
Maintainer: | Liam J. Revell <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0.6 |
Built: | 2024-10-31 16:33:08 UTC |
Source: | https://github.com/liamrevell/learnpopgen |
The Central Limit Theorem tells us that when independent random variables are added together, the distribution of their sum tends towards a normal distribution, regardless of the shape of their individual distributions. This function attempts to illustrate this concept by allowing the user to visualize the sum of an arbitrary number of different independent random variables with different underlying distributions.
clt(nvar=1, nobs=1000, df=c("normal","uniform","exponential","binomial"), theta=NULL, breaks="Sturges", show=c("sum","mean")) ## S3 method for class 'clt' print(x, ...) ## S3 method for class 'clt' plot(x, ...)
clt(nvar=1, nobs=1000, df=c("normal","uniform","exponential","binomial"), theta=NULL, breaks="Sturges", show=c("sum","mean")) ## S3 method for class 'clt' print(x, ...) ## S3 method for class 'clt' plot(x, ...)
nvar |
number of random variables to sum (1 or more). |
nobs |
total number of observations (per random variable). |
df |
distribution functions of individual random variables to sum. These can be |
theta |
parameter of the distribution functions: variance in the case of |
breaks |
breaks (see |
show |
whether to show the row-wise sum of the independent random variables ( |
x |
object of class |
... |
optional arguments for |
The central limit theorem (CLT) establishes that when independent random variables are added together their (normalized) sum will tend towards a normal distribution, regardless of the distribution of the original random variables. That is to say if we were to generate a set of nvar
(say) independent, uniform, random variables, normalize each one to have the same variance, and then sum or average the variables by observation, this sum or average will tend towards a normal distribution as the number of random variables (nvar
in this function) is increased.
Creates a plot showing the observation-wise distribution of the sum or average of the independent random variables.
The distribution of the observation-wise sum or average and the underlying data are also returned invisibly to the user in the form of an object of class "clt"
. This object can in turn be printed or re-plotted using custom print
and plot
methods. (See examples.)
Liam Revell [email protected]
clt(nvar=1,df="exponential") clt(nvar=10,df="exponential") object<-clt(nvar=40,df="exponential") print(object) plot(object)
clt(nvar=1,df="exponential") clt(nvar=10,df="exponential") object<-clt(nvar=40,df="exponential") print(object) plot(object)
Coalescence or coalescent theory is a model for genetic drift within a population in which we envision gene copies merging or "coalescing" into ancestors in the past. This function generates a(n) (optionally animated) visualization of this process of coalescence within a population.
coalescent.plot(n=10, ngen=20, colors=NULL, ...) ## S3 method for class 'coalescent.plot' print(x, ...) ## S3 method for class 'coalescent.plot' plot(x, ...)
coalescent.plot(n=10, ngen=20, colors=NULL, ...) ## S3 method for class 'coalescent.plot' print(x, ...) ## S3 method for class 'coalescent.plot' plot(x, ...)
n |
number of haploid individuals or gene copies. |
ngen |
number of generations. |
colors |
colors to use for plotting individuals and lines. By default, the function tries to use a contrasting color scheme such that adjacent allele copies are dissimilar (to facilitate visualization of the coalescent process.) |
x |
object of class |
... |
optional arguments. For |
Creates a plot or animation.
Invisibly returns an object of class "coalescent.plot"
containing the alleles (coded numerically) and the parent-offspring relationships from the coalescent simulation. This object can be printed or re-plotted using print
and plot
methods. (See examples.)
Liam Revell [email protected]
drift.selection
, genetic.drift
coalescent.plot() ## Not run: coalescent.plot(n=20,ngen=30,col.order="alternating") object<-coalescent.plot() print(object) plot(object) ## End(Not run)
coalescent.plot() ## Not run: coalescent.plot(n=20,ngen=30,col.order="alternating") object<-coalescent.plot() print(object) plot(object) ## End(Not run)
Simulates drift and natural selection at a single biallelic locus within one or various populations.
drift.selection(p0=0.5, Ne=100, w=c(1,1,1), ngen=400, nrep=10, colors=NULL, ...)
drift.selection(p0=0.5, Ne=100, w=c(1,1,1), ngen=400, nrep=10, colors=NULL, ...)
p0 |
starting frequency for the A allele. |
Ne |
effective population size. |
w |
fitnesses of the three genotypes: AA, Aa, and aa. |
nrep |
number of replicate simulations. |
ngen |
total time, in number of generations, for the simulation. |
colors |
colors to use for plotting. |
... |
optional arguments. Presently the only arguments are |
The function creates a plot and returns an object of class "drift.selelction"
consisting of list containing the allele frequency through time for each simulation. This object can be printed or plotted using corresponding methods. (See examples.)
Liam Revell [email protected]
drift.selection() p<-drift.selection(p0=0.01,Ne=100,w=c(1,0.9,0.8),ngen=200,nrep=5) print(p) plot(p)
drift.selection() p<-drift.selection(p0=0.01,Ne=100,w=c(1,0.9,0.8),ngen=200,nrep=5) print(p) plot(p)
This function simulates genetic drift with a founding event / population bottleneck at time etime
.
founder.event(p0=0.5, Ne=1000, Nf=10, ttime=100, etime=50, show="p", ...)
founder.event(p0=0.5, Ne=1000, Nf=10, ttime=100, etime=50, show="p", ...)
p0 |
Starting frequency for the A allele. |
Ne |
Effective population size at the start of the simulation and after the founding event. |
Nf |
Size of the founding population. |
ttime |
Total time of the simulation. |
etime |
Time for the founding event. Can either be a single generation, or a sequence of generations (e.g., |
show |
Two different options for plotting. |
... |
optional arguments. Presently, the only optional argument in |
The function creates one of two different plots, depending on the value of show
.
The function also invisibly returns an object of class "founder.event"
which can be printed or plotted using corresponding print
and plot
methods. (See examples.)
Liam Revell [email protected]
drift.selection
, genetic.drift
founder.event() p<-founder.event(show="variation") print(p) plot(p,show="p",ltype="l")
founder.event() p<-founder.event(show="variation") print(p) plot(p,show="p",ltype="l")
This function performs numerical analysis of a frequency dependent selection model based on Rice (2004; Evolutionary Theory: Mathematical & Conceptual Foundations). The fitnesses of the three genotypes in the model are as follows, where f(XX) denotes the frequency of the XX genotype: w(AA)=1-3*f(Aa)+3*f(aa); w(Aa)=1-s*f(Aa); and w(aa)=1-3*f(Aa)+3*f(AA). As shown in Rice (2004), though entirely deterministic, the model can exhibit chaotic behavior under some values for s
.
freqdep(p0=0.01, s=0, time=100, show="p", pause=0, ...)
freqdep(p0=0.01, s=0, time=100, show="p", pause=0, ...)
p0 |
Starting frequency for the A allele. |
s |
Parameter that determines the strength of selection against heterozygotes when they are common. |
time |
Number of generations to run the analysis. |
show |
Various options for plotting. |
pause |
Pause between generations. |
... |
optional arguments. Presently, the only optional argument in |
The function creates one of several possible plots, depending on the value of show
.
The use of cobweb plots follows selection
.
The function also invisibly returns an object of class "freqdep"
containing the frequency of the allele A through time, if this was calculated by the selected method. This can be printed or plotted using the corresponding methods. (See examples.)
Liam Revell [email protected]
freqdep(time=100) freqdep(s=1.5,time=100) p<-freqdep(s=2,show="cobweb",time=100) plot(p)
freqdep(time=100) freqdep(s=1.5,time=100) p<-freqdep(s=2,show="cobweb",time=100) plot(p)
This function simulates genetic drift at a biallelic genetic locus with no selection and no mutation in a sexually reproducing diploid population or set of populations. It is essentially redundant with drift.selection
, but in which there is no difference in relative fitness among genotypes; however, it also allows the user to visualize heterozygosity or genetic variation through time - options that are not yet implemented in drift.selection
.
genetic.drift(p0=0.5, Ne=20, nrep=10, time=100, show="p", pause=0.1, ...)
genetic.drift(p0=0.5, Ne=20, nrep=10, time=100, show="p", pause=0.1, ...)
p0 |
Starting frequency for the A allele. |
Ne |
Effective population size. |
nrep |
Number of replicate simulations. |
time |
Total time, in number of generations, for the simulation. |
show |
Various options for plotting. |
pause |
Pause between generations. |
... |
optional arguments. In |
The function creates one of several possible plots, depending on the value of show
.
The function also invisibly returns an object of class "genetic.drift"
that can be printed or re-plotted by the user using corresponding print
and plot
methods. (See examples.)
Liam Revell [email protected]
drift.selection
, founder.event
, selection
## Not run: genetic.drift() object<-genetic.drift(p0=0.7,show="heterozygosity") plot(object,show="genotypes") ## End(Not run)
## Not run: genetic.drift() object<-genetic.drift(p0=0.7,show="heterozygosity") plot(object,show="genotypes") ## End(Not run)
hardy.weinberg
computes Hardy-Weinberg frequencies for a multiallelic locus using arbitrary allele frequencies.
multilocus.hw
computes multilocus Hardy-Weinberg frequencies for a set of biallelic loci.
hardy.weinberg(p=c(0.5,0.5), alleles=c("A","a"), as.matrix=FALSE) multilocus.hw(nloci=2, p=NULL)
hardy.weinberg(p=c(0.5,0.5), alleles=c("A","a"), as.matrix=FALSE) multilocus.hw(nloci=2, p=NULL)
p |
allele frequencies. In the case of |
alleles |
names of the alleles. |
as.matrix |
logical argument indicating whether to return the result in the form of a matrix (if |
nloci |
for |
Returns a matrix or vector.
Liam Revell [email protected]
hardy.weinberg() hardy.weinberg(p=c(0.4,0.3,0.2,0.1),alleles=letters[1:4])
hardy.weinberg() hardy.weinberg(p=c(0.4,0.3,0.2,0.1),alleles=letters[1:4])
This function performs numerical analysis of a discrete-time hawk-dove model in which "payoff" determines relative fitness in the population.
hawk.dove(p=c(0.01,0.99), M=NULL, time=100)
hawk.dove(p=c(0.01,0.99), M=NULL, time=100)
p |
Starting frequency of hawk & dove phenotypes, respectively. Should correspond with the rows of |
M |
Payoff matrix. |
time |
Number of generations. |
The function creates a two panel plot. The upper panel shows the relative frequencies of each of the two interacting phenotypes. The lower panel shows mean fitness of the population and of each morph through time.
The function also invisibly returns an object of class "hawk.dove"
containing the frequencies of each strategy through time and their fitnesses. This object can be printed or re-plotted using corresponding print
and plot
methods. (See examples.)
Liam Revell [email protected]
hawk.dove(time=60) Payoff<-matrix(c(0.5,0.6,1.5,1.0),2,2) object<-hawk.dove(M=Payoff,time=60) print(object) plot(object)
hawk.dove(time=60) Payoff<-matrix(c(0.5,0.6,1.5,1.0),2,2) object<-hawk.dove(M=Payoff,time=60) print(object) plot(object)
Simulates migration, natural selection, and genetic drift. Selection can be in opposite directions in the two populations experiencing gene flow.
msd(p0=c(0.5,0.5), Ne=c(100,100), w=list(c(1,1,1),c(1,1,1)), m=c(0.01,0.01), ngen=400, colors=c("red","blue"), ...)
msd(p0=c(0.5,0.5), Ne=c(100,100), w=list(c(1,1,1),c(1,1,1)), m=c(0.01,0.01), ngen=400, colors=c("red","blue"), ...)
p0 |
starting frequency for the A allele in each of two populations. |
Ne |
effective population size for each of two populations. |
w |
fitnesses of the three genotypes (AA, Aa, and aa, in that order) in each of the two populations. |
m |
rates of migration from the first population to the second, and from the second population to the first, in that order. This value is best interpreted as the probability that an individual born in population 1 will migrate to population 2 before reproduction, and vice versa. |
ngen |
total time, in number of generations, for the simulation. |
colors |
colors to use for plotting. |
... |
optional arguments. Presently, the only optional argument for |
The function creates a plot and invisibly returns a list containing the allele frequency through time for each of the two simulated populations.
The returned object is of class "msd"
and can be printed or re-plotted using corresponding print
or plot
methods. (See examples.)
Liam Revell [email protected]
msd() msd(p0=c(0.25,0.75),w=list(c(1,0.9,0.8),c(0.8,0.9,1))) object<-msd(p0=c(0.75,0.25),w=list(c(1,0.9,0.8), c(0.8,0.9,1)),m=c(0.1,0.1),ngen=100) print(object) plot(object,colors=c("black","grey"),lwd=4,type="s")
msd() msd(p0=c(0.25,0.75),w=list(c(1,0.9,0.8),c(0.8,0.9,1))) object<-msd(p0=c(0.75,0.25),w=list(c(1,0.9,0.8), c(0.8,0.9,1)),m=c(0.1,0.1),ngen=100) print(object) plot(object,colors=c("black","grey"),lwd=4,type="s")
This function performs numerical analysis of mutation-selection balance with mutation from A to a and selection against (either or both of) Aa and aa.
mutation.selection(p0=1.0, w=c(1,0), u=0.001, time=100, show="q", pause=0, ylim=c(0,1))
mutation.selection(p0=1.0, w=c(1,0), u=0.001, time=100, show="q", pause=0, ylim=c(0,1))
p0 |
Starting frequency for the A allele. |
w |
Fitnesses of the heterozygote (Aa) and homozygote deleterious (aa) genotypes. The fitness of genotype AA is assumed to be 1.0. |
u |
Rate at which A alleles are converted to a alleles by mutation. |
time |
Number of generations to run the analysis. |
show |
Two options for plotting. |
pause |
Pause between generations. |
ylim |
Limits on the y-axis for plotting. |
The function creates one of three possible plots, depending on the value of show
.
The function also invisibly returns the frequency of the A allele through time and the mean population fitness as an object of class "mutation.selection"
that can be printed or re-plotted with associated print
and plot
methods, respectively. The plot
method also permits user control over various attributes of the appearance of the plot, such as the color of the plotted lines (color
), the line widths (lwd
), the limits of the y-axis (ylim
), and the type of line (e.g., "l"
vs. "s"
, via the argument type
).
Liam Revell [email protected]
mutation.selection(w=c(1,0),time=100,ylim=c(0,0.1))
mutation.selection(w=c(1,0),time=100,ylim=c(0,0.1))
phenotype.freq
computes the phenotypic trait distribution for a polygenic trait. Can be used to demonstrate that the phenotypic distribution of a polygenic trait will tend to normality as the number of loci is increased, regardless of the allele frequencies at each locus.
phenotype.selection
computes the change in the phenotypic trait distribution through time under natural selection. Can be used to show that natural selection on a polygenic trait can move the value of the trait well beyond its original distribution in the population.
phenotype.freq(nloci=6, p=NULL, effect=1/nloci) phenotype.selection(nloci=6, p=NULL, effect=1/nloci, beta=0.1, ngen=20, ...)
phenotype.freq(nloci=6, p=NULL, effect=1/nloci) phenotype.selection(nloci=6, p=NULL, effect=1/nloci, beta=0.1, ngen=20, ...)
nloci |
number of loci. For simplicity all loci are assumed to be biallelic. |
p |
allele frequency, p, for each locus, in a vector. If not supplied, initially frequencies will be assumed to be 0.5 at all loci. |
effect |
additive effect of an allele substitution. For simplicity, this is assumed to be the same at all loci. |
beta |
selection gradient. |
ngen |
number of generations to analyze. |
... |
optional arguments. Presently the only optional argument in the function |
Creates a plot or animation.
phenotype.freq
also invisibly returns an object of class "phenotype.freq"
that can be printed or re-plotted using print
and plot
methods corresponding to the object type. (See examples.)
Liam Revell [email protected]
## Not run: phenotype.freq(n=4) object<-phenotype.freq(nloci=6,p=runif(n=6),effect=1/6) print(object) plot(object) object<-phenotype.freq(nloci=10,p=runif(n=10),effect=rexp(n=10)) print(object) plot(object) phenotype.selection(ngen=100) ## End(Not run)
## Not run: phenotype.freq(n=4) object<-phenotype.freq(nloci=6,p=runif(n=6),effect=1/6) print(object) plot(object) object<-phenotype.freq(nloci=10,p=runif(n=10),effect=rexp(n=10)) print(object) plot(object) phenotype.selection(ngen=100) ## End(Not run)
This function conducts individual-based, genetically explicit numerical simulation of reproductive character displacement in an ecological community. The model is one of multiple species (with fixed relative abundance) competing to utilize the same signal space. There is both stabilizing selection on the signal trait for detectability, as well as (in multi-species simulations) countervailing selection for divergence due to the costs of erroneous mismating attempts.
rcd(nsp=3, nindivs=c(700,400,100), w_t=10, gen=c(500,500), figs="on", pf=100, ...)
rcd(nsp=3, nindivs=c(700,400,100), w_t=10, gen=c(500,500), figs="on", pf=100, ...)
nsp |
Number of species in the simulation. If |
nindivs |
A vector of length |
w_t |
Shape parameter of the Gaussian selection surface for the male signalling trait. |
gen |
Vector containing the number of allopatric generations followed by the number of sympatric generations for simulation. |
figs |
Either |
pf |
Print frequency for the simulation status to screen. |
... |
Optional arguments. |
The function returns a list containing the mean male signal trait and the mean female preference over time. It also (optionally) plots these.
Liam Revell [email protected]
drift.selection
, genetic.drift
, freqdep
, selection
## Not run: obj<-rcd(nsp=2,nindivs=c(500,500)) ## End(Not run)
## Not run: obj<-rcd(nsp=2,nindivs=c(500,500)) ## End(Not run)
This function performs numerical analysis of a simple biallelic selection model.
selection(p0=0.01, w=c(1.0,0.9,0.8), time=100, show="p", pause=0, ...)
selection(p0=0.01, w=c(1.0,0.9,0.8), time=100, show="p", pause=0, ...)
p0 |
Starting frequency for the A allele. |
w |
Fitnesses for the three genotypes in the following order: AA, Aa, aa. |
time |
Number of generations to run the analysis. |
show |
Various options for plotting. |
pause |
Pause between generations. |
... |
Optional arguments, including: |
The function creates one of several possible plots, depending on the value of show
.
The cobweb plot shows p(t+1) as a function of p(t), with stairsteps giving the changes across generations given the initial value of p (p0
) and total time (time
) that are specified by the user.
The function invisibly returns an object of class "selection"
which can be printed or re-plotted using associated print
and plot
methods. (See examples.)
Liam Revell [email protected]
drift.selection
, freqdep
, msd
, mutation.selection
selection(w=c(1.0,0.8,0.8),time=500) selection(w=c(1.0,1.0,0.0),show="surface") object<-selection(w=c(0.8,1.1,0.7)) print(object) plot(object,show="cobweb")
selection(w=c(1.0,0.8,0.8),time=500) selection(w=c(1.0,1.0,0.0),show="surface") object<-selection(w=c(0.8,1.1,0.7)) print(object) plot(object,show="cobweb")
This function performs numerical analysis of a frequency dependent selection model of a hypothetical diploid sexually reproducing population in which sex is determined by the genotype at a biallelic genetic locus. Genotype AA are male, genotype aa are female, and genotype Aa might be male or female with probabilities that can be specified by the user. (Users may find, for instance, that setting sex.Aa=c(1,0)
will result in evolution towards an XY sex determination system; whereas sex.Aa=c(0,1)
will evolve towards a ZW system.)
sexratio(p0=0.01, time=40, show="p", pause=0, sex.Aa=c(0.5,0.5))
sexratio(p0=0.01, time=40, show="p", pause=0, sex.Aa=c(0.5,0.5))
p0 |
Starting frequency for the A allele. Individuals with AA genotypes are male, while individuals with Aa genotypes are male or female with probability given by |
time |
Number of generations to run the analysis. |
show |
Two different options for plotting. |
pause |
Pause between generations. |
sex.Aa |
Probability that individuals with genotype Aa are male or female, respectively. |
The function creates one of four possible plots, depending on the value of show
. Numerical analysis of this model shows how frequency dependent selection should favor alleles that tend to produce the rarer sex in the population.
The function invisibly returns an object of class "sexratio"
that can be printed or re-plotted by the user. (See examples.)
Liam Revell [email protected]
sexratio() sexratio(p0=0.001,show="sex-ratio") sexratio(p0=0.001,show="fitness") object<-sexratio(p0=0.001,sex.Aa=c(0.9,0.1), time=20) print(object) par(mfrow=c(2,1)) plot(object,lwd=4,type="s",show="sex-ratio") plot(object,lwd=4,type="s",show="genotypes") par(mfrow=c(1,1))
sexratio() sexratio(p0=0.001,show="sex-ratio") sexratio(p0=0.001,show="fitness") object<-sexratio(p0=0.001,sex.Aa=c(0.9,0.1), time=20) print(object) par(mfrow=c(2,1)) plot(object,lwd=4,type="s",show="sex-ratio") plot(object,lwd=4,type="s",show="genotypes") par(mfrow=c(1,1))