Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
6f56757
ignore .o .h .so
mhpob May 15, 2026
72b0dd6
add logistic decay
mhpob May 15, 2026
4373987
more ignoring
mhpob May 15, 2026
37057d7
fix copy/paste error
mhpob May 15, 2026
61eb0f5
update docs
mhpob May 15, 2026
652ffc8
update tests to include logistic decay
mhpob May 15, 2026
30d2c3e
fix misspecified warmup in test file
mhpob May 15, 2026
47f1dec
Change all variables name to snake_case. Also change y to det. this f…
benjaminhlina Sep 14, 2025
3411d47
Change generated qunaties to use snake_case
benjaminhlina Sep 14, 2025
be8c115
Change test-genrated_quanities to match the changes in using snake_case
benjaminhlina Sep 14, 2025
fad5cec
Change expected_lengths() to follow snake_case format changes and exp…
benjaminhlina Sep 14, 2025
8bf10de
change documentation of expected_lengths()
benjaminhlina Sep 14, 2025
902bd10
Change this to expect det instead of y and expect snake_case
benjaminhlina Sep 14, 2025
582dc65
Change setup file to use new snake_case argument names
benjaminhlina Sep 14, 2025
ec41527
remove test for ndraws as this should be being tested by generated_qu…
benjaminhlina Sep 14, 2025
85952f3
refromat using air
benjaminhlina Sep 14, 2025
35aa4df
Change test-COA_standard to expect sanke_case variables
benjaminhlina Sep 14, 2025
9ab92fa
Change gobal variabes to snake_case
benjaminhlina Sep 14, 2025
ee095bc
Change documenation to reflect changes in snake_case for coa_standard
benjaminhlina Sep 14, 2025
43b0a00
change expected_lengths documentation to expect snake_case variable n…
benjaminhlina Sep 14, 2025
53abfd2
Change gq arguments to snake_case in documenation
benjaminhlina Sep 14, 2025
6c8ed26
Change back to old names for supplied data we haven't applied snake c…
benjaminhlina Sep 14, 2025
00bf161
Change this back to the names of the argumetns flom objects in the pkg
benjaminhlina Sep 14, 2025
3b4bbc9
Change roxygen documentation to snake_case
benjaminhlina Sep 15, 2025
9218ca8
change function arguments for COA_timingvaring to snake_case
benjaminhlina Sep 15, 2025
f66be22
replace ' with "
benjaminhlina Sep 15, 2025
bbc68a4
change tests for coa_timevaring to snake_case
benjaminhlina Sep 15, 2025
9d4047b
change to expect y_rep
benjaminhlina Sep 15, 2025
c350930
Change y to det in function arguments
benjaminhlina Sep 15, 2025
0939f1b
Change this to ntrans in model_param_ex for now we need to change thi…
benjaminhlina Sep 15, 2025
2d5517d
change this to expect y_rep not yrep or test_rep not testrep
benjaminhlina Sep 15, 2025
3fc2c03
Change this to expect ntrans for now
benjaminhlina Sep 15, 2025
7292f5f
change to expect y_rep
benjaminhlina Sep 15, 2025
d3d84f4
Change test for coa_tag_integrated to snake_case
benjaminhlina Sep 16, 2025
25520f2
fix n_ind
benjaminhlina Sep 16, 2025
d1b7a78
Change to expect y_rep or test_rep
benjaminhlina Sep 16, 2025
7d4d2bc
Change documentation of COA_tagInt to be snake_case
benjaminhlina Sep 16, 2025
b836fa5
Change all arguments to snake_case and change y to det and testY to d…
benjaminhlina Sep 16, 2025
d4563fb
Change to det_test
benjaminhlina Sep 16, 2025
7e6c3ff
Change sx and sy to x and y and change ' to "
benjaminhlina Sep 16, 2025
ad17d32
change testX and testY to test_x and test_y
benjaminhlina Sep 16, 2025
9da727c
change ntest_ln to n_test_len bot hin documentation and argument
benjaminhlina Sep 16, 2025
9826e3d
change arguments from ntest to n_test and test_x and test_y
benjaminhlina Sep 16, 2025
f3f11ac
change expected variables in standata to be det_test and test_x and t…
benjaminhlina Sep 16, 2025
0531f61
change testY to det_test
benjaminhlina Sep 16, 2025
e0f136b
just reformat
benjaminhlina Sep 16, 2025
0cacc92
Update manual coa_time_varing
benjaminhlina Sep 16, 2025
268bb22
Add in n_trans_test into function - this number could be the same as …
benjaminhlina Sep 22, 2025
c65a26a
Added expected lengths to check for n_trans_test
benjaminhlina Sep 22, 2025
7e925b6
Updated the manual to include n_trans_test for coa_tagint
benjaminhlina Sep 22, 2025
88bb6f3
Added n_trans_test to setup stanadata
benjaminhlina Sep 22, 2025
253bbe7
Add in check for n_trans_test
benjaminhlina Sep 22, 2025
247d407
Forgot to change n_trans here to n_trans_test
benjaminhlina Sep 22, 2025
42df594
clean up what was lost during rebase
mhpob May 15, 2026
7e4dae8
update documentation
mhpob May 15, 2026
cd41d9f
add underscore
mhpob May 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
^\.github$
^Dockerfile$
^_pkgdown\.yml$
^\.devcontainer$
^docs$
^pkgdown$
^CNAME$
^air\.toml$
^src/StanExports_*
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ data-raw
.DS_Store
docs
.vscode
src/RcppExports.cpp
src/stanExports_*
*.o
*.so
171 changes: 96 additions & 75 deletions R/COA_Tag_Integrated.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

