These are factory functions that generate piFuns with the required defaults, which are enclosed within the environment of the piFun. See the main entry for piFuns.

makeRemPiFun(times)
makeCrPiFun(nOcc)
makeCrPiFunMb(nOcc)
makeCrPiFunMh(nOcc)

Arguments

times

a vector of times for each interval, length(times) is the number of survey occasions; can be all 1's if times are the same.

nOcc

the number of survey occasions

Details

makeRemPiFun produces a piFun for a removal model with the required number of occasions and potentially varying time intervals. The input to the piFun must be probabilities per unit time. This is a generalisation of the piFun in the Examples section of piFuns.

makeCrPiFun produces a piFun for a standard capture-recapture model, M0, Mt or Mx. Probabilities of detection may vary across occasions. See Kery & Royle (2016) section 7.8.1.

makeCrPiFunMb produces a piFun for a capture-recapture model with a behavioral response after the first capture, Mb. Probabilities of detection are constant across occasions. The first column is the probability of detection for animals not caught before, column #2 is for animals after the first capture. The remaining columns are ignored. See Kery & Royle (2016) section 7.8.2.

makeCrPiFunMh produces a piFun for a capture-recapture model with individual heterogeneity in detection probability, Mh, using a logit-normal distribution. Probabilities of detection are constant across occasions. The first column is the mean of the logit-normal on the probability scale. Cell p[1, 2] is a value in [0, 1] which controls the spread of the distribution. The remaining cells are ignored. See Kery & Royle (2016) section 7.8.3.

Value

A piFun with the appropriate defaults.

References

Kery, M., Royle, J. A. (2016) Applied Hierarchical Modeling in Ecology Vol 1.

Examples

# Generate piFuns and check their behaviour:

# makeRemPiFun
# ============
( pRem <- matrix(0.4, nrow=5, ncol=3) )
#>      [,1] [,2] [,3]
#> [1,]  0.4  0.4  0.4
#> [2,]  0.4  0.4  0.4
#> [3,]  0.4  0.4  0.4
#> [4,]  0.4  0.4  0.4
#> [5,]  0.4  0.4  0.4
myPi <- makeRemPiFun(times=c(2,3,5))
myPi(pRem)
#>      [,1]    [,2]       [,3]
#> [1,] 0.64 0.28224 0.07171338
#> [2,] 0.64 0.28224 0.07171338
#> [3,] 0.64 0.28224 0.07171338
#> [4,] 0.64 0.28224 0.07171338
#> [5,] 0.64 0.28224 0.07171338
ls(environment(myPi))  # See what's in the environment
#> [1] "instRemPiFun" "times"       
environment(myPi)$times
#> [1] 2 3 5

( pRem <- matrix(runif(15), 5, 3) )
#>           [,1]      [,2]      [,3]
#> [1,] 0.6637637 0.5758327 0.6403992
#> [2,] 0.5244821 0.3480286 0.2322364
#> [3,] 0.3729590 0.6832623 0.7975949
#> [4,] 0.9435243 0.4058678 0.5724019
#> [5,] 0.1423031 0.4589708 0.7250769
myPi(pRem)
#>           [,1]        [,2]         [,3]
#> [1,] 0.8869451 0.104427050 0.0085759318
#> [2,] 0.7738827 0.163453100 0.0459472419
#> [3,] 0.6068196 0.380686735 0.0124894616
#> [4,] 0.9968105 0.002520589 0.0006593558
#> [5,] 0.2643560 0.619142977 0.1163180616

myPi <- makeRemPiFun(c(5,3,2))
environment(myPi)$times
#> [1] 5 3 2
myPi(pRem)
#>           [,1]         [,2]         [,3]
#> [1,] 0.9957024 3.969601e-03 2.855596e-04
#> [2,] 0.9756872 1.757494e-02 2.766143e-03
#> [3,] 0.9030652 9.385461e-02 2.954012e-03
#> [4,] 0.9999994 4.540331e-07 9.846123e-08
#> [5,] 0.5358383 3.906542e-01 6.795155e-02

