Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant #10

Merged
merged 36 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3488c5d
Notation (#5)
Tess-LaCoil Oct 17, 2023
cfa652d
Added single ind const model, updated rmot_models, rmot_run, rmot vig…
Tess-LaCoil Oct 17, 2023
f2014e0
Updated the single individual constant model stan file and the testin…
Tess-LaCoil Oct 19, 2023
d783188
Added some comments to the model testing script.
Tess-LaCoil Oct 19, 2023
fc05a1a
Expending tests: testing run
dfalster Oct 31, 2023
b504bbc
Reorganise if statements
dfalster Oct 31, 2023
3bbfd26
Rename file to linear
dfalster Oct 31, 2023
79f4c9d
Added unit testing of model output for linear data.
Tess-LaCoil Mar 20, 2024
fbad66f
Fixing conflicts.
Tess-LaCoil Mar 20, 2024
58e19ab
Re-added testing structure.
Tess-LaCoil Mar 20, 2024
9cddbc4
Found problem with unit testing for const model outputs: object y_sin…
Tess-LaCoil Mar 20, 2024
bab2e1b
Added internal testing data so no data is created within test_that() …
fontikar Mar 20, 2024
73435e7
Added internal testing data so no data is created within test_that() …
fontikar Mar 20, 2024
c03856b
Merge branch 'constant_single_individual' of https://github.com/trait…
fontikar Mar 20, 2024
7eac4c6
Skip snapshots on CI
fontikar Mar 20, 2024
5e9311e
Updated snaps
fontikar Mar 20, 2024
428f790
Removed snaps as a testing framework and using expect_equal #1
fontikar Mar 20, 2024
3e4d34b
Removed skip_on_ci #1
fontikar Mar 20, 2024
cd7bb8c
Updating roxygen version and package doc
fontikar Mar 22, 2024
7b79404
Updated code to generate testing data
fontikar Mar 22, 2024
a76881f
Moved helper code in generating fake data, removed internal testing data
fontikar Mar 22, 2024
797316c
Updated rmot_assign and tests passing I think
fontikar Mar 22, 2024
4536570
Completed testing workflow
fontikar Mar 24, 2024
a62a753
Added filepath to saving rds and updated TO DO in assign_data
fontikar Mar 25, 2024
4fa7bd5
Set tolerance
fontikar Mar 25, 2024
5c46049
Updated version for checkout
fontikar Mar 25, 2024
d6c7d95
Added constant data generation and tests based on model output summar…
Tess-LaCoil Mar 26, 2024
d0f608e
Init comit for constant branch
fontikar Mar 27, 2024
deac9fe
Tess helper funcs
fontikar Mar 27, 2024
2c6fb61
Regenerate test data using stan seed method
dfalster Mar 27, 2024
bc44f9f
using rstan::extract instead of summary
fontikar Mar 28, 2024
ffaab78
Added constant as trigger
fontikar Mar 28, 2024
8d3b268
Moved unique code back to make_constant.R
fontikar Apr 3, 2024
cc8b9e3
Changed test data generation and single- and multi-individual testing…
Tess-LaCoil Apr 3, 2024
d72e601
Fixed linear regression model testing.
Tess-LaCoil Apr 3, 2024
297f915
Resolving issues raised in pull request review: changed the set-up of…
Tess-LaCoil Apr 4, 2024
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
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
^LICENSE\.md$
^\.github$
^codecov\.yml$
^data-raw$
^tests/testthat/_snaps/$

6 changes: 3 additions & 3 deletions .github/workflows/R-CMD-check.yaml
dfalster marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
branches: [main, master, constant]
pull_request:
branches: [main, master]
branches: [main, master, develop]

name: R-CMD-check

Expand All @@ -29,7 +29,7 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
branches: [main, master, develop]

name: test-coverage

Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ rsconnect/
inst/doc
/doc/
/Meta/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to exclude this stuff. FYI, I have these files as a general exclusion in my global gitignore, i.e. applying to all projects. Saves setting it up for each project. I'll send you a copy.


# Pre-compiled stan files
*.o
*.so

# Bugged directory from snapshot package
tests/testthat/_snaps
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Biarch: true
Depends:
R (>= 3.4.0)
Expand All @@ -36,6 +36,7 @@ SystemRequirements: GNU make
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr
VignetteBuilder: knitr
Config/testthat/edition: 3
1 change: 1 addition & 0 deletions R/rmot-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
#' @references
#' Stan Development Team (NA). RStan: the R interface to Stan. R package version 2.26.23. https://mc-stan.org
#'
"_PACKAGE"
NULL
7 changes: 4 additions & 3 deletions R/rmot_assign_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' rmot_model("linear") |> rmot_assign_data(X = Loblolly$age, Y = Loblolly$height)
rmot_assign_data <- function(model_template, ...){
# Grab user expressions
user_code <- rlang::enexprs(..., .check_assign = TRUE)
user_code <- rlang::enquos(..., .check_assign = TRUE)

# Grab the names
fields <- names(user_code)
Expand All @@ -19,13 +19,14 @@ rmot_assign_data <- function(model_template, ...){

# Evaluate the RHS of expressions (the values)
data <- purrr::map(user_code,
eval)
~rlang::eval_tidy(.x, env = rlang::caller_env())
)

for(i in fields){
model_template <- purrr::list_modify(model_template, !!!data[i])
}

#TODO: Check if N is supplied, if not, assign by default to length(X) and give warning
#TODO: Check if N is supplied, if not, give error

return(model_template)
}
34 changes: 24 additions & 10 deletions R/rmot_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ rmot_model <- function(model=NULL){

output <- switch(model,
linear = rmot_lm(),
constant_single = rmot_cgs())
constant_single_ind = rmot_const_single_ind(),
constant_multi_ind = rmot_const_multi_ind())

class(output) <- "rmot_object"

Expand All @@ -32,19 +33,32 @@ rmot_lm <- function(){
model = "linear")
}

