Because of the length of time needed to run the vignettes, only static vignettes have been included with this package.
The original of the vignettes and the code can be obtained from the GitHub site at https://github.com/cschwarz-stat-sfu-ca/BTSPAS
In some cases, the population of fish consist of a mixture of ages (young of year, and juvenile) and stocks (wild and hatchery). BTSPAS has a number of routines to handle three common occurrences as explained below.
In all cases, only diagonal recoveries are allowed.
Refer to the vignette on the Diagonal Case for information about fixing values of \(p\) or modelling \(p\) using covariates such a stream flow or smoothing \(p\) using a temporal spline.
In each stratum \(j\), \(n1[j]\) fish are marked and released above a rotary screw trap. Of these, \(m2[j]\) are recaptured in the stratum of release, i.e. the matrix of releases and recoveries is diagonal. The \(n1[j]\) and \(m2[j]\) establish the capture efficiency of the trap.
At the same time, \(u2[j]\) unmarked fish are captured at the screw trap. These fish are a mixture of wild and hatchery raised Chinook Salmon. A portion (clip.rate) of the hatchery raised fish are adipose fin clipped and can be recognized as hatchery raised. The unclipped fish are a mixture of wild and hatchery fish which must be separated. Hence the \(u2[j]\) are separated into:
Here is an example of some raw data that is read in:
<- textConnection(
demo.data.csv " jweek, n1, m2, u2.A, u2.N
9, 0, 0, 0, 4135
10, 1465, 51, 0, 10452
11, 1106, 121, 0, 2199
12, 229, 25, 0, 655
13, 20, 0, 0, 308
14, 177, 17, 0, 719
15, 702, 74, 0, 973
16, 633, 94, 0, 972
17, 1370, 62, 0, 2386
18, 283, 10, 0, 469
19, 647, 32, 0, 897
20, 276, 11, 0, 426
21, 277, 13, 0, 407
22, 333, 15, 0, 526
23, 3981, 242, 9427, 30542
24, 3988, 55, 4243, 13337
25, 2889, 115, 1646, 6282
26, 3119, 198, 1366, 5552
27, 2478, 80, 619, 2959
28, 1292, 71, 258, 1455
29, 2326, 153, 637, 3575
30, 2528, 156, 753, 4284
31, 2338, 275, 412, 2903
32, 1012, 101, 173, 1127
33, 729, 66, 91, 898
34, 333, 44, 38, 406
35, 269, 33, 22, 317
36, 77, 7, 8, 99
37, 62, 9, 2, 77
38, 26, 3, 4, 37
39, 20, 1, 1, 22")
<- read.csv(demo.data.csv, header=TRUE, as.is=TRUE, strip.white=TRUE)
demo.data
print(demo.data)
#> jweek n1 m2 u2.A u2.N
#> 1 9 0 0 0 4135
#> 2 10 1465 51 0 10452
#> 3 11 1106 121 0 2199
#> 4 12 229 25 0 655
#> 5 13 20 0 0 308
#> 6 14 177 17 0 719
#> 7 15 702 74 0 973
#> 8 16 633 94 0 972
#> 9 17 1370 62 0 2386
#> 10 18 283 10 0 469
#> 11 19 647 32 0 897
#> 12 20 276 11 0 426
#> 13 21 277 13 0 407
#> 14 22 333 15 0 526
#> 15 23 3981 242 9427 30542
#> 16 24 3988 55 4243 13337
#> 17 25 2889 115 1646 6282
#> 18 26 3119 198 1366 5552
#> 19 27 2478 80 619 2959
#> 20 28 1292 71 258 1455
#> 21 29 2326 153 637 3575
#> 22 30 2528 156 753 4284
#> 23 31 2338 275 412 2903
#> 24 32 1012 101 173 1127
#> 25 33 729 66 91 898
#> 26 34 333 44 38 406
#> 27 35 269 33 22 317
#> 28 36 77 7 8 99
#> 29 37 62 9 2 77
#> 30 38 26 3 4 37
#> 31 39 20 1 1 22
There are several unusual features of the data set:
We already read in the data above. Here we set the rest of the parameters.
Don’t forget to set the working directory as appropriate:
library("BTSPAS")
# After which weeks do the hatchery fish start to arrive. Prior to this point, all fish are wild and it is not
# necessary to separate out the wild vs hatchery
<- c(22) # julian weeks after which hatchery fish arrive.
demo.hatch.after
# Which julian weeks have "bad" values. These will be set to 0 (releases or recaptures) or missing (unmarked) and estimated.
<- c() # list of julian weeks with bad m2 values
demo.bad.m2 <- c() # list of julian weeks with bad u2.A values
demo.bad.u2.A <- c() # list of julian weeks with bad u2.N values
demo.bad.u2.N
# The clipping fraction
<- .25 # what fraction of the hatchery fish are adipose fin clipped
demo.clip.frac.H
# The prefix for the output files:
<- "JC-2003-CH"
demo.prefix
# Title for the analysis
<- "Junction City 2003 Chinook - Separation of Wild and Hatchery YoY Chinook"
demo.title
cat("*** Starting ",demo.title, "\n\n")
#> *** Starting Junction City 2003 Chinook - Separation of Wild and Hatchery YoY Chinook
# Make the call to fit the model and generate the output files
<- TimeStratPetersenDiagErrorWHChinook_fit(
demo.fit title =demo.title,
prefix =demo.prefix,
time =demo.data$jweek ,
n1 =demo.data$n1,
m2 =demo.data$m2,
u2.A =demo.data$u2.A,
u2.N =demo.data$u2.N ,
clip.frac.H= demo.clip.frac.H,
hatch.after=demo.hatch.after,
bad.m2 =demo.bad.m2,
bad.u2.A =demo.bad.u2.A,
bad.u2.N =demo.bad.u2.N,
debug=TRUE, # this generates only 10,000 iterations of the MCMC chain for checking.
save.output.to.files=FALSE)
#>
#>
#> *** Start of call to JAGS
#> Working directory: /Users/cschwarz/Dropbox/SPAS-Bayesian/BTSPAS/vignettes
#> Initial seed for JAGS set to: 786588
#> Random number seed for chain 309430
#> Random number seed for chain 41861
#> Random number seed for chain 1207
#> Compiling model graph
#> Resolving undeclared variables
#> Allocating nodes
#> Graph information:
#> Observed stochastic nodes: 78
#> Unobserved stochastic nodes: 177
#> Total graph size: 1630
#>
#> Initializing model
#>
#>
|
| | 0%
|
|++ | 4%
|
|++++ | 8%
|
|++++++ | 12%
|
|++++++++ | 16%
|
|++++++++++ | 20%
|
|++++++++++++ | 24%
|
|++++++++++++++ | 28%
|
|++++++++++++++++ | 32%
|
|++++++++++++++++++ | 36%
|
|++++++++++++++++++++ | 40%
|
|++++++++++++++++++++++ | 44%
|
|++++++++++++++++++++++++ | 48%
|
|++++++++++++++++++++++++++ | 52%
|
|++++++++++++++++++++++++++++ | 56%
|
|++++++++++++++++++++++++++++++ | 60%
|
|++++++++++++++++++++++++++++++++ | 64%
|
|++++++++++++++++++++++++++++++++++ | 68%
|
|++++++++++++++++++++++++++++++++++++ | 72%
|
|++++++++++++++++++++++++++++++++++++++ | 76%
|
|++++++++++++++++++++++++++++++++++++++++ | 80%
|
|++++++++++++++++++++++++++++++++++++++++++ | 84%
|
|++++++++++++++++++++++++++++++++++++++++++++ | 88%
|
|++++++++++++++++++++++++++++++++++++++++++++++ | 92%
|
|++++++++++++++++++++++++++++++++++++++++++++++++ | 96%
|
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100%
#>
|
| | 0%
|
|** | 4%
|
|**** | 8%
|
|****** | 12%
|
|******** | 16%
|
|********** | 20%
|
|************ | 24%
|
|************** | 28%
|
|**************** | 32%
|
|****************** | 36%
|
|******************** | 40%
|
|********************** | 44%
|
|************************ | 48%
|
|************************** | 52%
|
|**************************** | 56%
|
|****************************** | 60%
|
|******************************** | 64%
|
|********************************** | 68%
|
|************************************ | 72%
|
|************************************** | 76%
|
|**************************************** | 80%
|
|****************************************** | 84%
|
|******************************************** | 88%
|
|********************************************** | 92%
|
|************************************************ | 96%
|
|**************************************************| 100%
#>
#>
#> *** Finished JAGS ***
#> Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.
Here is the fitted spline curve to the number of unmarked fish available in each recovery sample
$plots$fit.plot demo.fit
The separation of wild and hatchery fish is evident as well has the start of the spline for the hatchery fish.
A plot of the \(logit(P)\) is
$plots$logitP.plot demo.fit
In cases where there is no information (such as the first julian week), \(BTSPAS\) has interpolated based on the distribution of catchability in the other strata and so the credible interval is very wide.
A summary of the posterior for each parameter is also available. In particular, here are the summary statistics on the posterior sample for the total number unmarked separated by wild and hatchery origin:
$summary[ row.names(demo.fit$summary) %in% c("Ntot","Utot","Utot.H","Utot.W"),]
demo.fit#> mean sd 2.5% 25% 50% 75% 97.5% Rhat n.eff
#> Utot 4886878 2201403 2876388.7 3190186 3550698.7 7225287 8138957 3.741584 3
#> Utot.H 3672542 2007050 2006948.2 2205647 2392307.5 6300015 6924714 17.726043 3
#> Utot.W 1214336 1199008 722701.6 825935 926705.4 1169012 3487257 1.072534 50
In this example, BTSPAS allows for separating wild from hatchery Chinook salmon when Age-1 Chinook Salmon are present (residualized) from last year.
In each stratum \(j\), \(n1[j]\) fish are marked and released above a rotary screw trap. Of these, \(m2[j]\) are recaptured in the stratum of release, i.e. the matrix of releases and recoveries is diagonal. The \(n1[j]\) and \(m2[j]\) establish the capture efficiency of the trap.
At the same time, \(u2[j]\) unmarked fish are captured at the screw trap. These fish are a mixture of YoY and Age-1 wild and hatchery raised Chinook Salmon. A portion (clip.rate.H.YoY, clip.rate.H.1) of the YoY and Age1 hatchery raised fish are adipose fin clipped and can be recognized as hatchery raised. The unclipped fish are a mixture of wild and hatchery fish which must be separated. Hence the \(u2[j]\) are separated into
Here is an example of some raw data that is read in:
<- textConnection(
demo2.data.csv " jweek, n1, m2, u2.A.YoY, u2.N.YoY, u2.A.1, u2.N.1
2, 0, 0, 0, 15, 0, 10
3, 0, 0, 0, 94, 1, 90
4, 833, 52, 0, 385, 2, 112
5, 852, 67, 0, 1162, 12, 140
6, 1495, 77, 0, 592, 10, 103
7, 1356, 182, 0, 1151, 4, 94
8, 1889, 145, 0, 2258, 7, 121
9, 2934, 89, 0, 1123, 2, 80
10, 1546, 53, 0, 2277, 5, 57
11, 4001, 232, 0, 2492, 4, 27
12, 2955, 158, 0, 1579, 14, 88
13, 529, 14, 0, 1046, 5, 45
14, 1172, 49, 0, 766, 3, 13
15, 3204, 232, 0, 2702, 1, 9
16, 1328, 57, 0, 10408, 2, 18
17, 3540, 114, 0, 12145, 3, 15
18, 4791, 45, 0, 186, 0, 1
19, 4808, 11, 0, 407, 0, 2
20, 5952, 44, 0, 862, 0, 0
21, 3852, 55, 0, 465, 0, 0
22, 2621, 17, 0, 724, 0, 27
23, 2131, 37, 854, 4860, 0, 0
24, 5002, 152, 794, 3539, 0, 1
25, 3706, 120, 904, 4597, 0, 0
26, 1225, 44, 708, 3819, 0, 0
27, 723, 45, 762, 3300, 0, 0
28, 2895, 167, 1356, 5460, 0, 0
29, 1395, 117, 614, 2918, 0, 0
30, 479, 77, 420, 2252, 0, 0
31, 964, 74, 289, 1240, 0, 0
32, 2803, 288, 87, 428, 0, 0
33, 952, 51, 114, 464, 0, 0
34, 880, 126, 53, 515, 0, 0
35, 0, 0, 21, 93, 0, 0")
<- read.csv(demo2.data.csv, header=TRUE, as.is=TRUE, strip.white=TRUE)
demo2.data
print(demo2.data)
#> jweek n1 m2 u2.A.YoY u2.N.YoY u2.A.1 u2.N.1
#> 1 2 0 0 0 15 0 10
#> 2 3 0 0 0 94 1 90
#> 3 4 833 52 0 385 2 112
#> 4 5 852 67 0 1162 12 140
#> 5 6 1495 77 0 592 10 103
#> 6 7 1356 182 0 1151 4 94
#> 7 8 1889 145 0 2258 7 121
#> 8 9 2934 89 0 1123 2 80
#> 9 10 1546 53 0 2277 5 57
#> 10 11 4001 232 0 2492 4 27
#> 11 12 2955 158 0 1579 14 88
#> 12 13 529 14 0 1046 5 45
#> 13 14 1172 49 0 766 3 13
#> 14 15 3204 232 0 2702 1 9
#> 15 16 1328 57 0 10408 2 18
#> 16 17 3540 114 0 12145 3 15
#> 17 18 4791 45 0 186 0 1
#> 18 19 4808 11 0 407 0 2
#> 19 20 5952 44 0 862 0 0
#> 20 21 3852 55 0 465 0 0
#> 21 22 2621 17 0 724 0 27
#> 22 23 2131 37 854 4860 0 0
#> 23 24 5002 152 794 3539 0 1
#> 24 25 3706 120 904 4597 0 0
#> 25 26 1225 44 708 3819 0 0
#> 26 27 723 45 762 3300 0 0
#> 27 28 2895 167 1356 5460 0 0
#> 28 29 1395 117 614 2918 0 0
#> 29 30 479 77 420 2252 0 0
#> 30 31 964 74 289 1240 0 0
#> 31 32 2803 288 87 428 0 0
#> 32 33 952 51 114 464 0 0
#> 33 34 880 126 53 515 0 0
#> 34 35 0 0 21 93 0 0
There are several unusual features of the data set:
We already read in the data above. Here we set the rest of the parameters.
Don’t forget to set the working directory as appropriate:
library("BTSPAS")
# After which weeks do the YoY hatchery fish start to arrive.
# Prior to this point, all YoY fish are wild and it is not
# necessary to separate out the YoY wild vs hatchery
<- c(22) # julian weeks after which YoY hatchery fish arrive.
demo2.hatch.after.YoY
# Which julian weeks have "bad" values. These will be set 0 (releases or recaptures) or missing (unmarked) and estimated.
<- c() # list of julian weeks with bad m2 values
demo2.bad.m2 <- c() # list of julian weeks with bad u2.A.YoY values
demo2.bad.u2.A.YoY <- c() # list of julian weeks with bad u2.N.YoY values
demo2.bad.u2.N.YoY .1 <- c() # list of julian weeks with bad u2.A.YoY values
demo2.bad.u2.A.1 <- c() # list of julian weeks with bad u2.N.YoY values
demo2.bad.u2.N
# The clipping fraction for the current YoY and last year's YoY (which are now Age 1 fish)
<- .25 # what fraction of the YoY hatchery fish are adipose fin clipped
demo2.clip.frac.H.YoY .1 <- .25 # what fraction of the Age1 hatchery fish are adipose fin clipped
demo2.clip.frac.H
# The prefix for the output files:
<- "NF-2009-CH-WH-YoY-Age1"
demo2.prefix
# Title for the analysis
<- "North Fork 2009 Chinook - Separation of YoY and Age 1 Wild and Hatchery Chinook"
demo2.title
cat("*** Starting ",demo2.title, "\n\n")
#> *** Starting North Fork 2009 Chinook - Separation of YoY and Age 1 Wild and Hatchery Chinook
# Make the call to fit the model and generate the output files
<- TimeStratPetersenDiagErrorWHChinook2_fit(
demo2.fit title = demo2.title,
prefix = demo2.prefix,
time = demo2.data$jweek,
n1 = demo2.data$n1,
m2 = demo2.data$m2,
u2.A.YoY = demo2.data$u2.A.YoY,
u2.N.YoY = demo2.data$u2.N.YoY,
u2.A.1 = demo2.data$u2.A.1,
u2.N.1 = demo2.data$u2.N.1,
clip.frac.H.YoY= demo2.clip.frac.H.YoY,
clip.frac.H.1 = demo2.clip.frac.H.1,
hatch.after.YoY= demo2.hatch.after.YoY,
bad.m2 = demo2.bad.m2,
bad.u2.A.YoY = demo2.bad.u2.A.YoY,
bad.u2.N.YoY = demo2.bad.u2.N.YoY,
bad.u2.A.1 = demo2.bad.u2.A.1 ,
bad.u2.N.1 = demo2.bad.u2.N.1,
debug=TRUE, # this generates only 10,000 iterations of the MCMC chain for checking.
save.output.to.files=FALSE)
#>
#>
#> *** Start of call to JAGS
#> Working directory: /Users/cschwarz/Dropbox/SPAS-Bayesian/BTSPAS/vignettes
#> Initial seed for JAGS set to: 370578
#> Random number seed for chain 669181
#> Random number seed for chain 956873
#> Random number seed for chain 878382
#> Compiling model graph
#> Resolving undeclared variables
#> Allocating nodes
#> Graph information:
#> Observed stochastic nodes: 146
#> Unobserved stochastic nodes: 331
#> Total graph size: 2933
#>
#> Initializing model
#>
#>
|
| | 0%
|
|++ | 4%
|
|++++ | 8%
|
|++++++ | 12%
|
|++++++++ | 16%
|
|++++++++++ | 20%
|
|++++++++++++ | 24%
|
|++++++++++++++ | 28%
|
|++++++++++++++++ | 32%
|
|++++++++++++++++++ | 36%
|
|++++++++++++++++++++ | 40%
|
|++++++++++++++++++++++ | 44%
|
|++++++++++++++++++++++++ | 48%
|
|++++++++++++++++++++++++++ | 52%
|
|++++++++++++++++++++++++++++ | 56%
|
|++++++++++++++++++++++++++++++ | 60%
|
|++++++++++++++++++++++++++++++++ | 64%
|
|++++++++++++++++++++++++++++++++++ | 68%
|
|++++++++++++++++++++++++++++++++++++ | 72%
|
|++++++++++++++++++++++++++++++++++++++ | 76%
|
|++++++++++++++++++++++++++++++++++++++++ | 80%
|
|++++++++++++++++++++++++++++++++++++++++++ | 84%
|
|++++++++++++++++++++++++++++++++++++++++++++ | 88%
|
|++++++++++++++++++++++++++++++++++++++++++++++ | 92%
|
|++++++++++++++++++++++++++++++++++++++++++++++++ | 96%
|
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100%
#>
|
| | 0%
|
|** | 4%
|
|**** | 8%
|
|****** | 12%
|
|******** | 16%
|
|********** | 20%
|
|************ | 24%
|
|************** | 28%
|
|**************** | 32%
|
|****************** | 36%
|
|******************** | 40%
|
|********************** | 44%
|
|************************ | 48%
|
|************************** | 52%
|
|**************************** | 56%
|
|****************************** | 60%
|
|******************************** | 64%
|
|********************************** | 68%
|
|************************************ | 72%
|
|************************************** | 76%
|
|**************************************** | 80%
|
|****************************************** | 84%
|
|******************************************** | 88%
|
|********************************************** | 92%
|
|************************************************ | 96%
|
|**************************************************| 100%
#>
#>
#> *** Finished JAGS ***
Here is the fitted spline curve to the number of unmarked fish available of both stocks and ages.
$plots$fit.plot demo2.fit