Skip to content

Commit

Permalink
changes to satisfy R_CMD_CHECK
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Nov 17, 2024
1 parent 012d5e7 commit e395489
Show file tree
Hide file tree
Showing 28 changed files with 442 additions and 845 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
^pkgdown$
^scratch$
^codecov\.yml$
^vignettes/web_only$

11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ Imports:
checkmate,
ggplot2,
ggnetwork,
intergraph
igraph
Suggests:
RTMBode,
knitr
Enhances:
spict,
JABBA,
Rpath
Rpath,
boot
Remotes:
DTUAqua/spict/spict,
jabbamodel/JABBA,
NOAA-EDAB/Rpath
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
VignetteBuilder: knitr
LazyData: true
URL: https://james-thorson-noaa.github.io/EcoState/
BugReports: https://github.com/James-Thorson-NOAA/EcoState/issues
URL: https://james-thorson-noaa.github.io/ecostate/
BugReports: https://github.com/James-Thorson-NOAA/ecostate/issues
674 changes: 0 additions & 674 deletions LICENSE

This file was deleted.

16 changes: 15 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@ export(compute_tracer)
export(dBdt)
export(ecostate)
export(ecostate_control)
export(myode)
export(ode23)
export(plot_foodweb)
export(print_ecopars)
export(rk4sys)
importFrom(MASS,ginv)
importFrom(RTMB,ADREPORT)
importFrom(RTMB,MakeADFun)
importFrom(RTMB,REPORT)
importFrom(RTMB,getAll)
importFrom(RTMB,sdreport)
importFrom(TMB,config)
importFrom(checkmate,assertCharacter)
importFrom(checkmate,assertDouble)
importFrom(checkmate,assertFactor)
importFrom(ggnetwork,geom_edges)
importFrom(ggnetwork,geom_nodes)
importFrom(ggnetwork,geom_nodetext)
importFrom(ggnetwork,ggnetwork)
importFrom(ggplot2,aes)
importFrom(ggplot2,ggplot)
importFrom(igraph,graph_from_adjacency_matrix)
importFrom(stats,dnorm)
importFrom(stats,nlminb)
importFrom(stats,optimHess)

