Skip to content

Commit

Permalink
collapse cov stan files
Browse files Browse the repository at this point in the history
  • Loading branch information
abigailkeller committed Dec 22, 2024
1 parent c18b662 commit 8cecb4e
Show file tree
Hide file tree
Showing 28 changed files with 1,065 additions and 1,657 deletions.
1,008 changes: 504 additions & 504 deletions .Rhistory

Large diffs are not rendered by default.

207 changes: 207 additions & 0 deletions .ipynb_checkpoints/Untitled-checkpoint.ipynb

Large diffs are not rendered by default.

21 changes: 8 additions & 13 deletions R/detectionCalculate.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ detectionCalculate <- function(modelfit, mu, cov.val = NULL, probability = 0.9,
if(isCatch(modelfit@model_pars)){
out <- cbind(mu,ntrad_out,ndna_out)
#rename columns
trad_names <- c()
for(i in 1:modelfit@par_dims$q){
trad_names <- paste('n_traditional_',i+1,sep = '')
trad_names <- c(trad_names,paste('n_traditional_',i+1,sep = ''))
}
colnames(out) <- c('mu','n_traditional_1',trad_names,'n_eDNA')
} else {
Expand Down Expand Up @@ -172,10 +173,6 @@ isNegbin <- function(pars){
out <- ifelse('phi' %in% pars,TRUE,FALSE)
return(out)
}
isCov <- function(pars){
out <- ifelse('alpha' %in% pars,TRUE,FALSE)
return(out)
}

# function to get n traditional samples
get_ntrad <- function(pars, modelfit, mu, probability){
Expand Down Expand Up @@ -253,12 +250,8 @@ get_ntrad_q <- function(pars, modelfit, mu, probability){
get_ndna <- function(pars, modelfit, mu, qPCR.N, probability, cov.val){

# get beta
if(isCov(pars)){
alpha <- apply(rstan::extract(modelfit, pars = 'alpha')$alpha,2,'median')
beta <- alpha %*% c(1,cov.val)
} else {
beta <- stats::median(unlist(rstan::extract(modelfit, pars = 'beta')))
}
alpha <- apply(rstan::extract(modelfit, pars = 'alpha')$alpha,2,'median')
beta <- alpha %*% c(1,cov.val)

# create output
ndna_out <- vector(length = length(mu))
Expand Down Expand Up @@ -324,7 +317,8 @@ detectionCalculate_input_checks <- function(modelfit, mu, cov.val,
}

## #5. Only include input cov.val if covariates are included in model
if(all(!is.null(cov.val)) && !c('alpha') %in% modelfit@model_pars) {
if(all(!is.null(cov.val)) &&
modelfit@par_dims$alpha == 1) {
errMsg <- paste0("cov.val must be NULL if the model does not ",
"contain site-level covariates.")
stop(errMsg)
Expand All @@ -340,7 +334,8 @@ detectionCalculate_input_checks <- function(modelfit, mu, cov.val,
}

## #7. If covariates are in model, cov.val must be provided
if(all(c('alpha','p10') %in% modelfit@model_pars) && all(is.null(cov.val))) {
if(modelfit@par_dims$alpha > 1 &&
'p10' %in% modelfit@model_pars && all(is.null(cov.val))) {
errMsg <- paste0("cov.val must be provided if the model contains ",
"site-level covariates.")
stop(errMsg)
Expand Down
6 changes: 4 additions & 2 deletions R/detectionPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ detectionPlot_input_checks <- function(modelfit, mu.min, mu.max, cov.val,
}

## #6. Only include input cov.val if covariates are included in model
if(all(!is.null(cov.val)) && !c('alpha') %in% modelfit@model_pars) {
if(all(!is.null(cov.val)) &&
modelfit@par_dims$alpha == 1) {
errMsg <- paste0("cov.val must be NULL if the model does not contain ",
"site-level covariates.")
stop(errMsg)
Expand All @@ -220,7 +221,8 @@ detectionPlot_input_checks <- function(modelfit, mu.min, mu.max, cov.val,
}

## #8. If covariates are in model, cov.val must be provided
if(all(c('alpha','p10') %in% modelfit@model_pars) && all(is.null(cov.val))) {
if(modelfit@par_dims$alpha > 1 &&
'p10' %in% modelfit@model_pars && all(is.null(cov.val))) {
errMsg <- paste0("cov.val must be provided if the model contains ",
"site-level covariates.")
stop(errMsg)
Expand Down
Loading

0 comments on commit 8cecb4e

Please sign in to comment.