## Example 1 - Fitting a primary model --------------------------------------
## A dummy dataset describing the variation of the population size
my_data <- data.frame(time = c(0, 25, 50, 75, 100),
logN = c(2, 2.5, 7, 8, 8))
## A list of model keys can be gathered from
primary_model_data()
## The primary model is defined as a list
models <- list(primary = "Baranyi")
## The keys of the model parameters can also be gathered from primary_model_data
primary_model_data("Baranyi")$pars
## Any model parameter can be fixed
known <- c(mu = .2)
## The remaining parameters need initial guesses
start <- c(logNmax = 8, lambda = 25, logN0 = 2)
primary_fit <- fit_growth(my_data, models, start, known,
environment = "constant",
)
## The instance of FitIsoGrowth includes several useful methods
print(primary_fit)
plot(primary_fit)
coef(primary_fit)
summary(primary_fit)
## time_to_size can be used to calculate the time for some concentration
time_to_size(primary_fit, 4)
## Example 2 - Fitting under dynamic conditions------------------------------
## We will use the example data included in the package
data("example_dynamic_growth")
## And the example environmental conditoins (temperature & aw)
data("example_env_conditions")
## Valid keys for secondary models can be retrived from
secondary_model_data()
## We need to assign a model equation (secondary model) to each environmental factor
sec_models <- list(temperature = "CPM", aw = "CPM")
## The keys of the model parameters can be gathered from the same function
secondary_model_data("CPM")$pars
## Any model parameter (of the primary or secondary models) can be fixed
known_pars <- list(Nmax = 1e4, # Primary model
N0 = 1e0, Q0 = 1e-3, # Initial values of the primary model
mu_opt = 4, # mu_opt of the gamma model
temperature_n = 1, # Secondary model for temperature
aw_xmax = 1, aw_xmin = .9, aw_n = 1 # Secondary model for water activity
)
## The rest, need initial guesses (you know, regression)
my_start <- list(temperature_xmin = 25, temperature_xopt = 35,
temperature_xmax = 40, aw_xopt = .95)
## We can now fit the model
# \donttest{
dynamic_fit <- fit_growth(example_dynamic_growth,
sec_models,
my_start, known_pars,
environment = "dynamic",
env_conditions = example_env_conditions
)
## The instance of FitDynamicGrowth has several S3 methods
plot(dynamic_fit, add_factor = "temperature")
summary(dynamic_fit)
## We can use time_to_size to calculate the time required to reach a given size
time_to_size(dynamic_fit, 3)
# }
## Example 3- Fitting under dynamic conditions using MCMC -------------------
## We can reuse most of the arguments from the previous example
## We just need to define the algorithm and the number of iterations
# \donttest{
set.seed(12421)
MCMC_fit <- fit_growth(example_dynamic_growth,
sec_models,
my_start, known_pars,
environment = "dynamic",
env_conditions = example_env_conditions,
algorithm = "MCMC",
niter = 1000
)
## The instance of FitDynamicGrowthMCMC has several S3 methods
plot(MCMC_fit, add_factor = "aw")
summary(MCMC_fit)
## We can use time_to_size to calculate the time required to reach a given size
time_to_size(MCMC_fit, 3)
## It can also make growth predictions including uncertainty
uncertain_growth <- predictMCMC(MCMC_fit,
seq(0, 10, length = 1000),
example_env_conditions,
niter = 1000)
## The instance of MCMCgrowth includes several nice S3 methods
plot(uncertain_growth)
print(uncertain_growth)
## time_to_size can calculate the time to reach some count
time_to_size(uncertain_growth, 2)
time_to_size(uncertain_growth, 2, type = "distribution")
# }
## Example 4 - Fitting a unique model to several dynamic experiments --------
## We will use the data included in the package
data("multiple_counts")
data("multiple_conditions")
## We need to assign a model equation for each environmental factor
sec_models <- list(temperature = "CPM", pH = "CPM")
## Any model parameter (of the primary or secondary models) can be fixed
known_pars <- list(Nmax = 1e8, N0 = 1e0, Q0 = 1e-3,
temperature_n = 2, temperature_xmin = 20,
temperature_xmax = 35,
pH_n = 2, pH_xmin = 5.5, pH_xmax = 7.5, pH_xopt = 6.5)
## The rest, need initial guesses
my_start <- list(mu_opt = .8, temperature_xopt = 30)
## We can now fit the model
# \donttest{
global_fit <- fit_growth(multiple_counts,
sec_models,
my_start,
known_pars,
environment = "dynamic",
algorithm = "regression",
approach = "global",
env_conditions = multiple_conditions
)
## The instance of FitMultipleDynamicGrowth has nice S3 methods
plot(global_fit)
summary(global_fit)
print(global_fit)
## We can use time_to_size to calculate the time to reach a given size
time_to_size(global_fit, 4.5)
# }
## Example 5 - MCMC fitting a unique model to several dynamic experiments ---
## Again, we can re-use all the arguments from the previous example
## We just need to define the right algorithm and the number of iterations
## On top of that, we will also pass upper and lower bounds to modMCMC
# \donttest{
set.seed(12421)
global_MCMC <- fit_growth(multiple_counts,
sec_models,
my_start,
known_pars,
environment = "dynamic",
algorithm = "MCMC",
approach = "global",
env_conditions = multiple_conditions,
niter = 1000,
lower = c(.2, 29), # lower limits of the model parameters
upper = c(.8, 34) # upper limits of the model parameters
)
## The instance of FitMultipleDynamicGrowthMCMC has nice S3 methods
plot(global_MCMC)
summary(global_MCMC)
print(global_MCMC)
## We can use time_to_size to calculate the time to reach a given size
time_to_size(global_MCMC, 3)
## It can also be used to make model predictions with parameter uncertainty
uncertain_prediction <- predictMCMC(global_MCMC,
seq(0, 50, length = 1000),
multiple_conditions[[1]],
niter = 100
)
## The instance of MCMCgrowth includes several nice S3 methods
plot(uncertain_growth)
print(uncertain_growth)
## time_to_size can calculate the time to reach some count
time_to_size(uncertain_growth, 2)
time_to_size(uncertain_growth, 2, type = "distribution")
# }
Run the code above in your browser using DataLab