# More than 3 occasions
myPi <- makeRemPiFun(c(1,2,3,5))
try(myPi(pRem))  # Error
#> Error : You have 3 occasions, but piFun expects 4.
( pRem <- matrix(runif(20), 5, 4) )
#>            [,1]      [,2]      [,3]      [,4]
#> [1,] 0.02888708 0.5003932 0.4580120 0.6708692
#> [2,] 0.72325781 0.6464060 0.6268303 0.1121477
#> [3,] 0.62242657 0.8807434 0.7629524 0.4150688
#> [4,] 0.48111159 0.6747469 0.5870138 0.8419171
#> [5,] 0.51192018 0.8137893 0.7388630 0.6318430
myPi(pRem)
#>            [,1]      [,2]        [,3]         [,4]
#> [1,] 0.02888708 0.7287164 0.203804717 3.844279e-02
#> [2,] 0.72325781 0.2421415 0.032802667 8.060690e-04
#> [3,] 0.62242657 0.3722035 0.005298372 6.662969e-05
#> [4,] 0.48111159 0.4639954 0.051026427 3.866165e-03
#> [5,] 0.51192018 0.4711559 0.016622507 2.993351e-04
# Probability of escaping detection
1 - rowSums(myPi(pRem))
#> [1] 1.490521e-04 9.919918e-04 4.897749e-06 3.817222e-07 2.038310e-06

# makeCrPiFun
# ===========
p <- matrix(0.4, 2, 3)
myPi <- makeCrPiFun(3)
myPi(p)
#>        001   010   011   100   101   110   111
#> [1,] 0.144 0.144 0.096 0.144 0.096 0.096 0.064
#> [2,] 0.144 0.144 0.096 0.144 0.096 0.096 0.064
myPi  # Look at the function
#> function (p) 
#> {
#>     if (ncol(p) != nOcc) 
#>         stop("You have ", ncol(p), " occasions, but piFun expects ", 
#>             nOcc, ".", call. = FALSE)
#>     out <- matrix(NA, nrow(p), nrow(combs))
#>     colnames(out) <- histories
#>     q <- t(p)
#>     for (i in 1:ncol(out)) {
#>         temp <- (1 - q) * (1 - combs[i, ]) + q * combs[i, ]
#>         out[, i] <- apply(temp, 2, prod)
#>     }
#>     return(out)
#> }
#> <bytecode: 0x55c3b88425b8>
#> <environment: 0x55c3b88443c0>
ls(environment(myPi))
#> [1] "combs"     "crPiFun"   "histories" "nOcc"     
environment(myPi)$histories
#> [1] "001" "010" "011" "100" "101" "110" "111"

p <- matrix(runif(6, 0.1, 0.9), 2, 3)  # different p's everywhere
myPi(p)
#>            001         010        011        100       101        110       111
#> [1,] 0.3475385 0.023902881 0.06454031 0.09922076 0.2679065 0.01842598 0.0497521
#> [2,] 0.1719135 0.007413497 0.05544683 0.06618910 0.4950397 0.02134781 0.1596640

p <- matrix(runif(4*5, 0.1, 0.9), 4, 5)  # > 3 occasions
try(myPi(p))  # Error
#> Error : You have 5 occasions, but piFun expects 3.
myPi <- makeCrPiFun(5)
( tmp <- myPi(p) )
#>            00001      00010      00011       00100       00101       00110
#> [1,] 0.008010499 0.08222948 0.01033147 0.020570767 0.002584551 0.026530963
#> [2,] 0.020777933 0.03782340 0.14986132 0.002492283 0.009874754 0.017975648
#> [3,] 0.230914909 0.03715307 0.12491692 0.008137138 0.027358877 0.004401908
#> [4,] 0.017759341 0.02343414 0.02182141 0.114900342 0.106992934 0.141181354
#>            00111        01000       01001       01010       01011        01100
#> [1,] 0.003333401 0.0651786396 0.008189169 0.084063568 0.010561908 0.0210295873
#> [2,] 0.071221888 0.0009164312 0.003631021 0.006609781 0.026188825 0.0004355357
#> [3,] 0.014800199 0.0234641803 0.078891819 0.012693304 0.042677725 0.0027800440
#> [4,] 0.131465294 0.0067132529 0.006251249 0.008248767 0.007681089 0.0404446654
#>            01101       01110       01111       10000       10001       10010
#> [1,] 0.002642198 0.027122722 0.003407751 0.081295312 0.010214099 0.104849902
#> [2,] 0.001725650 0.003141311 0.012446290 0.008915179 0.035323117 0.064300936
#> [3,] 0.009347129 0.001503907 0.005056471 0.030456922 0.102402981 0.016476134
#> [4,] 0.037661275 0.049695523 0.046275492 0.005391851 0.005020786 0.006625123
#>            10011       10100       10101       10110       10111       11000
#> [1,] 0.013173543 0.026229557 0.003295532 0.033829337 0.004250383 0.083108562
#> [2,] 0.254768796 0.004236957 0.016787381 0.030559147 0.121079371 0.001557961
#> [3,] 0.055396444 0.003608546 0.012132740 0.001952098 0.006563389 0.010405573
#> [4,] 0.006169184 0.032483749 0.030248226 0.039913716 0.037166866 0.001897920
#>            11001       11010       11011        11100       11101        11110
#> [1,] 0.010441919 0.107188525 0.013467372 0.0268145941 0.003369037 0.0345838830
#> [2,] 0.006172846 0.011236828 0.044521797 0.0007404239 0.002933657 0.0053403248
#> [3,] 0.034985863 0.005629053 0.018926133 0.0012328558 0.004145137 0.0006669321
#> [4,] 0.001767305 0.002332028 0.002171539 0.0114342074 0.010647309 0.0140495395
#>            11111
#> [1,] 0.004345185
#> [2,] 0.021159071
#> [3,] 0.002242375
#> [4,] 0.013082654
1 - rowSums(tmp) # Probability of non-capture
#> [1] 0.06375658 0.00524413 0.06867923 0.01907186