#' Fits a test-tag integrated Bayesian Spatial Point Process model to estimate individual centers of activity from acoustic telemetry data using Stan
#'
#' @param nind Number of tagged individuals
#' @param nrec Number of receivers
#' @param ntime Number of time steps
#' @param ntest Number of test tags
#' @param ntrans Number of expected transmissions per tag per time interval
#' @param y Array of detection data, where row = individual, column = time step, and matrix = receiver
#' @param test Array of test tag detection data, where row = individual tag, column = time step, and matrix = receiver
#' @param recX Receiver coordinates in the east-west direction (should be projected and scaled for computational efficiency)
#' @param recY Receiver coordinates in the north-south direction (should be projected and scaled for computational efficiency)
#' @param xlim East-west boundaries of spatial extent (receiver array + buffer)
#' @param ylim North-south boundaries of spatial extent (receiver array + buffer)
#' @param testX Test tag coordinates in the east-west direction (should be projected and scaled for computational efficiency)
#' @param testY Test tag coordinates in the north-south direction (should be projected and scaled for computational efficiency)
#' @param ndraws to be passed to `generated_quantities`. Changes the number of draws. Default is 10.
#' @param n_ind Number of tagged individuals
#' @param n_rec Number of receivers
#' @param n_time Number of time steps
#' @param n_test Number of test tags
#' @param n_trans Number of expected transmissions per tag per time interval
#' @param n_trans_test Number of expected transmissions per sentinel tag per time interval
#' @param det Array of detection data, where row = individual, column = receiver, and matrix = time step
#' @param det_test Array of test tag detection data, where row = individual tag, column = receiver, and matrix = time step
#' @param rec_x Receiver coordinates in the east-west direction (should be projected and scaled for computational efficiency)
#' @param rec_y Receiver coordinates in the north-south direction (should be projected and scaled for computational efficiency)
#' @param x_lim East-west boundaries of spatial extent (receiver array + buffer)
#' @param y_lim North-south boundaries of spatial extent (receiver array + buffer)
#' @param test_x Test tag coordinates in the east-west direction (should be projected and scaled for computational efficiency)
#' @param test_y Test tag coordinates in the north-south direction (should be projected and scaled for computational efficiency)
#' @param decay desired decay function. Currently one of "gaussian" or "logistic". Default is "gaussian".
#' @param n_draws to be passed to `generated_quantities`. Changes the number of draws. Default is 10.
#' @param ... Additional arguments passed to `sampling` from `rstan`.
#' This can include setting `chains`, `iter`, `warmup`, and `control`. Please see
#' `rstan::sampling()` for more info.
Expand All @@ -26,42 +28,46 @@
#'
#' @seealso [rstan::sampling()]
#' @export

