# Functions used to simulate Lincoln-Petersen Estimates of Population Size

lp.est<-function(N,n1,n2){
# Create a population of n1 marked individuals and N-n1 unmarked individuals 
# for the second sampling occassion
  pop.data<-c(rep("M",n1), rep("U", N-n1)) 

# Take a sample of n2 individuals (without replacement) from this population  
  samp.data<-sample(pop.data, size=n2, replace=FALSE) 

# Estimate p
  m2<-sum(samp.data=="M")
  N.hat<-(n1+1)*(n2+1)/(m2+1)-1
  return(N.hat)
}

lp.est.het<-function(N,n1,n2, ps){
# Create a population of n1 marked individuals and N-n1 unmarked individuals 
# for the second sampling occassion
  pop.data<-c(rep("M",n1), rep("U", N-n1)) 

# Take a sample of n2 individuals (without replacement) from this population  
# ps = relative probability of capturing marked and unmarked individuals.  
# For example: ps = c(2,1) suggests that marked individuals are twice as likely to 
# be captured as unmarked individuals on the second sampling occassion.
  samp.data<-sample(pop.data, size=n2, replace=FALSE, prob=c(rep(ps[1],n1), rep(ps[2],N-n1))) 

# Estimate p
  m2<-sum(samp.data=="M")
  N.hat<-(n1+1)*(n2+1)/(m2+1)-1
  return(N.hat)
}