### Fit growth model using MCMC with example data Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_MCMC_growth.Rd This example shows how to fit a growth model using MCMC with the example data included in the package. It defines secondary models, fixes known parameters, provides starting values, runs the MCMC algorithm, and plots the results. The seed is set for reproducibility. ```R ## We use the example data included in the package data("example_dynamic_growth") data("example_env_conditions") ## Definition of the secondary models sec_model_names <- c(temperature = "CPM", aw= "CPM") ## Any model parameter 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 ) ## We need starting values for the remaining parameters my_start <- list(temperature_xmin = 25, temperature_xopt = 35, temperature_xmax = 40, aw_xopt = .95) ## We can now call the fitting function set.seed(12124) # Setting seed for repeatability my_MCMC_fit <- fit_MCMC_growth(example_dynamic_growth, example_env_conditions, my_start, known_pars, sec_model_names, niter = 3000) ## Always check the MCMC chain!! plot(my_MCMC_fit$fit_results) ## We can compare data against fitted curve plot(my_MCMC_fit) ## Any environmental factor can be included using add_factor plot(my_MCMC_fit, add_factor = "temperature", label_y1 = "Count (log CFU/ml)", label_y2 = "Temperature (C)") ``` -------------------------------- ### Fit multiple growth experiments with fit_multiple_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_multiple_growth.Rd Example from the documentation. It loads the multiple_experiments data set, defines secondary models (CPM for temperature and pH), fixes known parameters, sets starting values, fits the model, and displays summary and plots. The example is wrapped in \donttest{}. ```r \donttest{ ## We will use the multiple_experiments data set data("multiple_experiments") ## For each environmental factor, we need to defined a model sec_names <- c(temperature = "CPM", pH = "CPM") ## Any model parameter can be fixed known <- 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 require starting values for model fitting start <- list(mu_opt = .8, temperature_xopt = 30) ## We can now call the fitting function global_fit <- fit_multiple_growth(start, multiple_experiments, known, sec_names) ## Parameter estimates can be retrieved with summary summary(global_fit) ## We can compare fitted model against observations plot(global_fit) ## Any single environmental factor can be added to the plot using add_factor plot(global_fit, add_factor = "temperature") } ``` -------------------------------- ### Fit dynamic growth model with example datasets Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_dynamic_growth.Rd Example from the documentation showing how to fit a dynamic growth model using the included datasets. It defines secondary models, fixes known parameters, sets starting values, calls fit_dynamic_growth(), and then summarizes and plots the results. ```R data("example_dynamic_growth") data("example_env_conditions") ## Define the secondary models sec_model_names <- c(temperature = "CPM", aw= "CPM") ## Any model parameter 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 remaining parameters need initial values my_start <- list(temperature_xmin = 25, temperature_xopt = 35, temperature_xmax = 40, aw_xopt = .95) ## We can now call the fitting function my_dyna_fit <- fit_dynamic_growth(example_dynamic_growth, example_env_conditions, my_start, known_pars, sec_model_names) summary(my_dyna_fit) ## We can compare the data and the fitted curve plot(my_dyna_fit) ## We can plot any environmental condition using add_factor plot(my_dyna_fit, add_factor = "aw", label_y1 = "Log count (log CFU/ml)", label_y2 = "Water activity") ``` -------------------------------- ### Create StochasticGrowth model for distribution_to_logcount example Source: https://github.com/albgarre/biogrowth/blob/master/man/distribution_to_logcount.Rd Example setup for distribution_to_logcount(): defines model parameters and generates a StochasticGrowth object using predict_stochastic_growth(). The example is wrapped in \donttest{} and requires the tibble package. ```r my_model <- "modGompertz" my_times <- seq(0, 30, length = 100) n_sims <- 3000 library(tibble) pars <- tribble( ~par, ~mean, ~sd, ~scale, "logN0", 0, .2, "original", "mu", 2, .3, "sqrt", "lambda", 4, .4, "sqrt", "C", 6, .5, "original" ) stoc_growth <- predict_stochastic_growth(my_model, my_times, n_sims, pars) ``` -------------------------------- ### Define starting parameters for dynamic fitting Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Creates a list of starting values for the model parameters to be estimated. The list includes temperature_xmin, temperature_xopt, and aw_xopt. ```r my_start <- list(temperature_xmin = 25, temperature_xopt = 35, aw_xopt = .95) ``` -------------------------------- ### Define known and starting parameters Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Defines known parameters (`known`) and starting values (`start`) for the model fit. Only `mu_opt` and `temperature_xopt` are estimated. ```r known <- 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) start <- list(mu_opt = .8, temperature_xopt = 30) ``` -------------------------------- ### Load and inspect example dataset example_coupled_onestep Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v06_coupledmodels.Rmd Loads the example dataset included in the package and displays its first rows. The dataset must have columns 'temp', 'time', and 'logN'. ```r data("example_coupled_onestep") head(example_coupled_onestep) ``` -------------------------------- ### Load example datasets for dynamic environment Source: https://github.com/albgarre/biogrowth/blob/master/man/check_growth_guess.Rd Loads the example_dynamic_growth and example_env_conditions datasets included in the package. ```R data("example_dynamic_growth") data("example_env_conditions") ``` -------------------------------- ### Create example data for constant environment Source: https://github.com/albgarre/biogrowth/blob/master/man/check_growth_guess.Rd Creates a data frame with time and logN columns for use in constant environment examples. ```R my_data <- data.frame(time = 0:9, logN = c(2, 2.1, 1.8, 2.5, 3.1, 3.4, 4, 4.5, 4.8, 4.7) ) ``` -------------------------------- ### Example: predict_MCMC_growth with fit_MCMC_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/predict_MCMC_growth.Rd Complete example showing how to fit a dynamic growth model with fit_MCMC_growth, define simulation conditions, call predict_MCMC_growth, and plot the prediction interval. Requires example_dynamic_growth and example_env_conditions datasets. The example sets a seed for reproducibility and uses niter = 3000 for fitting and niter = 2000 for predictions. ```R \donttest{ ## We need a FitDynamicGrowthMCMC object data("example_dynamic_growth") data("example_env_conditions") sec_model_names <- c(temperature = "CPM", aw= "CPM") 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 ) my_start <- list(temperature_xmin = 25, temperature_xopt = 35, temperature_xmax = 40, aw_xopt = .95) set.seed(12124) # Setting seed for repeatability my_MCMC_fit <- fit_MCMC_growth(example_dynamic_growth, example_env_conditions, my_start, known_pars, sec_model_names, niter = 3000) ## Define the conditions for the simulation my_times <- seq(0, 15, length = 50) niter <- 2000 newpars <- list(N0 = 1e-1, # A parameter that was fixed temperature_xmax = 120 # A parameter that was fitted ) ## Make the simulations my_MCMC_prediction <- predict_MCMC_growth(my_MCMC_fit, my_times, example_env_conditions, # It could be different from the one used for fitting niter, newpars) ## We can plot the prediction interval plot(my_MCMC_prediction) ## We can also get the quantiles at each time point print(my_MCMC_prediction$quantiles) } ``` -------------------------------- ### Define model parameters and data for MCMC fitting Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Loads example data and defines secondary model names, known parameters, and starting values for the MCMC fitting process. ```r data("example_dynamic_growth") data("example_env_conditions") sec_model_names <- c(temperature = "CPM", aw= "CPM") 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, temperature_xmax = 40, # Secondary model for temperature aw_xmax = 1, aw_xmin = .9, aw_n = 1 # Secondary model for water activity ) my_start <- list(temperature_xmin = 25, temperature_xopt = 35, aw_xopt = .95) ``` -------------------------------- ### Load multiple_experiments dataset Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Loads the example dataset `multiple_experiments` used with `fit_multiple_growth()`. ```r data("multiple_experiments") ``` -------------------------------- ### Install biogrowth development version from GitHub Source: https://github.com/albgarre/biogrowth/blob/master/README.Rmd Installs the development version of biogrowth from GitHub using devtools::install_github(). Requires the devtools package, which is commented out for installation. ```R # install.packages("devtools") devtools::install_github("albgarre/biogrowth") ``` -------------------------------- ### Simulate growth with parameter uncertainty using predict_growth_uncertainty Source: https://github.com/albgarre/biogrowth/blob/master/man/predict_growth_uncertainty.Rd Defines simulation settings (model, times, number of simulations, and parameter uncertainty tibble), calls predict_growth_uncertainty, plots the result, adds a correlation matrix, and uses time_to_size to get the median time to reach a size and its distribution. The example is wrapped in \donttest{} and requires the tibble package. ```r \donttest{ ## Definition of the simulation settings my_model <- "Baranyi" my_times <- seq(0, 30, length = 100) n_sims <- 3000 library(tibble) pars <- tribble( ~par, ~mean, ~sd, ~scale, "logN0", 0, .2, "original", "mu", 2, .3, "sqrt", "lambda", 4, .4, "sqrt", "logNmax", 6, .5, "original" ) ## Calling the function stoc_growth <- predict_growth_uncertainty(my_model, my_times, n_sims, pars) ## We can plot the results plot(stoc_growth) ## Adding parameter correlation my_cor <- matrix(c(1, 0, 0, 0, 0, 1, 0.7, 0, 0, 0.7, 1, 0, 0, 0, 0, 1), nrow = 4) stoc_growth2 <- predict_growth_uncertainty(my_model, my_times, n_sims, pars, my_cor) plot(stoc_growth2) ## The time_to_size function can calculate the median growth curve to reach a size time_to_size(stoc_growth, 4) ## Or the distribution of times dist <- time_to_size(stoc_growth, 4, type = "distribution") plot(dist) } ``` -------------------------------- ### Fit multiple dynamic growth experiments with fit_multiple_growth_MCMC Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_multiple_growth_MCMC.Rd Example workflow for fitting a growth model to multiple dynamic experiments using fit_multiple_growth_MCMC. It loads the multiple_experiments dataset, defines secondary models (CPM for temperature and pH), fixes known parameters, sets starting values, runs the MCMC with 1000 iterations and lower/upper bounds, then summarizes and plots the results. The function is superseded by fit_growth. ```R \donttest{ ## We will use the multiple_experiments data set data("multiple_experiments") ## For each environmental factor, we need to defined a model sec_names <- c(temperature = "CPM", pH = "CPM") ## Any model parameter can be fixed known <- 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 require starting values for model fitting start <- list(mu_opt = .8, temperature_xopt = 30) ## We can now call the fitting function set.seed(12412) global_MCMC <- fit_multiple_growth_MCMC(start, multiple_experiments, known, sec_names, niter = 1000, lower = c(.2, 29), # lower limits of the model parameters upper = c(.8, 34)) # upper limits of the model parameters ## Parameter estimates can be retrieved with summary summary(global_MCMC) ## We can compare fitted model against observations plot(global_MCMC) ## Any single environmental factor can be added to the plot using add_factor plot(global_MCMC, add_factor = "temperature") } ``` -------------------------------- ### Two-steps fitting example for make_guess_coupled Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_coupled.Rd Shows how to generate initial guesses for the two_steps mode using example_coupled_twosteps, visualize them with show_guess_coupled, and fit the model with fit_coupled_growth. The fit results are printed and summarized. ```R data(example_coupled_twosteps) guess <- make_guess_coupled(example_coupled_twosteps) show_guess_coupled(example_coupled_twosteps, guess) my_fit <- fit_coupled_growth(example_coupled_twosteps, start = guess, mode = "two_steps") print(my_fit) coef(my_fit) summary(my_fit) plot(my_fit) ``` -------------------------------- ### One-step fitting example for make_guess_coupled Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_coupled.Rd Shows how to generate initial guesses for the one_step mode using example_coupled_onestep, visualize them with show_guess_coupled, and fit the model with fit_coupled_growth. The fit results are printed and summarized. ```R data("example_coupled_onestep") guess <- make_guess_coupled(example_coupled_onestep, mode = "one_step") show_guess_coupled(example_coupled_onestep, guess, "one_step") my_fit <- fit_coupled_growth(example_coupled_onestep, start = guess, mode = "one_step") print(my_fit) coef(my_fit) summary(my_fit) plot(my_fit) ``` -------------------------------- ### Display example environmental conditions Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Shows the tibble example_env_conditions that defines the dynamic environmental conditions used in the prediction examples. ```r example_env_conditions ``` -------------------------------- ### Create example data for make_guess_primary Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_primary.Rd Creates a data frame with time and logN columns representing experimental growth data. ```R my_data <- data.frame(time = 0:9, logN = c(2, 2.1, 1.8, 2.5, 3.1, 3.4, 4, 4.5, 4.8, 4.7)) ``` -------------------------------- ### Fit dynamic growth model with fit_dynamic_growth() Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Calls fit_dynamic_growth() with example data, environmental conditions, starting parameters, known parameters, and secondary model names. The function checks parameter validity and returns a FitDynamicGrowth object. ```r my_dyna_fit <- fit_dynamic_growth(example_dynamic_growth, example_env_conditions, my_start, known_pars, sec_model_names) ``` -------------------------------- ### Install biogrowth from CRAN Source: https://github.com/albgarre/biogrowth/blob/master/README.Rmd Installs the released version of the biogrowth package from CRAN using install.packages(). ```R install.packages("biogrowth") ``` -------------------------------- ### Fit growth under dynamic conditions with fit_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/compare_growth_fits.Rd Example of fitting a growth model under dynamic environmental conditions using example datasets and secondary models. ```R data("example_dynamic_growth") data("example_env_conditions") ``` ```R sec_models <- list(temperature = "CPM", aw = "CPM") ``` ```R known_pars <- list(Nmax = 1e4, N0 = 1e0, Q0 = 1e-3, mu_opt = 4, temperature_n = 1, aw_xmax = 1, aw_xmin = .9, aw_n = 1 ) ``` ```R my_start <- list(temperature_xmin = 25, temperature_xopt = 35, temperature_xmax = 40, aw_xopt = .95) ``` ```R dynamic_fit <- fit_growth(example_dynamic_growth, sec_models, my_start, known_pars, environment = "dynamic", env_conditions = example_env_conditions ) ``` -------------------------------- ### Define starting values for remaining parameters Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Sets initial guesses for parameters not fixed, close to those from make_guess_secondary(). ```R my_start <- list(temperature_xmin = 5, temperature_xopt = 35, pH_xopt = 6.5) ``` -------------------------------- ### Load biogrowth and tidyverse packages Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v01_growth_predictions.Rmd Loads the `biogrowth` and `tidyverse` packages, which are required for the examples in this vignette. ```r library(biogrowth) library(tidyverse) ``` -------------------------------- ### Fit secondary growth model with example_cardinal data Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_secondary_growth.Rd Loads example data, defines secondary model names, fixes known parameters, sets initial values, and calls fit_secondary_growth. Then uses summary and plot to inspect results. ```R data("example_cardinal") ## Define the models to fit sec_model_names <- c(temperature = "Zwietering", pH = "CPM") ## Any model parameter can be fixed known_pars <- list(mu_opt = 1.2, temperature_n = 1, pH_n = 2, pH_xmax = 6.8, pH_xmin = 5.2) ## Initial values must be given for every other parameter my_start <- list(temperature_xmin = 5, temperature_xopt = 35, pH_xopt = 6.5) ## We can now call the fitting function fit_cardinal <- fit_secondary_growth(example_cardinal, my_start, known_pars, sec_model_names) ## With summary, we can look at the parameter estimates summary(fit_cardinal) ## The plot function compares predictions against observations plot(fit_cardinal) ## Passing which = 2, generates a different kind of plot plot(fit_cardinal, which = 2) plot(fit_cardinal, which = 2, add_trend = TRUE) plot(fit_cardinal, which = 2, add_segment = TRUE) ``` -------------------------------- ### Plot example OD dataset Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v07_serial_dilution.Rmd Loads the example_od dataset and creates a line plot of optical density over time for each sample, with the legend removed. ```r data("example_od") example_od %>% pivot_longer(-"time") %>% ggplot() + geom_line(aes(x = time, y = value, colour = name)) + theme(legend.position = "none") ``` -------------------------------- ### Define equivalent model parameters and predict growth Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v01_growth_predictions.Rmd Shows how to define a primary model using alternative parameter names (logN0, logNmax, mu, lambda) and then create an equivalent parameter list using N0, Nmax, mu_opt, and Q0, where Q0 is computed via lambda_to_Q0(). The example calls predict_growth() with constant environment and plots the prediction. ```R primary_model <- list(model = my_model, logN0 = 1, logNmax = 7, mu = .2, lambda = 20) Q0 <- lambda_to_Q0(lambda = 20, mu = .2) equivalent_pars <- list(model = my_model, N0 = 10^1, Nmax = 10^7, mu_opt = .2, Q0 = Q0) equivalent_prediction <- predict_growth(environment = "constant", my_times, equivalent_pars) plot(equivalent_prediction) ``` -------------------------------- ### Load example dataset and call get_TTDs Source: https://github.com/albgarre/biogrowth/blob/master/man/get_TTDs.Rd Loads the example_od dataset and demonstrates two calls to get_TTDs: one with default behavior (codified = FALSE) returning two columns, and one with codified = TRUE that also extracts the number of dilutions. ```R data("example_od") # example dataset included int he package get_TTDs(example_od, target_OD = 0.2) # default behaviour, returns two columns get_TTDs(example_od, target_OD = 0.2, codified = TRUE) # extracts also the number of dilutions ``` -------------------------------- ### Set starting values for remaining parameters Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Provides initial values for the Baranyi model parameters that will be fitted: logNmax, lambda, and logN0. ```r start = c(logNmax = 8, lambda = 25, logN0 = 2) ``` -------------------------------- ### Define simulation parameters for predict_isothermal_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/predict_isothermal_growth.Rd Sets up the model name, parameters, and time vector for the example. Uses the modGompertz model with parameters logN0, C, mu, and lambda, and a time sequence from 0 to 100 with 1000 points. ```R my_model <- "modGompertz" my_pars <- list(logN0 = 2, C = 6, mu = .2, lambda = 25) my_time <- seq(0, 100, length = 1000) ``` -------------------------------- ### Get detailed summary with summary() Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Retrieves a more detailed output of the fitted model using summary() on the GlobalGrowthFit object. ```r summary(global_fit) ``` -------------------------------- ### Fit secondary growth models with different parameters Source: https://github.com/albgarre/biogrowth/blob/master/man/compare_secondary_fits.Rd Fits two secondary growth models using fit_secondary_growth() with different known parameters (temperature_n = 1 vs 2). The example uses the example_cardinal dataset and defines sec_model_names, known_pars, and my_start. The first fit uses temperature_n = 1, the second uses temperature_n = 2. ```R data("example_cardinal") sec_model_names <- c(temperature = "Zwietering", pH = "CPM") known_pars <- list(mu_opt = 1.2, temperature_n = 1, pH_n = 2, pH_xmax = 6.8, pH_xmin = 5.2) my_start <- list(temperature_xmin = 5, temperature_xopt = 35, pH_xopt = 6.5) fit1 <- fit_secondary_growth(example_cardinal, my_start, known_pars, sec_model_names) known_pars <- list(mu_opt = 1.2, temperature_n = 2, pH_n = 2, pH_xmax = 6.8, pH_xmin = 5.2) fit2 <- fit_secondary_growth(example_cardinal, my_start, known_pars, sec_model_names) ``` -------------------------------- ### Example 3 - Global fitting Source: https://github.com/albgarre/biogrowth/blob/master/man/time_to_size.Rd Performs a global fit using fit_growth with secondary models and then calculates the time to reach a given size for each experiment. Returns NA for experiments where the size is not reached. ```R ## Example 3 - Global fitting ----------------------------------------------- ## We need a model first data("multiple_counts") data("multiple_conditions") sec_models <- list(temperature = "CPM", pH = "CPM") known_pars <- list(Nmax = 1e8, N0 = 1e0, Q0 = 1e-3, temperature_n = 2, temperature_xmin = 20, temperature_xmax = 35, temperature_xopt = 30, pH_n = 2, pH_xmin = 5.5, pH_xmax = 7.5, pH_xopt = 6.5) my_start <- list(mu_opt = .8) global_fit <- fit_growth(multiple_counts, sec_models, my_start, known_pars, environment = "dynamic", algorithm = "regression", approach = "global", env_conditions = multiple_conditions ) plot(global_fit) ## The function calculates the time for each experiment time_to_size(global_fit, 3) ## It returns NA for the particular experiment if the size is not reached time_to_size(global_fit, 4.5) ``` -------------------------------- ### Example 2 - Model fit Source: https://github.com/albgarre/biogrowth/blob/master/man/time_to_size.Rd Fits a growth model to data using fit_growth and then calculates the time to reach a given size. The model is defined with primary = "Baranyi" and known parameters. ```R ## Example 2 - Model fit ---------------------------------------------------- my_data <- data.frame(time = c(0, 25, 50, 75, 100), logN = c(2, 2.5, 7, 8, 8)) models <- list(primary = "Baranyi") known <- c(mu = .2) start <- c(logNmax = 8, lambda = 25, logN0 = 2) primary_fit <- fit_growth(my_data, models, start, known, environment = "constant", ) plot(primary_fit) time_to_size(primary_fit, 4) ``` -------------------------------- ### Refit secondary growth model with fixed parameters Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Calls fit_secondary_growth() again using the defined starting values and known parameters. ```R fit_cardinal <- fit_secondary_growth(example_cardinal, my_start, known_pars, sec_model_names) ``` -------------------------------- ### Pass initial guesses directly to fit_secondary_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_secondary.Rd Uses the initial guesses from make_guess_secondary() as the start parameter for fit_secondary_growth(), with an empty vector for other parameters and the same secondary model names. ```r fit_secondary_growth(example_cardinal, make_guess_secondary(example_cardinal, sec_model_names), c(), sec_model_names) ``` -------------------------------- ### Estimate TTDs and prepare data for fit_serial_dilution Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_serial_dilution.Rd Loads the example data, estimates TTDs using get_TTDs, and filters to a specific condition. Requires the tidyverse package. ```r data("example_od") ## We first need to estimate the TTDs library(tidyverse) my_TTDs <- get_TTDs(example_od, target_OD = 0.2, codified = TRUE) my_data <- filter(my_TTDs, condition == "S/6,5/35/R1") ``` -------------------------------- ### Fit and summarize the model Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_isothermal_growth.Rd Calls fit_isothermal_growth() with the data, model, starting values, and known parameters. Then displays the summary and plots the fitted model against observations. ```R static_fit <- fit_isothermal_growth(my_data, my_model, start, known) summary(static_fit) ``` ```R plot(static_fit) ``` -------------------------------- ### fit_dynamic_growth(fit_data, env_conditions, starting_point, known_pars, sec_model_names, ...) Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_dynamic_growth.Rd Fits a dynamic growth model to observed data using the one-step approach. It takes observed population size data, dynamic environmental conditions, starting values for fitted parameters, known parameters, and secondary model definitions, returning a FitDynamicGrowth object. ```APIDOC ## fit_dynamic_growth(fit_data, env_conditions, starting_point, known_pars, sec_model_names, ...) ### Description Fits a dynamic growth model to observed data using the one-step approach (non-linear regression). This function has been superseded by fit_growth() but remains available for fitting growth models to data obtained under dynamic environmental conditions. ### Method Function call ### Endpoint fit_dynamic_growth(fit_data, env_conditions, starting_point, known_pars, sec_model_names, ...) ### Parameters #### fit_data - **Type**: Tibble - **Required**: Yes - **Description**: Tibble with the data to use for model fit. It must contain a column with the elapsed time (named "time" by default) and another one with the decimal logarithm of the observed population size (named "logN" by default). Different column names can be specified using the "formula" argument. #### env_conditions - **Type**: Tibble - **Required**: Yes - **Description**: Tibble with the (dynamic) environmental conditions during the experiment. It must have one column with the elapsed time (named "time" by default) and as many columns as required with the environmental conditions. A different column name can be specified using the "formula" argument, although it must be the same one as in "fit_data". Note that only those defined in "sec_model_names" will be considered for the model fit. #### starting_point - **Type**: Named vector - **Required**: Yes - **Description**: A named vector of starting values for the model parameters. Parameters for the primary model must be named in the usual way. Parameters for the secondary model are named as env_factor+'_'+parameter. For instance, the maximum growth temperature shall be named 'temperature_xmax'. #### known_pars - **Type**: Named vector - **Required**: Yes - **Description**: A named vector of known model parameters (i.e. not fitted). They must be named using the same convention as for starting_point. #### sec_model_names - **Type**: Named character vector - **Required**: Yes - **Description**: A named character vector defining the secondary model for each environmental factor. The names define the factor and the value the type of model. Names must match columns in fit_data and env_conditions. #### ... - **Type**: Additional arguments - **Required**: No - **Description**: Additional arguments passed to modFit. #### check - **Type**: Logical - **Required**: No - **Description**: Whether to check model parameters (TRUE by default). #### logbase_mu - **Type**: Numeric - **Required**: No - **Description**: Base of the logarithm the growth rate is referred to. By default, the same as logbase_logN. See vignette about units for details. #### logbase_logN - **Type**: Numeric - **Required**: No - **Description**: Base of the logarithm for the population size. By default, 10 (i.e. log10). See vignette about units for details. #### formula - **Type**: Formula - **Required**: No - **Description**: An object of class "formula" describing the x and y variables. logN ~ time as a default. ### Request Example ```r # Example usage from the documentation sec_model_names <- c(temperature = "CPM", aw= "CPM") known_pars <- list(Nmax = 1e4, N0 = 1e0, Q0 = 1e-3, mu_opt = 4, temperature_n = 1, aw_xmax = 1, aw_xmin = .9, aw_n = 1) my_start <- list(temperature_xmin = 25, temperature_xopt = 35, temperature_xmax = 40, aw_xopt = .95) my_dyna_fit <- fit_dynamic_growth(example_dynamic_growth, example_env_conditions, my_start, known_pars, sec_model_names) ``` ### Response #### Success Response - **Type**: FitDynamicGrowth object - **Description**: An instance of FitDynamicGrowth(). #### Response Example ```r # The returned object can be used with summary() and plot() summary(my_dyna_fit) plot(my_dyna_fit) plot(my_dyna_fit, add_factor = "aw", label_y1 = "Log count (log CFU/ml)", label_y2 = "Water activity") ``` ``` -------------------------------- ### Fit secondary growth model with fit_secondary_growth() Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Calls fit_secondary_growth() with example data, initial guesses, known parameters, and model names. Uses default transformation (square root) and formula (mu). ```R fit_cardinal <- fit_secondary_growth(example_cardinal, sec_guess, known, sec_model_names) ``` -------------------------------- ### Fit coupled growth model with fit_coupled_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_coupled_growth.Rd Calls the fit_coupled_growth function with the example dataset, start guesses, known parameters, and mode set to 'one_step'. ```R my_fit <- fit_coupled_growth(example_coupled_onestep, start = guess, known = known, mode = "one_step") ``` -------------------------------- ### Load and inspect example OD data Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v07_serial_dilution.Rmd Loads the example_od dataset and displays its first rows to illustrate the required data format for get_TTDs(). The data must be a tibble or data.frame with a 'time' column and additional columns for each well's OD readings. ```r data("example_od") head(example_od) ``` -------------------------------- ### Load and inspect example_env_conditions dataset Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v22_datasets.Rmd Loads the example_env_conditions dataset and displays its first rows. It describes the experimental design for a dynamic growth experiment, with columns time, temperature, and aw. ```r data("example_env_conditions") head(example_env_conditions) ``` -------------------------------- ### fit_growth(fit_data, model_keys, start, known, environment, algorithm, approach, env_conditions, niter, ..., check, logbase_mu, logbase_logN, formula) Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_growth.Rd Fits microbial growth models to observed population size data over time, under constant or dynamic environmental conditions. Supports both regression and MCMC fitting algorithms, and single or global fitting approaches. ```APIDOC ## fit_growth ### Description This function provides a top-level interface for fitting growth models to data describing the variation of the population size through time, either under constant or dynamic environment conditions. ### Usage fit_growth( fit_data, model_keys, start, known, environment = ``` -------------------------------- ### Load and inspect example_dynamic_growth dataset Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v22_datasets.Rmd Loads the example_dynamic_growth dataset and displays its first rows. It contains observed population sizes (logN) over time for the experiment described by example_env_conditions. ```r data("example_dynamic_growth") head(example_dynamic_growth) ``` -------------------------------- ### Load and inspect example_cardinal dataset Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Loads the example_cardinal dataset included in biogrowth and displays its first rows. The dataset contains growth rates (mu) and environmental factors (temperature, pH) for static experiments. ```r data("example_cardinal") head(example_cardinal) ``` -------------------------------- ### Define primary and secondary model parameters for pH and temperature Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v31_secondary_for_static.Rmd Sets up the primary model parameters (mu_opt, Nmax, N0, Q0) and secondary models for temperature and pH using the CPM model. This configuration is used with predict_growth() to simulate growth under constant temperature and varying pH. ```r my_primary <- list(mu_opt = mu_opt, Nmax = 1e8,N0 = 1e2, Q0 = q0) sec_temperature <- list(model = "CPM", xmin = 5, xopt = 35, xmax = 40, n = 2) sec_pH <- list(model = "CPM", xmin = 4, xopt = 7, xmax = 8, n = 2) my_secondary_2 <- list(temperature = sec_temperature, pH = sec_pH) ``` -------------------------------- ### print.GlobalGrowthFit(x, ...) Source: https://github.com/albgarre/biogrowth/blob/master/man/GlobalGrowthFit.Rd Prints an instance of GlobalGrowthFit. ```APIDOC ## print.GlobalGrowthFit(x, ...) ### Description Prints an instance of GlobalGrowthFit. ### Method print ### Parameters - **x** (GlobalGrowthFit) - Required - An instance of GlobalGrowthFit. - **...** - Optional - Ignored. ### Response No return value, prints the object. ``` -------------------------------- ### Get statistical indexes with summary() Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v04_model_comparison.Rmd Retrieves the statistical indexes of the model fits. ```r summary(salmonella_models) ``` -------------------------------- ### Calculate initial guesses with make_guess_secondary Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_secondary.Rd Loads the example_cardinal dataset, assigns secondary model equations to factors (temperature: Zwietering, pH: fullRatkowsky), and computes initial guesses using make_guess_secondary(). ```r data("example_cardinal") sec_model_names <- c(temperature = "Zwietering", pH = "fullRatkowsky") make_guess_secondary(example_cardinal, sec_model_names) ``` -------------------------------- ### Print MCMCgrowth object Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v03_growth_uncertainty.Rmd Prints the MCMCgrowth object to get an overview of the simulation settings. ```r print(uncertain_prediction) ``` -------------------------------- ### Get parameter estimates with coef() Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v04_model_comparison.Rmd Retrieves the table of parameter estimates for the fitted models. ```r coef(salmonella_models) ``` -------------------------------- ### Define secondary models and parameters for global fitting Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Sets up the secondary models (CPM for temperature and pH) and defines known parameters and initial guesses. The known parameters include Nmax, N0, Q0, and cardinal parameters for temperature and pH. The initial guess for mu_opt is 0.8. ```r 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, temperature_xopt = 30) ## The rest, need initial guesses my_start <- list(mu_opt = .8) ``` -------------------------------- ### Get parameter keys for modGompertz Source: https://github.com/albgarre/biogrowth/blob/master/man/predict_growth.Rd Retrieves the parameter keys for the specified primary model (modGompertz). ```R primary_model_data(my_model)$pars ``` -------------------------------- ### Check initial guess with check_growth_guess() Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Passes the initial guess and fixed parameters to check_growth_guess() for a dynamic environment, using example_dynamic_growth data and example_env_conditions. The resulting plot shows the initial guess is reasonably close to the experimental data. ```R check_growth_guess( example_dynamic_growth, sec_model_names, c(my_start, known_pars), environment = "dynamic", env_conditions = example_env_conditions ) ``` -------------------------------- ### Define fixed parameters and initial guesses for fit_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_growth.Rd Define fixed model parameters (known_pars) and initial guesses (my_start) for fitting a growth model. The fixed parameters include Nmax, N0, Q0, and temperature/pH cardinal parameters; the initial guesses are for mu_opt and temperature_xopt. ```r 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) ``` ```r my_start <- list(mu_opt = .8, temperature_xopt = 30) ``` -------------------------------- ### Get detailed statistics with summary() Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v06_coupledmodels.Rmd Calls summary() on the fitted model to obtain detailed statistics of the fit. ```r summary(fit_twosteps) ``` -------------------------------- ### Get summary of fit Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v07_serial_dilution.Rmd Uses the summary() S3 method to obtain a statistical summary of the fitted model. ```r summary(my_fit) ``` -------------------------------- ### Get valid primary model keys Source: https://github.com/albgarre/biogrowth/blob/master/man/predict_growth.Rd Retrieves the valid model keys for the primary growth models. ```R primary_model_data() ``` -------------------------------- ### Define primary and secondary models for growth prediction Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v31_secondary_for_static.Rmd Sets up the primary model parameters (mu_opt, Nmax, N0, Q0) and a secondary model for temperature using the CPM model with cardinal parameters. These are used as inputs to predict_growth(). ```r q0 <- 1e-4 mu_opt <- .5 my_primary <- list(mu_opt = mu_opt, Nmax = 1e8,N0 = 1e2, Q0 = q0) sec_temperature <- list(model = "CPM", xmin = 5, xopt = 35, xmax = 40, n = 2) my_secondary <- list(temperature = sec_temperature) ``` -------------------------------- ### Print simulation overview Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v03_growth_uncertainty.Rmd Uses the print S3 method on the GrowthUncertainty object to show an overview of the simulation settings. ```r print(unc_growth) ``` -------------------------------- ### Print GrowthFit object Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Uses the print() method on the GrowthFit object to get a quick view of the fitted model. ```r print(primary_fit) ``` -------------------------------- ### Get Baranyi model parameters Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Retrieves the parameter keys for the Baranyi model, which are needed for defining initial guesses. ```r primary_model_data("Baranyi")$pars ``` -------------------------------- ### Load example_coupled_twosteps dataset Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v06_coupledmodels.Rmd Loads the example_coupled_twosteps dataset included in the biogrowth package, which is a tibble with columns temp, mu, and lambda for two-steps fitting. ```r data("example_coupled_twosteps") example_coupled_twosteps ``` -------------------------------- ### Load and inspect growth_salmonella data Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v04_model_comparison.Rmd Loads the example dataset of Salmonella growth under constant conditions and displays its first rows. ```r data("growth_salmonella") head(growth_salmonella) ``` -------------------------------- ### Fit dynamic growth model and use S3 methods Source: https://github.com/albgarre/biogrowth/blob/master/man/fit_growth.Rd Fits a dynamic growth model using fit_growth, then demonstrates S3 methods: plot with temperature factor, summary, and time_to_size to calculate time to reach a given size. The code is wrapped in \donttest{} to avoid execution during package checks. ```R dynamic_fit <- fit_growth(example_dynamic_growth, sec_models, my_start, known_pars, environment = "dynamic", env_conditions = example_env_conditions ) ``` ```R plot(dynamic_fit, add_factor = "temperature") summary(dynamic_fit) ``` ```R time_to_size(dynamic_fit, 3) ``` -------------------------------- ### Summary of FitSecondaryGrowth object Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v02_growth_fitting.Rmd Returns statistical information of the fit. In this example, shows NA standard errors due to poor identifiability. ```R summary(fit_cardinal) ``` -------------------------------- ### Use make_guess_primary with fit_growth Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_primary.Rd Shows how to pass the initial guesses to fit_growth() along with the data and model specification. ```R fit_growth(my_data, list(primary = "Logistic"), make_guess_primary(my_data, "Logistic"), c() ) ``` -------------------------------- ### Generate initial guesses for Baranyi model Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_primary.Rd Calls make_guess_primary with the example data and the 'Baranyi' model to obtain initial parameter guesses. ```R make_guess_primary(my_data, "Baranyi") ``` -------------------------------- ### Generate initial guesses for Logistic model Source: https://github.com/albgarre/biogrowth/blob/master/man/make_guess_primary.Rd Calls make_guess_primary with the example data and the 'Logistic' model to obtain initial parameter guesses. ```R make_guess_primary(my_data, "Logistic") ``` -------------------------------- ### Fit isothermal growth model Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Calls `fit_isothermal_growth()` with the data, model, starting values, known parameters, and formula. Returns a `FitIsoGrowth` object. ```r static_fit <- fit_isothermal_growth(my_data, my_model, start, known, formula = my_formula ) ``` -------------------------------- ### Define time points for prediction Source: https://github.com/albgarre/biogrowth/blob/master/vignettes/v99_deprecated.Rmd Creates a vector of 5 time points uniformly distributed between 0 and 15 for the initial prediction example. ```r my_times <- seq(0, 15, length = 5) ```