Paquetes necesarios

# Load required packages
library(growthcurver)
library(dplyr)
library(tidyr)
library(gt)

Cargar los datos

Los datos deben estar en un archivo CSV con las columnas Record_ID, Time y Logcfu. La dirección del archivo es "DATA/kamila-clostridium.csv". en mi caso, pero puede cambiarse según la ubicación del archivo en su sistema.

# Read the data
data <- read.csv("DATA/kamila-clostridium.csv")

# View the structure of the data
str(data)
## 'data.frame':    56 obs. of  3 variables:
##  $ Record_ID: chr  "M204_Cp" "M204_Cp" "M204_Cp" "M204_Cp" ...
##  $ Time     : num  0 0.25 0.5 0.75 1 1.3 1.7 2 2.3 2.5 ...
##  $ Logcfu   : num  4 4.05 4.1 4.3 4.5 4.8 5.3 5.6 5.9 6.5 ...

Ajuste de Curvas de Crecimiento y Extracción de Parámetros

# Get unique Record_IDs
record_ids <- unique(data$Record_ID)

# Initialize an empty list to store results
results_list <- list()

# Loop through each Record_ID and fit growth curve
for (id in record_ids) {
  # Subset data for current Record_ID
  subset_data <- data %>% filter(Record_ID == id)
  
  # Fit the growth curve using SummarizeGrowth
  gc_fit <- SummarizeGrowth(subset_data$Time, subset_data$Logcfu)
  # Extract parameters
  k <- gc_fit$vals$k
  r <- gc_fit$vals$r
  n0 <- gc_fit$vals$n0
  t_mid <- gc_fit$vals$t_mid
  # Calculate lag time using the formula
  lag_time1 <- t_mid - (log((k - n0)/n0)) / r
  # Alternative calculation: 
  lag_time2 <- t_mid - (2/r)
  
  # Extract parameters and store in list
  results_list[[id]] <- data.frame(
    Record_ID = id,
    k = k,                                # carrying capacity
    r = r,                                # growth rate
    n0 = n0,                              # initial population size
    t_mid = t_mid,                        # time at inflection point
    sigma = gc_fit$vals$sigma,            # residual standard error
    auc_l = gc_fit$vals$auc_l,            # area under the curve (logistic)
    auc_e = gc_fit$vals$auc_e,            # area under the curve (empirical)
    t_gen = gc_fit$vals$t_gen,            # generation time
    lag_time1 = lag_time1,                  # lag time1 (calculated)
    lag_time2 = lag_time2,                  # lag time2 (calculated)
    doubling_time = log(2)/r              # doubling time (calculated)
  )
}

Resultados

# Combine all results into a single dataframe
growth_parameters <- bind_rows(results_list)

# Round the results for better readability
growth_parameters_rounded <- growth_parameters %>%
  mutate(across(where(is.numeric), ~round(., 4)))

# gt table
growth_parameters_rounded %>%
  gt() %>%
  tab_header(
    title = "Parámetros de Curvas de Crecimiento",
    subtitle = "Calculados usando el paquete growthcurver"
  ) %>%
  fmt_number(
    columns = where(is.numeric),
    decimals = 4
  ) %>%
  cols_label(
    Record_ID = "ID de Registro",
    k = "Capacidad de Carga (k)",
    r = "Tasa de Crecimiento (r)",
    n0 = "Población Inicial (n0)",
    t_mid = "Tiempo en Punto de Inflexión (t_mid)",
    sigma = "Error Estándar Residual (σ)",
    auc_l = "Área Bajo la Curva (Logístico)",
    auc_e = "Área Bajo la Curva (Empírico)",
    t_gen = "Tiempo de Generación",
    lag_time1 = "Tiempo de Latencia 1",
    lag_time2 = "Tiempo de Latencia 2",
    doubling_time = "Tiempo de Duplicación"
  )
Parámetros de Curvas de Crecimiento
Calculados usando el paquete growthcurver
ID de Registro Capacidad de Carga (k) Tasa de Crecimiento (r) Población Inicial (n0) Tiempo en Punto de Inflexión (t_mid) Error Estándar Residual (σ) Área Bajo la Curva (Logístico) Área Bajo la Curva (Empírico) Tiempo de Generación Tiempo de Latencia 1 Tiempo de Latencia 2 Tiempo de Duplicación
M204_Cp 4.0713 1.4571 0.1336 2.3220 0.1365 8.8888 8.8388 0.4757 0.0000 0.9494 0.4757
M205_Cp 4.7646 1.5369 0.1129 2.4195 0.1274 7.7179 7.6375 0.4510 0.0000 1.1182 0.4510
M206_Cp 4.1518 1.8511 0.1469 1.7856 0.1054 11.2035 11.1375 0.3744 0.0000 0.7052 0.3744
# Save results to CSV
write.csv(growth_parameters_rounded, "growth_curve_parameters.csv", row.names = FALSE)

Visualización de las Curvas de Crecimiento Ajustadas

for (id in record_ids) {
  subset_data <- data %>% filter(Record_ID == id)
  gc_fit <- SummarizeGrowth(subset_data$Time, subset_data$Logcfu)
 # extraer parámetros 
  k <- gc_fit$vals$k
  r <- gc_fit$vals$r
  n0 <- gc_fit$vals$n0
  t_mid <- gc_fit$vals$t_mid
  lag_time2 <- gc_fit$vals$lag_time2
  # Plot
  plot(gc_fit, main = "", xlab = "Tiempo", ylab = "Log(CFU)") 
  legend("topleft", 
         legend = paste("Identificación:", id),
         bty = "n",       # no box around text
         cex = 0.8,       # text size
         text.font = 2,   # bold
         text.col = "black")
  abline(v = t_mid, lty = 2, col = "blue", lwd = 2)
  abline(h = k, lty = 2, col = "red", lwd = 2)
}