# Edited by hand
import("RTMB")
28 changes: 18 additions & 10 deletions R/abm3pc_sys.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#' Third-order Adams-Bashford-Moulton predictor-corrector method.
#'
#' @inheritParams rk4sys
#' @param n number of steps
#' @param ... additional inputs to function \code{f}
#'
#' @details
#' Combined Adams-Bashford and Adams-Moulton (or: multi-step) method of third order
Expand All @@ -13,7 +15,13 @@
#'
#' @export
abm3pc_sys <-
function (f, a, b, y0, n, Pars) {
function( f,
a,
b,
y0,
n,
Pars,
... ) {
if (!is.numeric(n) || length(n) != 1 || n < 5){
stop("Argument 'n' must be an integer greater or equal to 5.")
}
Expand All @@ -23,25 +31,25 @@ function (f, a, b, y0, n, Pars) {
k <- h/12
x <- seq(a, b, by = h)
z <- y <- matrix(nrow=n + 1, ncol=m)
z[1,] <- f(a, y0, Pars)
z[1,] <- f(a, y0, Pars, ...)
y[1,] <- y0
k1 <- h * z[1,]
k2 <- h * f(a + h/2, y0 + k1/2, Pars)
k3 <- h * f(a + 0.75 * h, y0 + 0.75 * k2, Pars)
k2 <- h * f(a + h/2, y0 + k1/2, Pars, ...)
k3 <- h * f(a + 0.75 * h, y0 + 0.75 * k2, Pars, ...)
y[2,] <- y0 + (2 * k1 + 3 * k2 + 4 * k3)/9
z[2,] <- f(x[2], y[2,], Pars)
z[2,] <- f(x[2], y[2,], Pars, ...)
k1 <- h * z[2,]
k2 <- h * f(x[2] + h/2, y[2,] + k1/2, Pars)
k3 <- h * f(x[2] + 0.75 * h, y[2,] + 0.75 * k2, Pars)
k2 <- h * f(x[2] + h/2, y[2,] + k1/2, Pars, ...)
k3 <- h * f(x[2] + 0.75 * h, y[2,] + 0.75 * k2, Pars, ...)
y[3,] <- y[2,] + (2 * k1 + 3 * k2 + 4 * k3)/9
z[3,] <- f(x[2], y[2,], Pars)
z[3,] <- f(x[2], y[2,], Pars, ...)
zz <- yy <- matrix(nrow=n+1, ncol=m)
#errorest <- numeric(n)
for (i in 3:n) {
yy[i+1,] <- y[i,] + k * (23 * z[i,] - 16 * z[i-1,] + 5 * z[i-2,])
zz[i+1,] <- f(x[i+1], yy[i+1,], Pars)
zz[i+1,] <- f(x[i+1], yy[i+1,], Pars, ...)
y[i+1,] <- y[i,] + k * (5 * zz[i+1,] + 8 * z[i,] - z[i-1,])
z[i+1,] <- f(x[i+1], y[i+1,], Pars)
z[i+1,] <- f(x[i+1], y[i+1,], Pars, ...)
#errorest[i+1] <- -0.1 * (y[i+1] - yy[i+1])
}
#errorest <- sqrt(abs(errorest))
Expand Down
3 changes: 3 additions & 0 deletions R/add_equilibrium.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#' Compute equilibrium values
#'
#' @param ecoparams list of parameters
#' @param noB_i Boolean vector indicating which taxa have no B value
#' @param type_i character vector indicating whether a taxon is "hetero", "auto", or
#' "detritus"
#'
#' @details
#' todo
Expand Down
44 changes: 41 additions & 3 deletions R/compute_nll.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,44 @@
#' @description
#' Compute negative log-likelihood for EcoState model
#'
#' @inheritParams add_equilibrium
#' @inheritParams dBdt
#'
#' @param p list of parameters
#' @param control output from \link{ecostate_control}
#' @param project_vars function to integrate differential equation
#' @param DC_ij Diet projections matrix
#' @param logQB_i log of QB
#' @param Bobs_ti matrix of biomass observations
#' @param Cobs_ti matrix of catch observations
#'
#' @details
#' todo
#'
#' @export
compute_nll <-
function( p ) {
function( p,
noB_i,
type_i,
n_species,
project_vars,
DC_ij,
logQB_i,
Bobs_ti,
Cobs_ti,
#scale_solver,
#process_error,
#F_type,
#n_steps,
#inverse_method,
control ) {

n_steps = control$n_steps
F_type = control$F_type
scale_solver = control$scale_solver
inverse_method = control$inverse_method
process_error = control$process_error

p = add_equilibrium( p,
scale_solver = scale_solver,
noB_i = noB_i,
Expand All @@ -31,6 +60,9 @@ function( p ) {
out_initial = dBdt( Time = 1,
State = c(p$logB_i,rep(0,n_species)),
Pars = p_t,
type_i = type_i,
n_species = n_species,
F_type = F_type,
what = "stuff" )

# Objects to save
Expand Down Expand Up @@ -69,6 +101,9 @@ function( p ) {
b = 1,
n = n_steps,
Pars = p_t,
type_i = type_i,
n_species = n_species,
F_type = F_type,
y0 = y0 )

# Average biomass
Expand Down Expand Up @@ -104,6 +139,9 @@ function( p ) {
out = dBdt( Time = 0,
State = c(Bmean_ti[t,], rep(0,n_species)),
Pars = p_t,
type_i = type_i,
n_species = n_species,
F_type = F_type,
what = "stuff" )
# Must calculate during loop because G_ti is NA for t=1
G_ti[t,] = out$G_i
Expand All @@ -124,8 +162,8 @@ function( p ) {
Z_ti = F_ti + M_ti

# likelihood
Bobs_ti = OBS(Bobs_ti)
Cobs_ti = OBS(Cobs_ti)
Bobs_ti = Bobs_ti
Cobs_ti = Cobs_ti
Bexp_ti = B_ti * (rep(1,nrow(B_ti)) %*% t(exp(p$logq_i)))
for( i in seq_len(n_species) ){
for( t in seq_len(nrow(Bexp_ti)) ){
Expand Down
35 changes: 24 additions & 11 deletions R/dBdt.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#' @param State todo
#' @param Pars todo
#' @param what todo
#' @param F_type whether to integrate catches along with biomass (\code{"integrated"})
#' or calculate catches from the Baranov catch equation applied to average
#' biomass (\code{"averaged"})
#' @param n_species number of species
#' @param type_i type for each taxon
#'
#' @details
#' todo
Expand All @@ -28,22 +33,30 @@
dBdt <-
function( Time,
State,
Pars,
what = "dBdt" ){
Pars,
type_i,
n_species,
F_type,
what = "dBdt" ){

# Inputs from function call
RTMB::getAll(Pars)
#RTMB::getAll(Pars)
Bt_i = State[1:n_species]

# Inputs from local environment
QB_i = exp(logQB_i)
PB_i = exp(logPB_i)
X_ij = exp(Xprime_ij) + 1
B_i = exp(logB_i)
# EE_i is in Pars
# U_i is in Pars
F_i = exp(logF_i)

QB_i = exp(Pars$logQB_i)
PB_i = exp(Pars$logPB_i)
X_ij = exp(Pars$Xprime_ij) + 1
B_i = exp(Pars$logB_i)
U_i = Pars$U_i
EE_i = Pars$EE_i
F_i = exp(Pars$logF_i)
m0_i = Pars$m0_i
Qe_ij = Pars$Qe_ij
GE_i = Pars$GE_i
detritus_turnover = Pars$detritus_turnover
epsilon_i = Pars$epsilon_i

# passed via environment: type_i

# Indicators
Expand Down
Loading

0 comments on commit e395489

Please sign in to comment.