#' Data configuration template for constant growth single individual model
#' @keywords internal
#' @noRd

rmot_const_single_ind <- function(){
list(n_obs = NULL,
y_obs = NULL,
obs_index = NULL,
time = NULL,
y_0_obs = NULL,
model = "constant_single_ind")
}

#' Data configuration template for constant growth single species model
#' @keywords internal
#' @noRd

rmot_cgs <- function(){
list(N_obs = NULL,
N_ind = NULL,
S_obs = NULL,
census = NULL,
census_interval = NULL,
id_factor = NULL,
S_0_obs = NULL,
model = "constant_single")
rmot_const_multi_ind <- function(){
list(n_obs = NULL,
n_ind = NULL,
y_obs = NULL,
obs_index = NULL,
time = NULL,
ind_id = NULL,
y_0_obs = NULL,
model = "constant_multi_ind")
}


Expand Down
3 changes: 2 additions & 1 deletion R/rmot_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ rmot_run <- function(model_template, ...) {
# Detect model
out <- switch(model_template$model,
linear = rstan::sampling(stanmodels$linear, data = model_template, ...),
constant_single = rstan::sampling(stanmodels$constant_single, data = model_template, ...))
constant_single_ind = rstan::sampling(stanmodels$constant_single_ind, data = model_template, ...),
constant_multi_ind = rstan::sampling(stanmodels$constant_multi_ind, data = model_template, ...))

return(out)
}
5 changes: 3 additions & 2 deletions R/stanmodels.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Generated by rstantools. Do not edit by hand.

# names of stan models
stanmodels <- c("constant_single", "linear")
stanmodels <- c("constant_multi_ind", "constant_single_ind", "linear")

# load each stan module
Rcpp::loadModule("stan_fit4constant_single_mod", what = TRUE)
Rcpp::loadModule("stan_fit4constant_multi_ind_mod", what = TRUE)
Rcpp::loadModule("stan_fit4constant_single_ind_mod", what = TRUE)
Rcpp::loadModule("stan_fit4linear_mod", what = TRUE)

# instantiate each stanmodel object
Expand Down
97 changes: 97 additions & 0 deletions inst/stan/constant_multi_ind.stan
dfalster marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//Constant Growth - Single species
functions{
//Growth function
real growth(real beta){
return beta;
}
}

// Data structure
data {
int n_obs;
int n_ind;
real y_obs[n_obs];
int obs_index[n_obs];
real time[n_obs];
int ind_id[n_obs];
real y_0_obs[n_ind];
}

// The parameters accepted by the model.
parameters {
//Individual level
real<lower=0> ind_y_0[n_ind];
real<lower=0> ind_beta[n_ind];

real species_beta_mu;
real<lower=0> species_beta_sigma;

//Global level
real<lower=0> global_error_sigma;
}

// The model to be estimated.
model {
real y_hat[n_obs];
real Delta_hat[n_obs];

for(i in 1:n_obs){

//Fits the first size
if(obs_index[i]==1){
y_hat[i] = ind_y_0[ind_id[i]];
}

// Estimate growth rate
Delta_hat[i] = growth(ind_beta[ind_id[i]]);

// Update next size if relevant
if(i < n_obs){
if(ind_id[i+1]==ind_id[i]){
//Analytic solution
y_hat[i+1] = y_hat[i] + Delta_hat[i]*(time[i+1]-time[i]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand there's an analytic solution which is exact, but at first glance this looks like a Euler step. I think to would better to write as something like

y_hat[i+1] = step(y_hat[I], time[I], time[i+1], pars);

i.e. similar syntax to what is sued when you have Canham or other functions. Then put the
details of the step in the step function at the not.

}
}
}

//Likelihood
y_obs ~ normal(y_hat, global_error_sigma);

//Priors
//Individual level
ind_y_0 ~ normal(y_0_obs, global_error_sigma);
ind_beta ~ lognormal(species_beta_mu,
species_beta_sigma);

//Species level
species_beta_mu ~ normal(0.1, 1);
species_beta_sigma ~cauchy(0.1, 1);

//Global level
global_error_sigma ~cauchy(0.1, 1);
}

// The output
generated quantities {
real y_hat[n_obs];
real Delta_hat[n_obs];

for(i in 1:n_obs){

//Fits the first size
if(obs_index[i]==1){
y_hat[i] = ind_y_0[ind_id[i]];
}

// Estimate growth rate
Delta_hat[i] = growth(ind_beta[ind_id[i]]);

// Update next size if relevant
if(i < n_obs){
if(ind_id[i+1]==ind_id[i]){
//Analytic solution
y_hat[i+1] = y_hat[i] + Delta_hat[i]*(time[i+1]-time[i]);
}
}
}
}
83 changes: 0 additions & 83 deletions inst/stan/constant_single.stan

This file was deleted.

Loading
Loading