# makeCrPiFunMb
# ==============
( pMb <- cbind(rep(0.7, 5), 0.3, NA) )
#>      [,1] [,2] [,3]
#> [1,]  0.7  0.3   NA
#> [2,]  0.7  0.3   NA
#> [3,]  0.7  0.3   NA
#> [4,]  0.7  0.3   NA
#> [5,]  0.7  0.3   NA
myPi <- makeCrPiFunMb(3)
myPi(pMb)
#>        001   010   011   100   101   110   111
#> [1,] 0.063 0.147 0.063 0.343 0.147 0.147 0.063
#> [2,] 0.063 0.147 0.063 0.343 0.147 0.147 0.063
#> [3,] 0.063 0.147 0.063 0.343 0.147 0.147 0.063
#> [4,] 0.063 0.147 0.063 0.343 0.147 0.147 0.063
#> [5,] 0.063 0.147 0.063 0.343 0.147 0.147 0.063

( pMb <- matrix(runif(15), 5, 3) )  # col #3 will be ignored
#>            [,1]      [,2]       [,3]
#> [1,] 0.76270563 0.9157759 0.08428396
#> [2,] 0.28199297 0.9824122 0.31757486
#> [3,] 0.75788209 0.9982782 0.17615453
#> [4,] 0.05325693 0.0422988 0.60653164
#> [5,] 0.57807647 0.5035473 0.41674278
myPi(pMb)
#>             001          010         011          100         101         110
#> [1,] 0.04294690 0.0152433666 0.165742385 5.410407e-03 0.058827807 0.058827807
#> [2,] 0.14537699 0.0035610568 0.198911878 8.722925e-05 0.004872411 0.004872411
#> [3,] 0.04442787 0.0003159483 0.183180879 2.246862e-06 0.001302689 0.001302689
#> [4,] 0.04773538 0.0482878985 0.002132732 4.884681e-02 0.002157418 0.002157418
#> [5,] 0.10290886 0.1210868315 0.122817234 1.424758e-01 0.144511843 0.144511843
#>               111
#> [1,] 6.396396e-01
#> [2,] 2.721609e-01
#> [3,] 7.552745e-01
#> [4,] 9.528669e-05
#> [5,] 1.465770e-01