COA_TagInt <- function(
nind,
nrec,
ntime,
ntest,
ntrans,
y,
test,
recX,
recY,
xlim,
ylim,
testX,
testY,
ndraws = NULL,
n_ind,
n_rec,
n_time,
n_test,
n_trans,
n_trans_test,
det,
det_test,
rec_x,
rec_y,
x_lim,
y_lim,
test_x,
test_y,
decay = "gaussian",
n_draws = NULL,
...
) {
# First move everything into a list
standata <- list(
nind = nind,
nrec = nrec,
ntime = ntime,
ntest = ntest,
ntrans = ntrans,
y = y,
test = test,
recX = recX,
recY = recY,
xlim = xlim,
ylim = ylim,
testX = testX,
testY = testY
n_ind = n_ind,
n_rec = n_rec,
n_time = n_time,
n_test = n_test,
n_trans = n_trans,
n_trans_test = n_trans_test,
det = det,
det_test = det_test,
rec_x = rec_x,
rec_y = rec_y,
x_lim = x_lim,
y_lim = y_lim,
test_x = test_x,
test_y = test_y
)

# validate this list prior to sending it to the model
exp_len <- expected_lengths(recX = recX, recY = recY, ntest_len = ntest)
exp_len <- expected_lengths(rec_x = rec_x, rec_y = rec_y, n_test_len = n_test)

validate_standata(standata, exp_len)
# set rstan options
Expand All @@ -70,53 +76,68 @@ COA_TagInt <- function(
options(mc.cores = parallel::detectCores())

# fit model
fit_model <- rstan::sampling(
stanmodels$COA_Tag_Integrated,
data = standata,
...
)
if (decay == "gaussian") {
fit_model <- rstan::sampling(
stanmodels$COA_Tag_Integrated_gaussian,
data = standata,
...
)
} else if (decay == "logistic") {
fit_model <- rstan::sampling(
stanmodels$COA_Tag_Integrated_logistic,
data = standata,
...
)
} else {
stop("decay parameter must be one of \"gaussian\" or \"logistic\".")
}

# Save chains after discarding warmup
fit_estimates <- as.data.frame(fit_model) # Note this returns parameters and latent states/derived values
fit_estimates <- as.data.frame(fit_model)
# Note this returns parameters and latent states/derived values

# Summary statistics and convergence diagnostics
fit_summary <- rstan::summary(fit_model, pars = c("p0", "sigma"))$summary
#fit_summary <- fit_sum$summary
if (decay == "gaussian") {
fit_summary <- rstan::summary(fit_model, pars = c("p0", "sigma"))$summary
} else if (decay == "logistic") {
fit_summary <- rstan::summary(fit_model, pars = c("p0"))$summary
}

# How much time did fitting take (in minutes)?
fit_time <- sum(print(rstan::get_elapsed_time(fit_model))) / 60
# # calculate generated quantities

# calculate generated quantities
fit_generated_quantities <- generated_quantities(
model = fit_model,
standata = standata,
ndraws = ndraws
n_draws = n_draws
)
# transform gq into matrix
tran_fit_gq <- transform_gq(fit_generated_quantities)

# Extract COA estimates
coas <- array(NA, dim = c(ntime, 7, nind))
coas <- array(NA, dim = c(n_time, 7, n_ind))
dimnames(coas)[[2]] <- c(
'time',
'x',
'y',
'x_lower',
'x_upper',
'y_lower',
'y_upper'
"time",
"x",
"y",
"x_lower",
"x_upper",
"y_lower",
"y_upper"
)
ew <- NULL
ns <- NULL

for (i in 1:nind) {
coas[, 1, i] <- seq(1, ntime, 1)
for (i in 1:n_ind) {
coas[, 1, i] <- seq(1, n_time, 1)
ew <- dplyr::select(
fit_estimates,
dplyr::starts_with(paste("sx[", i, ",", sep = ''))
dplyr::starts_with(paste("x[", i, ",", sep = ""))
)
ns <- dplyr::select(
fit_estimates,
dplyr::starts_with(paste("sy[", i, ",", sep = ''))
dplyr::starts_with(paste("y[", i, ",", sep = ""))
)
coas[, 2, i] <- apply(ew, 2, stats::median)
coas[, 3, i] <- apply(ns, 2, stats::median)
Expand All @@ -128,15 +149,15 @@ COA_TagInt <- function(

coas <- as.data.frame(coas[,, 1])
# Extract time-varying detection probability estimates
d_probs <- array(NA, dim = c(nrec, ntime))
d_probs <- array(NA, dim = c(n_rec, n_time))
p0_est <- NULL

for (i in 1:ntime) {
for (i in 1:n_time) {
p0_est <- dplyr::select(
fit_estimates,
dplyr::starts_with(paste("p0[", i, ",", sep = ''))
dplyr::starts_with(paste("p0[", i, ",", sep = ""))
)
for (j in 1:nrec) {
for (j in 1:n_rec) {
d_probs[j, i] <- stats::median(p0_est[, j])
}
}
Expand All @@ -152,13 +173,13 @@ COA_TagInt <- function(
tran_fit_gq
)
names(model_results) <- c(
'model',
'summary',
'time',
'coas',
'detection_probs',
'all_estimates',
'generated_quantities'
"model",
"summary",
"time",
"coas",
"detection_probs",
"all_estimates",
"generated_quantities"
)
return(model_results)
}
Loading
Loading