Skip to content

Commit

Permalink
update to selection to allow multiple conditions to be plotted on the…
Browse files Browse the repository at this point in the history
… same graph
  • Loading branch information
liamrevell committed Feb 13, 2018
1 parent a1fd25f commit b2e82d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
@@ -1,6 +1,6 @@
Package: PopGen
Version: 0.4
Date: 2017-10-06
Version: 0.5
Date: 2018-02-13
Title: Population genetic simulations & numerical analysis
Author: Liam J. Revell
Maintainer: Liam J. Revell <liam.revell@umb.edu>
Expand All @@ -11,6 +11,6 @@ ZipData: no
Description: conducts various numerical analyses and simulations in population genetics and evolutionary theory.
License: GPL (>= 2)
URL: http://github.com/liamrevell/PopGen
Packaged: 2017-10-06 21:00:00 EST
Packaged: 2018-02-13 12:00:00 EST
Repository:
Date/Publication: 2017-10-06 21:00:00 EST
Date/Publication: 2018-02-13 12:00:00 EST
39 changes: 22 additions & 17 deletions R/popgen.R
@@ -1,48 +1,53 @@
# functions by Liam Revell 2012 (some small updates 2017)
# functions by Liam Revell 2012 (some small updates 2017, 2018)

selection<-function(p0=0.01,w=c(1.0,0.9,0.8),time=100,show="p",pause=0){
selection<-function(p0=0.01,w=c(1.0,0.9,0.8),time=100,show="p",pause=0,...){
if(hasArg(add)) add<-list(...)$add
else add<-FALSE
if(hasArg(color)) color<-list(...)$color
else color<-"black"
if(show=="surface"){
p<-0:100/100
wbar<-p^2*w[1]+2*p*(1-p)*w[2]+(1-p)^2*w[3]
plot(p,wbar,type="l",ylim=c(0,1),main="mean fitness")
plot(p,wbar,type="l",ylim=c(0,1),main="mean fitness",col=color)
}
else if(show=="deltap"){
p<-0:100/100
wbar<-p^2*w[1]+2*p*(1-p)*w[2]+(1-p)^2*w[3]
deltap<-(p/wbar)*(p*w[1]+(1-p)*w[2]-wbar)
plot(p,deltap,type="l",main="delta p")
plot(p,deltap,type="l",main="delta p",col=color)
lines(c(0,1),c(0,0),lty=2)
} else {
if(show=="cobweb"){
p<-0:100/100
wbar<-p^2*w[1]+2*p*(1-p)*w[2]+(1-p)^2*w[3]
p2<-(p/wbar)*(p*w[1]+(1-p)*w[2]-wbar)+p
plot(p,p2,type="l",xlab="p(t)",ylab="p(t+1)")
plot(p,p2,type="l",xlab="p(t)",ylab="p(t+1)",col=color)
lines(c(0,1),c(0,1),lty=2)
dev.flush()
}
p<-wbar<-vector(); p[1]<-p0
p<-wbar<-vector()
p[1]<-p0
wbar[1]<-p[1]^2*w[1]+2*p[1]*(1-p[1])*w[2]+(1-p[1])^2*w[3]
for(i in 2:time){
p[i]<-p[i-1]
p[i]<-(p[i]^2*w[1]+p[i]*(1-p[i])*w[2])/wbar[i-1]
wbar[i]<-p[i]^2*w[1]+2*p[i]*(1-p[i])*w[2]+(1-p[i])^2*w[3]
ii<-(i-1):i
if(show=="p"){
if(i==2) plot(1:i,p,type="l",xlim=c(0,time),ylim=c(0,1),xlab="time",
main="frequency of A")
else lines(ii,p[ii],type="l")
} else if(show=="q"){
if(i==2 && !add) plot(1:i,p,type="l",xlim=c(0,time),ylim=c(0,1),xlab="time",
main="frequency of A",col=color)
else lines(ii,p[ii],type="l",col=color)
} else if(show=="q" && !add){
if(i==2) plot(1:i,1-p,type="l",xlim=c(0,time),ylim=c(0,1),xlab="time",
ylab="q",main="frequency of a")
else lines(ii,1-p[ii],type="l")
} else if(show=="fitness"){
ylab="q",main="frequency of a",col=color)
else lines(ii,1-p[ii],type="l",col=color)
} else if(show=="fitness" && !add){
if(i==2) plot(1:i,wbar/max(w),type="l",xlim=c(0,time),ylim=c(0,1),
xlab="time",main="mean fitness")
else lines(ii,wbar[ii]/max(w),type="l")
xlab="time",main="mean fitness",col=color)
else lines(ii,wbar[ii]/max(w),type="l",col=color)
} else if(show=="cobweb"){
lines(c(p[i-1],p[i-1]),c(p[i-1],p[i]))
lines(c(p[i-1],p[i]),c(p[i],p[i]))
lines(c(p[i-1],p[i-1]),c(p[i-1],p[i]),col=color)
lines(c(p[i-1],p[i]),c(p[i],p[i]),col=color)
} else {
message("not a recognized option")
break
Expand Down
3 changes: 2 additions & 1 deletion man/selection.Rd
Expand Up @@ -2,14 +2,15 @@
\alias{selection}
\title{Numerical analysis of biallelic locus frequency independent selection}
\usage{
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,...)
}
\arguments{
\item{p0}{Starting frequency for the A allele.}
\item{w}{Fitnesses for the three genotypes in the following order: AA, Aa, aa.}
\item{time}{Number of generations to run the analysis.}
\item{show}{Various options for plotting. \code{"p"} shows the frequency of A through time; \code{"surface"} plots the mean fitness as a function of p; \code{"deltap"} shows the change in p as a function of p; \code{"cobweb"} creates a cobweb plot showing p(t) by p(t+1). The default is \code{show="p"}.}
\item{pause}{Pause between generations. \code{pause=0.01} (for instance) might smooth animation.}
\item{...}{optional arguments.}
}
\description{
This function performs numerical analysis of a simple biallelic selection model.
Expand Down

0 comments on commit b2e82d2

Please sign in to comment.