# Generate piFuns and check their behaviour:
# makeRemPiFun
# ============
( pRem <- matrix(0.4, nrow=5, ncol=3) )
myPi <- makeRemPiFun(times=c(2,3,5))
myPi(pRem)
ls(environment(myPi)) # See what's in the environment
environment(myPi)$times
( pRem <- matrix(runif(15), 5, 3) )
myPi(pRem)
myPi <- makeRemPiFun(c(5,3,2))
environment(myPi)$times
myPi(pRem)
# More than 3 occasions
myPi <- makeRemPiFun(c(1,2,3,5))
try(myPi(pRem)) # Error
( pRem <- matrix(runif(20), 5, 4) )
myPi(pRem)
# Probability of escaping detection
1 - rowSums(myPi(pRem))
# makeCrPiFun
# ===========
p <- matrix(0.4, 2, 3)
myPi <- makeCrPiFun(3)
myPi(p)
myPi # Look at the function
ls(environment(myPi))
environment(myPi)$histories
p <- matrix(runif(6, 0.1, 0.9), 2, 3) # different p's everywhere
myPi(p)
p <- matrix(runif(4*5, 0.1, 0.9), 4, 5) # > 3 occasions
try(myPi(p)) # Error
myPi <- makeCrPiFun(5)
( tmp <- myPi(p) )
1 - rowSums(tmp) # Probability of non-capture
# makeCrPiFunMb
# ==============
( pMb <- cbind(rep(0.7, 5), 0.3, NA) )
myPi <- makeCrPiFunMb(3)
myPi(pMb)
( pMb <- matrix(runif(15), 5, 3) ) # col #3 will be ignored
myPi(pMb)
# with > 3 occasions
( pMb <- matrix(runif(15), 3, 5) )
try(myPi(pMb))
myPi <- makeCrPiFunMb(5)
myPi(pMb)
# makeCrPiFunMh
# =============
pMh <- cbind(rep(0.4, 5), NA, NA)
pMh[1, 2] <- 0.3
pMh
myPi <- makeCrPiFunMh(3)
myPi(pMh)
pMh <- cbind(runif(5), NA, NA)
pMh[1, 2] <- 0.3
pMh
myPi(pMh)
# with > 3 occasions
pMh <- cbind(runif(5), NA, NA, NA, NA)
pMh[1, 2] <- 0.3
pMh
try(myPi(pMh))
myPi <- makeCrPiFunMh(5)
1 - rowSums(myPi(pMh)) # Probability of non-detection
Run the code above in your browser using DataLab