# with > 3 occasions
( pMb <- matrix(runif(15), 3, 5) )
#>           [,1]       [,2]      [,3]      [,4]      [,5]
#> [1,] 0.3466903 0.16490402 0.6602055 0.7106252 0.6511188
#> [2,] 0.1021695 0.12765760 0.4369987 0.7968697 0.8602657
#> [3,] 0.7033585 0.05116885 0.6471055 0.5551394 0.9583013
try(myPi(pMb))
#> Error : You have 5 occasions, but piFun expects 3.
myPi <- makeCrPiFunMb(5)
myPi(pMb)
#>            00001      00010        00011      00100       00101       00110
#> [1,] 0.063156508 0.08073008 0.0159415394 0.10319358 0.020377342 0.020377342
#> [2,] 0.066389375 0.06450467 0.0094395412 0.06267348 0.009171566 0.009171566
#> [3,] 0.005446336 0.01742053 0.0009394597 0.05572094 0.003004936 0.003004936
#>            00111      01000       01001       01010       01011       01100
#> [1,] 0.004023856 0.13190765 0.026047427 0.026047427 0.005143511 0.026047427
#> [2,] 0.001342157 0.06089427 0.008911198 0.008911198 0.001304055 0.008911198
#> [3,] 0.000162051 0.17822781 0.009611522 0.009611522 0.000518333 0.009611522
#>            01101       01110        01111      10000       10001       10010
#> [1,] 0.005143511 0.005143511 1.015674e-03 0.16861153 0.033295238 0.033295238
#> [2,] 0.001304055 0.001304055 1.908339e-04 0.05916556 0.008658222 0.008658222
#> [3,] 0.000518333 0.000518333 2.795281e-05 0.57007563 0.030743206 0.030743206
#>            10011       10100       10101       10110        10111       11000
#> [1,] 0.006574716 0.033295238 0.006574716 0.006574716 1.298290e-03 0.033295238
#> [2,] 0.001267034 0.008658222 0.001267034 0.001267034 1.854164e-04 0.008658222
#> [3,] 0.001657929 0.030743206 0.001657929 0.001657929 8.940927e-05 0.030743206
#>            11001       11010        11011       11100        11101        11110
#> [1,] 0.006574716 0.006574716 1.298290e-03 0.006574716 1.298290e-03 1.298290e-03
#> [2,] 0.001267034 0.001267034 1.854164e-04 0.001267034 1.854164e-04 1.854164e-04
#> [3,] 0.001657929 0.001657929 8.940927e-05 0.001657929 8.940927e-05 8.940927e-05
#>             11111
#> [1,] 2.563697e-04
#> [2,] 2.713362e-05
#> [3,] 4.821689e-06

# makeCrPiFunMh
# =============
pMh <- cbind(rep(0.4, 5), NA, NA)
pMh[1, 2] <- 0.3
pMh
#>      [,1] [,2] [,3]
#> [1,]  0.4  0.3   NA
#> [2,]  0.4   NA   NA
#> [3,]  0.4   NA   NA
#> [4,]  0.4   NA   NA
#> [5,]  0.4   NA   NA
myPi <- makeCrPiFunMh(3)
myPi(pMh)
#>           001      010        011      100        101        110        111
#> [1,] 0.135958 0.135958 0.09500389 0.135958 0.09500389 0.09500389 0.07810148
#> [2,] 0.135958 0.135958 0.09500389 0.135958 0.09500389 0.09500389 0.07810148
#> [3,] 0.135958 0.135958 0.09500389 0.135958 0.09500389 0.09500389 0.07810148
#> [4,] 0.135958 0.135958 0.09500389 0.135958 0.09500389 0.09500389 0.07810148
#> [5,] 0.135958 0.135958 0.09500389 0.135958 0.09500389 0.09500389 0.07810148
pMh <- cbind(runif(5), NA, NA)
pMh[1, 2] <- 0.3
pMh
#>           [,1] [,2] [,3]
#> [1,] 0.2663681  0.3   NA
#> [2,] 0.8225699   NA   NA
#> [3,] 0.2183991   NA   NA
#> [4,] 0.7497923   NA   NA
#> [5,] 0.4351382   NA   NA
myPi(pMh)
#>             001        010        011        100        101        110
#> [1,] 0.13663372 0.13663372 0.05551761 0.13663372 0.05551761 0.05551761
#> [2,] 0.02972235 0.02972235 0.11743572 0.02972235 0.11743572 0.11743572
#> [3,] 0.12868059 0.12868059 0.04125804 0.12868059 0.04125804 0.04125804
#> [4,] 0.05065804 0.05065804 0.13452786 0.05065804 0.13452786 0.13452786
#> [5,] 0.13147928 0.13147928 0.10441266 0.13147928 0.10441266 0.10441266
#>             111
#> [1,] 0.02660072
#> [2,] 0.54962635
#> [3,] 0.01562281
#> [4,] 0.42193741
#> [5,] 0.09752819

# with > 3 occasions
pMh <- cbind(runif(5), NA, NA, NA, NA)
pMh[1, 2] <- 0.3
pMh
#>            [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.07829616  0.3   NA   NA   NA
#> [2,] 0.64917947   NA   NA   NA   NA
#> [3,] 0.48075350   NA   NA   NA   NA
#> [4,] 0.26099908   NA   NA   NA   NA
#> [5,] 0.60791933   NA   NA   NA   NA
try(myPi(pMh))
#> Error : You have 5 occasions, but piFun expects 3.
myPi <- makeCrPiFunMh(5)
1 - rowSums(myPi(pMh))  # Probability of non-detection
#> [1] 0.65348685 0.01063023 0.05276275 0.23401921 0.01673811