Skip to content

Commit

Permalink
Merge branch 'master' into function-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tess-LaCoil authored Aug 14, 2024
2 parents 9930a85 + 00fff73 commit 9244ad2
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 149 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
^.*\.Rproj$
^\.Rproj\.user$
.github
LICENSE.md
codecov.yml
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Biarch: true
Depends:
R (>= 3.4.0)
R (>= 3.5.0)
Imports:
methods,
dplyr,
Expand Down
20 changes: 10 additions & 10 deletions inst/stan/canham_multi_ind.stan
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ parameters {
//Individual level
real<lower=0> ind_y_0[n_ind];
real<lower=0> ind_max_growth[n_ind];
real<lower=0> ind_diameter_at_max_growth[n_ind];
real<lower=0> ind_size_at_max_growth[n_ind];
real<lower=0> ind_k[n_ind];

//Population level
real pop_max_growth_mean;
real<lower=0> pop_max_growth_sd;
real pop_diameter_at_max_growth_mean;
real<lower=0> pop_diameter_at_max_growth_sd;
real pop_size_at_max_growth_mean;
real<lower=0> pop_size_at_max_growth_sd;
real pop_k_mean;
real<lower=0> pop_k_sd;

Expand All @@ -88,7 +88,7 @@ model {
for(i in 1:n_obs){
// Initialise the parameters for the observation
pars[1] = ind_max_growth[ind_id[i]];
pars[2] = ind_diameter_at_max_growth[ind_id[i]];
pars[2] = ind_size_at_max_growth[ind_id[i]];
pars[3] = ind_k[ind_id[i]];

if(obs_index[i]==1){//Fits the first size
Expand All @@ -111,16 +111,16 @@ model {
ind_y_0 ~ normal(y_0_obs, global_error_sigma);
ind_max_growth ~lognormal(pop_max_growth_mean,
pop_max_growth_sd);
ind_diameter_at_max_growth ~lognormal(pop_diameter_at_max_growth_mean,
pop_diameter_at_max_growth_sd);
ind_size_at_max_growth ~lognormal(pop_size_at_max_growth_mean,
pop_size_at_max_growth_sd);
ind_k ~lognormal(pop_k_mean,
pop_k_sd);

//Population level
//Species level
pop_max_growth_mean ~normal(0, 1);
pop_max_growth_sd ~cauchy(0, 1);
pop_diameter_at_max_growth_mean ~normal(0, 1);
pop_diameter_at_max_growth_sd ~cauchy(0, 1);
pop_size_at_max_growth_mean ~normal(0, 1);
pop_size_at_max_growth_sd ~cauchy(0, 1);
pop_k_mean ~normal(0, 1);
pop_k_sd ~cauchy(0, 1);

Expand All @@ -136,7 +136,7 @@ generated quantities{
for(i in 1:n_obs){
// Initialise the parameters for the observation
pars[1] = ind_max_growth[ind_id[i]];
pars[2] = ind_diameter_at_max_growth[ind_id[i]];
pars[2] = ind_size_at_max_growth[ind_id[i]];
pars[3] = ind_k[ind_id[i]];

if(obs_index[i]==1){//Fits the first size
Expand Down
264 changes: 132 additions & 132 deletions inst/stan/canham_single_ind.stan
Original file line number Diff line number Diff line change
@@ -1,132 +1,132 @@
//Growth function
functions{
//Growth function for use with Runge-Kutta method
//pars = (g_max, s_max, k)
real DE(real y, vector pars){
return pars[1] *
exp(-0.5 * pow(log(y / pars[2]) / pars[3], 2));
}

real rk4_step(real y, vector pars, real interval){
real k1;
real k2;
real k3;
real k4;
real y_hat;

k1 = DE(y, pars);
k2 = DE(y+interval*k1/2.0, pars);
k3 = DE(y+interval*k2/2.0, pars);
k4 = DE(y+interval*k3, pars);

y_hat = y + (1.0/6.0) * (k1 + 2.0*k2 + 2.0*k3 + k4) * interval;

return y_hat;
}

real rk4(real y, vector pars, real interval, real step_size){
int steps;
real duration;
real y_hat;
real step_size_temp;

duration = 0;
y_hat = y;

while(duration < interval){
//Determine the relevant step size
step_size_temp = min([step_size, interval-duration]);

//Get next size estimate
y_hat = rk4_step(y_hat, pars, step_size_temp);

//Increment observed duration
duration = duration + step_size_temp;
}

return y_hat;
}
}

// Data structure
data {
real step_size;
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_max_growth;
real<lower=0> ind_diameter_at_max_growth;
real<lower=0> ind_k;

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

// The model to be estimated.
model {
real y_hat[n_obs];
vector[3] pars;

pars[1] = ind_max_growth;
pars[2] = ind_diameter_at_max_growth;
pars[3] = ind_k;

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){
//Estimate next size
y_hat[i+1] = rk4(y_hat[i], pars, (time[i+1] - time[i]), step_size);
}
}

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

//Priors
//Individual level
ind_y_0 ~ normal(y_0_obs, global_error_sigma);
ind_max_growth ~lognormal(0, 1);
ind_diameter_at_max_growth ~lognormal(3, 1);
ind_k ~lognormal(0, 1);

//Global level
global_error_sigma ~cauchy(0,5);
}

generated quantities{
real y_hat[n_obs];
real Delta_hat[n_obs];
vector[3] pars;

pars[1] = ind_max_growth;
pars[2] = ind_diameter_at_max_growth;
pars[3] = ind_k;

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){
//Estimate next size
y_hat[i+1] = rk4(y_hat[i], pars, (time[i+1] - time[i]), step_size);
Delta_hat[i] = y_hat[i+1] - y_hat[i];

} else {
Delta_hat[i] = DE(y_hat[i], pars)*(time[i] - time[i-1]);
}
}
}
//Growth function
functions{
//Growth function for use with Runge-Kutta method
//pars = (g_max, s_max, k)
real DE(real y, vector pars){
return pars[1] *
exp(-0.5 * pow(log(y / pars[2]) / pars[3], 2));
}

real rk4_step(real y, vector pars, real interval){
real k1;
real k2;
real k3;
real k4;
real y_hat;

k1 = DE(y, pars);
k2 = DE(y+interval*k1/2.0, pars);
k3 = DE(y+interval*k2/2.0, pars);
k4 = DE(y+interval*k3, pars);

y_hat = y + (1.0/6.0) * (k1 + 2.0*k2 + 2.0*k3 + k4) * interval;

return y_hat;
}

real rk4(real y, vector pars, real interval, real step_size){
int steps;
real duration;
real y_hat;
real step_size_temp;

duration = 0;
y_hat = y;

while(duration < interval){
//Determine the relevant step size
step_size_temp = min([step_size, interval-duration]);

//Get next size estimate
y_hat = rk4_step(y_hat, pars, step_size_temp);

//Increment observed duration
duration = duration + step_size_temp;
}

return y_hat;
}
}

// Data structure
data {
real step_size;
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_max_growth;
real<lower=0> ind_size_at_max_growth;
real<lower=0> ind_k;

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

// The model to be estimated.
model {
real y_hat[n_obs];
vector[3] pars;

pars[1] = ind_max_growth;
pars[2] = ind_size_at_max_growth;
pars[3] = ind_k;

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){
//Estimate next size
y_hat[i+1] = rk4(y_hat[i], pars, (time[i+1] - time[i]), step_size);
}
}

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

//Priors
//Individual level
ind_y_0 ~ normal(y_0_obs, global_error_sigma);
ind_max_growth ~lognormal(0, 1);
ind_size_at_max_growth ~lognormal(3, 1);
ind_k ~lognormal(0, 1);

//Global level
global_error_sigma ~cauchy(0,5);
}

generated quantities{
real y_hat[n_obs];
real Delta_hat[n_obs];
vector[3] pars;

pars[1] = ind_max_growth;
pars[2] = ind_size_at_max_growth;
pars[3] = ind_k;

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){
//Estimate next size
y_hat[i+1] = rk4(y_hat[i], pars, (time[i+1] - time[i]), step_size);
Delta_hat[i] = y_hat[i+1] - y_hat[i];

} else {
Delta_hat[i] = DE(y_hat[i], pars)*(time[i] - time[i-1]);
}
}
}
2 changes: 1 addition & 1 deletion inst/stan/constant_multi_ind.stan
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ model {
ind_beta ~ lognormal(pop_beta_mu,
pop_beta_sigma);

//pop level
//Population level
pop_beta_mu ~ normal(0.1, 1);
pop_beta_sigma ~cauchy(0.1, 1);

Expand Down
4 changes: 2 additions & 2 deletions inst/stan/power_multi_ind.stan
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ parameters {
real<lower=0> ind_coeff[n_ind];
real<lower=0> ind_power[n_ind];

//pop level
// Population level
real pop_coeff_mean;
real<lower=0> pop_coeff_sd;
real pop_power_mean;
Expand Down Expand Up @@ -110,7 +110,7 @@ model {
ind_coeff ~lognormal(pop_coeff_mean, pop_coeff_sd);
ind_power ~lognormal(pop_power_mean, pop_power_sd);

//pop level
// Population level
pop_coeff_mean ~normal(0, 2);
pop_coeff_sd ~cauchy(0, 2);
pop_power_mean ~normal(0, 2);
Expand Down
4 changes: 2 additions & 2 deletions inst/stan/vb_multi_ind.stan
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ parameters {
real<lower=0> ind_growth_par[n_ind];
real<lower=0> ind_max_size[n_ind];

//pop level
//Population level
real pop_growth_par_mean;
real<lower=0> pop_growth_par_sd;
real pop_max_size_mean;
Expand Down Expand Up @@ -109,7 +109,7 @@ model {
ind_growth_par ~lognormal(pop_growth_par_mean, pop_growth_par_sd);
ind_max_size ~lognormal(pop_max_size_mean, pop_max_size_sd);

//pop level
//Population level
pop_growth_par_mean ~normal(0, 2);
pop_growth_par_sd ~cauchy(0, 2);
pop_max_size_mean ~normal(max(log(y_obs)), 2);
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-hmde_models_canham.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test_that("Model structures: canham", {

test_that("Execution: canham single individual", {
model_name <- "canham"
par_names <- c("ind_max_growth", "ind_diameter_at_max_growth", "ind_k")
par_names <- c("ind_max_growth", "ind_size_at_max_growth", "ind_k")

hmde_test_single_individual(model_name, par_names)
})
Expand Down

0 comments on commit 9244ad2

Please sign in to comment.