Skip to content

Commit 35557fb

Browse files
authored
Merge pull request #13 from trackyverse/includes
Shift redundant code to #includes files
2 parents 460e2ff + ba45b58 commit 35557fb

12 files changed

Lines changed: 113 additions & 219 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: TelemetrySpace
2-
Version: 1.3.0
2+
Version: 1.3.1
33
Title: Spatial point process and random field models for electronic tagging data
44
Description: A collection of tools to fit spatial models to several types of telemetry data. Models are provided to account for the detection process when estimating individual centers of activity from acoustic telemetry data and to incorporate data from stationary test transmitters when available. Bayesian versions of models are fitted using Stan (http://mc-stan.org/). Maximum likelihood versions are fitted using Template Model Builder (https://kaskr.github.io/adcomp/index.html).
55
Authors@R: c(

NEWS.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
# TelemetrySpace 1.3.0
2-
- Code refactor, **BREAKING CHANGE**: The structure is now "per individual" (loop) > "per time" (loop) > _vectorized over_ receivers. Because of this, input data must be an array with dimensions 1: individual; 2: time; 3: receiver. Previously it was individual, receiver, time.
1+
# TelemetrySpace 1.3
2+
## 1.3.1
3+
- Code refactor ([PR #13](https://github.com/trackyverse/TelemetrySpace/pull/13)): leverage [Stan's "Includes" framework](https://mc-stan.org/docs/reference-manual/includes.html) to reduce redundant code across files.
4+
5+
## 1.3.0
6+
- Code refactor ([PR #12](https://github.com/trackyverse/TelemetrySpace/pull/12)), **BREAKING CHANGE**: The structure is now "per individual" (loop) > "per time" (loop) > _vectorized over_ receivers. Because of this, input data must be an array with dimensions 1: individual; 2: time; 3: receiver. Previously it was individual, receiver, time.
37
- `p0` and `sigma` are generated quantities rather than transformed parameters. This should not affect anything on the user side, but the code runs a bit faster.
48
- Calculated distances between receivers and COAs have been moved from the transformed parameters to the model block
59
- The model now operates on squared distances rather than Euclidean distance.

inst/stan/COA_Standard_gaussian.stan

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,22 @@
11
// Declare data
22
data {
3-
int<lower = 0> nind; // number of individuals
4-
int<lower = 0> nrec; // number of receivers
5-
int<lower = 0> ntime; // number of time steps
6-
int<lower = 0> ntrans; // number of trials/expected number of transmissions per time step
7-
array[nind, ntime, nrec] int<lower = 0> y; // number of detections for each individual at each receiver in each time step
8-
vector[nrec] recX; // receiver locations in east-west direction
9-
vector[nrec] recY; // receiver locations in north-south direction
10-
vector[2] xlim; // area bounds east-west
11-
vector[2] ylim; // area boundes north-south
3+
#include include/shared_data.stan
4+
}
5+
6+
transformed data {
7+
int logistic = 0; // Tells include file to use squared distance
128
}
139

1410
// Declare parameters
1511
parameters {
16-
// fixed effects
17-
real<lower = -7, upper = 7> alpha0; // detection probability intercept on the logit scale - bounds are to ensure only searching reasonable parameter space
18-
real<lower = 0> alpha1; // coef. for decline in detection probability with distance
19-
20-
// latent variables
21-
matrix<lower = xlim[1], upper = xlim[2]> [nind, ntime] sx; // E-W center of activity coordinate - bounds reflect spatial extent
22-
matrix<lower = ylim[1], upper = ylim[2]> [nind, ntime] sy; // N-S center of activity coordinate - bounds reflect spatial extent
12+
// detection probability intercept on the logit scale
13+
// bounds are to ensure only searching reasonable parameter space
14+
real<lower = -5, upper = 5> alpha0;
15+
#include include/shared_parameters.stan
2316
}
2417

2518
model {
26-
// priors
27-
alpha0 ~ cauchy(0, 2.5);
28-
alpha1 ~ cauchy(0, 2.5);
29-
30-
for (i in 1:nind) {
31-
for (t in 1:ntime) {
32-
// Calculate squared distance (d2)
33-
vector[nrec] d2 = square(recX - sx[i, t]) + square(recY - sy[i, t]);
34-
35-
// Run binomial on logit scale
36-
y[i, t] ~ binomial_logit(ntrans, alpha0 - (alpha1 * d2));
37-
}
38-
}
19+
#include include/likelihood_coa_static.stan
3920
}
4021

4122
generated quantities {

inst/stan/COA_Standard_logistic.stan

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
// Declare data
22
data {
3-
int<lower = 0> nind; // number of individuals
4-
int<lower = 0> nrec; // number of receivers
5-
int<lower = 0> ntime; // number of time steps
6-
int<lower = 0> ntrans; // number of trials/expected number of transmissions per time step
7-
array[nind, ntime, nrec] int<lower = 0> y; // number of detections for each individual at each receiver in each time step
8-
vector[nrec] recX; // receiver locations in east-west direction
9-
vector[nrec] recY; // receiver locations in north-south direction
10-
vector[2] xlim; // area bounds east-west
11-
vector[2] ylim; // area boundes north-south
3+
#include include/shared_data.stan
4+
}
5+
transformed data {
6+
int logistic = 1; // Tells include file to use linear distance (sqrt)
127
}
13-
148
// Declare parameters
159
parameters {
1610
// fixed effects
17-
real<lower = -7, upper = 7> alpha0; // detection probability intercept on the logit scale - bounds are to ensure only searching reasonable parameter space
18-
real<lower = 0> alpha1; // coef. for decline in detection probability with distance
19-
20-
// latent variables
21-
matrix<lower = xlim[1], upper = xlim[2]> [nind, ntime] sx; // E-W center of activity coordinate - bounds reflect spatial extent
22-
matrix<lower = ylim[1], upper = ylim[2]> [nind, ntime] sy; // N-S center of activity coordinate - bounds reflect spatial extent
11+
real<lower = -5, upper = 5> alpha0; // detection probability intercept on the logit scale - bounds are to ensure only searching reasonable parameter space
12+
#include include/shared_parameters.stan
2313
}
2414

2515
model {
26-
// priors
27-
alpha0 ~ cauchy(0, 2.5);
28-
alpha1 ~ cauchy(0, 2.5);
29-
30-
for (i in 1:nind) {
31-
for (t in 1:ntime) {
32-
// Calculate euclidean distance (d)
33-
vector[nrec] d = sqrt(square(recX - sx[i, t]) + square(recY - sy[i, t]));
34-
35-
// Run binomial on logit scale
36-
y[i, t] ~ binomial_logit(ntrans, alpha0 - (alpha1 * d));
37-
}
38-
}
16+
#include include/likelihood_coa_static.stan
3917
}
4018

4119
generated quantities {

inst/stan/COA_Tag_Integrated_gaussian.stan

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
// Declare data
22
data {
3-
int<lower = 0> nind; // number of individuals
4-
int<lower = 0> nrec; // number of receivers
5-
int<lower = 0> ntime; // number of time steps
3+
#include include/shared_data.stan
64
int<lower = 0> ntest; // number of test tags
7-
8-
// number of trials/expected number of transmissions per time step
9-
int<lower = 0> ntrans;
10-
// number of detections for each individual at each receiver in each time step
11-
array[nind, ntime, nrec] int<lower = 0> y;
125
// number of detections from each test tag at each receiver in each time step
136
array[ntest, ntime, nrec] int<lower = 0> test;
14-
15-
vector[nrec] recX; // trap locations in east-west direction
16-
vector[nrec] recY; // trap locations in north-south direction
17-
vector[2] xlim; // area bounds east-west
18-
vector[2] ylim; // area boundes north-south
197
vector[ntest] testX; // test tag locations east-west
208
vector[ntest] testY; // test tag locations north-south
219
}
2210

2311
transformed data {
12+
int logistic = 0;
13+
2414
// Pre-calculate squared distances from receiver for fixed test tags
2515
matrix[ntest, nrec] td2;
2616
for (s in 1:ntest) {
@@ -30,48 +20,26 @@ transformed data {
3020

3121
// Declare parameters
3222
parameters {
33-
// fixed effects
34-
// detection probability intercept - max of ~1
35-
matrix<lower = -5, upper = 5>[ntime, nrec] alpha0; // time effect
36-
real<lower = 0> alpha1; // coef. for decline in detection probability with distance
37-
38-
// latent variables
39-
// E-W center of activity coordinate - bounds reflect spatial extent
40-
matrix<lower = xlim[1], upper = xlim[2]>[nind, ntime] sx;
41-
// N-S center of activity coordinate - bounds reflect spatial extent
42-
matrix<lower = ylim[1], upper = ylim[2]>[nind, ntime] sy;
23+
matrix<lower = -5, upper = 5>[ntime, nrec] alpha0;
24+
#include include/shared_parameters.stan
4325
}
4426

4527
// Model specification
4628
model {
47-
// priors
48-
to_vector(alpha0) ~ cauchy(0, 2.5);
49-
alpha1 ~ cauchy(0, 2.5);
50-
5129
// Likelihood for test tags (fixed locations)
52-
for (s in 1:ntest) { // For each test tag
53-
// decay over distance portion of the binomial model
54-
vector[nrec] dist_decay = alpha1 * td2[s, ]';
55-
56-
for (t in 1:ntime) { // Run binomial on logit scale
57-
// row(p0, t) pulls the alpha0 vector for all receivers at time t
30+
for (t in 1:ntime) {
31+
row_vector[nrec] alpha0_t = row(alpha0, t);
32+
33+
for (s in 1:ntest) {
34+
// decay over distance portion of the binomial model
35+
vector[nrec] dist_decay = alpha1 * td2[s, ]';
36+
5837
test[s, t] ~ binomial_logit(ntrans, row(alpha0, t)' - dist_decay);
5938
}
6039
}
6140

6241
// Likelihood for individual COAs (estimated locations)
63-
for (i in 1:nind) {
64-
for (t in 1:ntime) {
65-
// Distance squared from each COA to each receiver at each time
66-
vector[nrec] d2 = square(recX - sx[i, t]) + square(recY - sy[i, t]);
67-
68-
// Calculate linear predictor
69-
vector[nrec] lp = row(alpha0, t)' - (alpha1 * d2);
70-
71-
// Run binomial on logit scale
72-
y[i, t] ~ binomial_logit(ntrans, lp);
73-
}
74-
}
42+
#include include/likelihood_coa_time_varying.stan
7543
}
7644

7745
generated quantities {

inst/stan/COA_Tag_Integrated_logistic.stan

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
// Declare data
22
data {
3-
int<lower = 0> nind; // number of individuals
4-
int<lower = 0> nrec; // number of receivers
5-
int<lower = 0> ntime; // number of time steps
3+
#include include/shared_data.stan
64
int<lower = 0> ntest; // number of test tags
7-
8-
// number of trials/expected number of transmissions per time step
9-
int<lower = 0> ntrans;
10-
// number of detections for each individual at each receiver in each time step
11-
array[nind, ntime, nrec] int<lower = 0> y;
125
// number of detections from each test tag at each receiver in each time step
136
array[ntest, ntime, nrec] int<lower = 0> test;
14-
15-
vector[nrec] recX; // trap locations in east-west direction
16-
vector[nrec] recY; // trap locations in north-south direction
17-
vector[2] xlim; // area bounds east-west
18-
vector[2] ylim; // area boundes north-south
197
vector[ntest] testX; // test tag locations east-west
208
vector[ntest] testY; // test tag locations north-south
219
}
2210

2311
transformed data {
12+
int logistic = 1;
2413
// Pre-calculate squared distances from receiver for fixed test tags
2514
matrix[ntest, nrec] td;
2615
for (s in 1:ntest) {
@@ -30,48 +19,27 @@ transformed data {
3019

3120
// Declare parameters
3221
parameters {
33-
// fixed effects
3422
// detection probability intercept - max of ~1
3523
matrix<lower = -5, upper = 5>[ntime, nrec] alpha0; // time effect
36-
real<lower = 0> alpha1; // coef. for decline in detection probability with distance
37-
38-
// latent variables
39-
// E-W center of activity coordinate - bounds reflect spatial extent
40-
matrix<lower = xlim[1], upper = xlim[2]>[nind, ntime] sx;
41-
// N-S center of activity coordinate - bounds reflect spatial extent
42-
matrix<lower = ylim[1], upper = ylim[2]>[nind, ntime] sy;
24+
#include include/shared_parameters.stan
4325
}
4426

4527
// Model specification
4628
model {
47-
// priors
48-
to_vector(alpha0) ~ cauchy(0, 2.5);
49-
alpha1 ~ cauchy(0, 2.5);
50-
5129
// Likelihood for test tags (fixed locations)
52-
for (s in 1:ntest) { // For each test tag
53-
// decay over distance portion of the binomial model
54-
vector[nrec] dist_decay = alpha1 * td[s, ]';
30+
for (t in 1:ntime) {
31+
row_vector[nrec] alpha0_t = row(alpha0, t);
32+
5533

56-
for (t in 1:ntime) { // Run binomial on logit scale
57-
// row(p0, t) pulls the alpha0 vector for all receivers at time t
34+
for (s in 1:ntest) {
35+
// decay over distance portion of the binomial model
36+
vector[nrec] dist_decay = alpha1 * td[s, ]';
37+
5838
test[s, t] ~ binomial_logit(ntrans, row(alpha0, t)' - dist_decay);
5939
}
6040
}
6141

62-
// Likelihood for individual COAs (estimated locations)
63-
for (i in 1:nind) {
64-
for (t in 1:ntime) {
65-
// Distance squared from each COA to each receiver at each time
66-
vector[nrec] d = sqrt(square(recX - sx[i, t]) + square(recY - sy[i, t]));
67-
68-
// Calculate linear predictor
69-
vector[nrec] lp = row(alpha0, t)' - (alpha1 * d);
70-
71-
// Run binomial on logit scale
72-
y[i, t] ~ binomial_logit(ntrans, lp);
73-
}
74-
}
42+
#include include/likelihood_coa_time_varying.stan
7543
}
7644

7745
generated quantities {

inst/stan/COA_TimeVarying_gaussian.stan

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,21 @@
11
// Declare data
22
data {
3-
int<lower = 0> nind; // number of individuals
4-
int<lower = 0> nrec; // number of receivers
5-
int<lower = 0> ntime; // number of time steps
6-
int<lower = 0> ntrans; // number of trials/expected number of transmissions per time step
7-
array[nind, ntime, nrec] int<lower = 0> y; // number of detections for each individual at each receiver in each time step
8-
vector[nrec] recX; // trap locations in east-west direction
9-
vector[nrec] recY; // trap locations in north-south direction
10-
vector[2] xlim; // area bounds east-west
11-
vector[2] ylim; // area boundes north-south
3+
#include include/shared_data.stan
4+
}
5+
6+
transformed data {
7+
int logistic = 0;
128
}
139

1410
// Declare parameters
1511
parameters {
16-
// fixed effects
17-
matrix<lower = -7, upper = 7>[ntime, nrec] alpha0; // time effect
18-
real<lower = 0> alpha1; // coef. for decline in detection probability with distance
19-
20-
// latent variables
21-
// E-W center of activity coordinate - bounds reflect spatial extent
22-
matrix<lower = xlim[1], upper = xlim[2]>[nind, ntime] sx;
23-
// N-S center of activity coordinate - bounds reflect spatial extent
24-
matrix<lower = ylim[1], upper = ylim[2]>[nind, ntime] sy;
12+
matrix<lower = -5, upper = 5>[ntime, nrec] alpha0; // time effect
13+
#include include/shared_parameters.stan
2514
}
2615

2716
// Model specification
2817
model {
29-
// priors
30-
to_vector(alpha0) ~ cauchy(0, 2.5);
31-
alpha1 ~ cauchy(0, 2.5);
32-
33-
// likelihood
34-
for (i in 1:nind) {
35-
for (t in 1:ntime) {
36-
// Calculate squared distance
37-
vector[nrec] d2 = square(recX - sx[i, t]) + square(recY - sy[i, t]);
38-
39-
// Run binomial on logit scale
40-
// row(alpha0, t) is a row_vector; ' converts to column vector
41-
y[i, t] ~ binomial_logit(ntrans, row(alpha0, t)' - (alpha1 * d2));
42-
}
43-
}
18+
#include include/likelihood_coa_time_varying.stan
4419
}
4520

4621
generated quantities {

0 commit comments

Comments
 (0)