Skip to content

Commit

Permalink
rstantools
Browse files Browse the repository at this point in the history
  • Loading branch information
ericward-noaa committed Jul 5, 2023
1 parent 37bda4d commit 402d42e
Show file tree
Hide file tree
Showing 58 changed files with 2,003 additions and 888 deletions.
19 changes: 13 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@ Description: This package fits models to bycatch data, and can expand to fleet-w
License: GPL-3
Encoding: UTF-8
Depends:
R (>= 4.0.0), Rcpp (>= 1.0.5), methods
R (>= 3.4.0)
Imports:
rstan (>= 2.21.2),
rstantools (>= 2.1.1),
ggplot2,
loo (>= 2.4.1),
dplyr (>= 1.0.2),
rlang (>= 0.4.1)
LinkingTo: StanHeaders (>= 2.21.0.7), rstan (>= 2.21.2), BH (>= 1.75.0.0),
Rcpp (>= 1.0.5), RcppEigen (>= 0.3.3.9.1)
rlang (>= 0.4.1),
methods,
Rcpp (>= 0.12.0),
RcppParallel (>= 5.0.1),
rstan (>= 2.18.1),
rstantools (>= 2.3.0)
LinkingTo: BH (>= 1.66.0),
Rcpp (>= 0.12.0),
RcppEigen (>= 0.3.3.3.0),
RcppParallel (>= 5.0.1),
rstan (>= 2.18.1),
StanHeaders (>= 2.18.0)
Suggests:
testthat,
parallel,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export(plot_fitted)
import(Rcpp)
import(ggplot2)
import(methods)
import(rstantools)
importFrom(rstan,extract)
importFrom(rstan,sampling)
importFrom(rstan,vb)
Expand Down
3 changes: 1 addition & 2 deletions R/bycatch-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
#' @useDynLib bycatch, .registration = TRUE
#' @import methods
#' @import Rcpp
#' @import rstantools
#' @importFrom rstan sampling
#'
#' @references
#' Stan Development Team (2018). RStan: the R interface to Stan. R package version 2.18.2. http://mc-stan.org
#' Stan Development Team (2023). RStan: the R interface to Stan. R package version 2.21.8. https://mc-stan.org
#'
NULL
62 changes: 22 additions & 40 deletions R/stanmodels.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
# Part of the bayesdfa package for estimating model parameters
# Copyright (C) 2015, 2016, 2017 Trustees of Columbia University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Generated by rstantools. Do not edit by hand.

# This file is only intended to be used during the installation process
# nocov start
MODELS_HOME <- "src"
if (!file.exists(MODELS_HOME)) MODELS_HOME <- sub("R$", "src", getwd())
# names of stan models
stanmodels <- c("bycatch")

stan_files <- dir(file.path(MODELS_HOME, "stan_files"),
pattern = "stan$", full.names = TRUE
)
stanmodels <- lapply(stan_files, function(f) {
model_cppname <- sub("\\.stan$", "", basename(f))
stanfit <- rstan::stanc(f,
allow_undefined = TRUE,
obfuscate_model_name = FALSE
)
stanfit$model_cpp <- list(
model_cppname = stanfit$model_name,
model_cppcode = stanfit$cppcode
)
return(do.call(methods::new, args = c(stanfit[-(1:3)],
Class = "stanmodel",
mk_cppmodule = function(x) get(paste0("model_", model_cppname))
)))
# load each stan module
Rcpp::loadModule("stan_fit4bycatch_mod", what = TRUE)

# instantiate each stanmodel object
stanmodels <- sapply(stanmodels, function(model_name) {
# create C++ code for stan model
stan_file <- if(dir.exists("stan")) "stan" else file.path("inst", "stan")
stan_file <- file.path(stan_file, paste0(model_name, ".stan"))
stanfit <- rstan::stanc_builder(stan_file,
allow_undefined = TRUE,
obfuscate_model_name = FALSE)
stanfit$model_cpp <- list(model_cppname = stanfit$model_name,
model_cppcode = stanfit$cppcode)
# create stanmodel object
methods::new(Class = "stanmodel",
model_name = stanfit$model_name,
model_code = stanfit$model_code,
model_cpp = stanfit$model_cpp,
mk_cppmodule = function(x) get(paste0("rstantools_model_", model_name)))
})
names(stanmodels) <- sub("\\.stan$", "", basename(stan_files))
rm(MODELS_HOME)
# nocov end
4 changes: 0 additions & 4 deletions R/zzz.R

This file was deleted.

1 change: 1 addition & 0 deletions bycatch.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
Expand Down
3 changes: 1 addition & 2 deletions docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions docs/articles/a01_overview.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

12 changes: 0 additions & 12 deletions docs/articles/a01_overview_files/header-attrs-2.9/header-attrs.js

This file was deleted.

5 changes: 2 additions & 3 deletions docs/articles/a02_fitting_models.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Binary file not shown.

This file was deleted.

5 changes: 2 additions & 3 deletions docs/articles/a03_expanding_estimates.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

5 changes: 2 additions & 3 deletions docs/articles/a04_diagnosing_problems.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 402d42e

Please sign in to comment.