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 single individual #6

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 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
094c7e2
Added Canham model, data generation. Changed format of data generatio…
Tess-LaCoil Mar 26, 2024
606cbd5
Reloading package in full allowed for compilation of Canham model. Ca…
Tess-LaCoil Mar 26, 2024
0881ee7
Re-ran data generation for constant models. Tests now run properly.
Tess-LaCoil Mar 26, 2024
786b28a
Added von Bertalanffy model functions.
Tess-LaCoil Mar 26, 2024
671b091
Merge branch 'vb_model_add' into constant_single_individual
Tess-LaCoil Mar 26, 2024
555f397
Added set.seed resets and re-ran von Bertalanffy data generation to k…
Tess-LaCoil Mar 26, 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
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.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: R-CMD-check

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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ rsconnect/
inst/doc
/doc/
/Meta/

# Pre-compiled stan files
*.o
*.so
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
89 changes: 89 additions & 0 deletions inst/stan/constant_multi_ind.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//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){
if(ind_id[i+1]==ind_id[i]){
if(obs_index[i]==1){//Fits the first size
y_hat[i] = ind_y_0[ind_id[i]];
}

if(i < n_obs){ //Analytic solution
Delta_hat[i] = growth(ind_beta[ind_id[i]]);
y_hat[i+1] = y_hat[i] + Delta_hat[i]*(time[i+1]-time[i]);
}
} else {
Delta_hat[i] = 0; //Gives 0 as the growth estimate for the last data point.
}
}

//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){
if(ind_id[i+1]==ind_id[i]){
if(obs_index[i]==1){//Fits the first size
y_hat[i] = ind_y_0[ind_id[i]];
}

if(i < n_obs){ //Analytic solution
Delta_hat[i] = growth(ind_beta[ind_id[i]]);
y_hat[i+1] = y_hat[i] + Delta_hat[i]*(time[i+1]-time[i]);
}
} else {
Delta_hat[i] = 0; //Gives 0 as the growth estimate for the last data point.
}
}
}
83 changes: 0 additions & 83 deletions inst/stan/constant_single.stan

This file was deleted.

75 changes: 75 additions & 0 deletions inst/stan/constant_single_ind.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//Constant Growth - Single species
functions{
//Growth function
real growth(real y, real beta){
return beta;
}
}

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

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

//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){
if(obs_index[i]==1){//Fits the first size
y_hat[i] = ind_y_0;
}

if(i < n_obs){ //Analytic solution
Delta_hat[i] = growth(y_hat[i], ind_beta);
y_hat[i+1] = y_hat[i] + Delta_hat[i]*(time[i+1]-time[i]);
} else {
Delta_hat[i] = 0;
}
}

//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(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){
if(obs_index[i]==1){//Fits the first size
y_hat[i] = ind_y_0;
}

if(i < n_obs){ //Analytic solution
Delta_hat[i] = growth(y_hat[i], ind_beta);
y_hat[i+1] = y_hat[i] + Delta_hat[i]*(time[i+1]-time[i]);
} else {
Delta_hat[i] = 0;
}
}
}
1 change: 0 additions & 1 deletion rmot.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ LineEndingConversion: Posix
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
6 changes: 4 additions & 2 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif


RcppExport SEXP _rcpp_module_boot_stan_fit4constant_single_mod();
RcppExport SEXP _rcpp_module_boot_stan_fit4constant_multi_ind_mod();
RcppExport SEXP _rcpp_module_boot_stan_fit4constant_single_ind_mod();
RcppExport SEXP _rcpp_module_boot_stan_fit4linear_mod();

static const R_CallMethodDef CallEntries[] = {
{"_rcpp_module_boot_stan_fit4constant_single_mod", (DL_FUNC) &_rcpp_module_boot_stan_fit4constant_single_mod, 0},
{"_rcpp_module_boot_stan_fit4constant_multi_ind_mod", (DL_FUNC) &_rcpp_module_boot_stan_fit4constant_multi_ind_mod, 0},
{"_rcpp_module_boot_stan_fit4constant_single_ind_mod", (DL_FUNC) &_rcpp_module_boot_stan_fit4constant_single_ind_mod, 0},
{"_rcpp_module_boot_stan_fit4linear_mod", (DL_FUNC) &_rcpp_module_boot_stan_fit4linear_mod, 0},
{NULL, NULL, 0}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#include <Rcpp.h>
using namespace Rcpp ;
#include "stanExports_constant_single.h"
#include "stanExports_constant_multi_ind.h"

RCPP_MODULE(stan_fit4constant_single_mod) {
RCPP_MODULE(stan_fit4constant_multi_ind_mod) {


class_<rstan::stan_fit<stan_model, boost::random::ecuyer1988> >("rstantools_model_constant_single")
class_<rstan::stan_fit<stan_model, boost::random::ecuyer1988> >("rstantools_model_constant_multi_ind")

.constructor<SEXP,SEXP,SEXP>()

Expand Down
Loading