Running Growth Rate In loop

I am using fit_easylinear from growthrate package to analyase my 96 well plate data

library(growthrates)
library(tidyverse)

set.seed(11)

##my data frame looks like this
df <- data.frame(time = seq(0, 865),
A1 = runif(866, 0, 1),
A2 = runif(866, 0, 1),
A3 = runif(866, 0, 1),
A4 = runif(866, 0, 1),
A5 = runif(866, 0, 1),
A6 = runif(866, 0, 1),
A7 = runif(866, 0, 1),
A8 = runif(866, 0, 1),
A9 = runif(866, 0, 1),
A10 = runif(866, 0, 1),
A11 = runif(866, 0, 1),
A12 = runif(866, 0, 1),
B1 = runif(866, 0, 1),
B2 = runif(866, 0, 1),
B3 = runif(866, 0, 1),
B4 = runif(866, 0, 1),
B5 = runif(866, 0, 1),
B6 = runif(866, 0, 1),
B7 = runif(866, 0, 1),
B8 = runif(866, 0, 1),
B9 = runif(866, 0, 1),
B10 = runif(866, 0, 1),
B11 = runif(866, 0, 1),
B12 = runif(866, 0, 1),
C1 = runif(866, 0, 1),
C2 = runif(866, 0, 1),
C3 = runif(866, 0, 1),
C4 = runif(866, 0, 1),
C5 = runif(866, 0, 1),
C6 = runif(866, 0, 1),
C7 = runif(866, 0, 1),
C8 = runif(866, 0, 1),
C9 = runif(866, 0, 1),
C10 = runif(866, 0, 1),
C11 = runif(866, 0, 1),
C12 = runif(866, 0, 1))

fit the model

fit_list <- lapply(2:length(colnames(df)), function(x) fit_easylinear(df$time, df[]))

this code plots all the value but not the actual fit

for (i in seq_along(fit_list)) {jpeg(paste0("C:/Users/Rahul/Desktop/images/", colnames(df)[i+1], ".jpg")); plot(slot(fit_list[[i]], "obs")); dev.off() }

removing slot

for (i in seq_along(fit_list)) {jpeg(paste0("C:/Users/Rahul/Desktop/images", colnames(df)[i+1], ".jpg"));plot(fit_list[[i]]) ;dev.off()}

###this give error as:
Error in seq.default(min(obs[, "time"] + lag), max(obs[, "time"]), length = 200) :
'from' must be a finite number

The problem happens only in specific wells:

coef(fit_list[[2]])
        y0      y0_lm      mumax        lag 
0.07540848 0.00000000 1.52502575        Inf 

So with lag = Inf the plotting fails. This problems happens for i = 2,12,17,19, 20,29,32; in other words for wells A2, A12, B5, B7, B8, C5, C8. So this random patterns suggests to me that the fit must fail in these particular samples, and that is because of the unrealistic data simulation.

If you use the data that comes with the package, the fit works perfectly:

library(growthrates)
#> Loading required package: lattice
#> Loading required package: deSolve
data("bactgrowth")

split_df <- split(bactgrowth,~strain+replicate+conc)
timeseries <- lapply(split_df, function(x) x$value)

df <- cbind(data.frame(time = 1:31),
            as.data.frame(timeseries))

fit_list <- lapply(2:length(colnames(df)), function(x) fit_easylinear(df$time, df[[x]]))
#> Warning in summary.lm(m): essentially perfect fit: summary may be unreliable

#> Warning in summary.lm(m): essentially perfect fit: summary may be unreliable

#> Warning in summary.lm(m): essentially perfect fit: summary may be unreliable
[...]


# for reprex
dir.create("images/")

for (i in seq_along(fit_list)) {
  jpeg(paste0("images/", colnames(df)[i+1], ".jpg"))
  plot(slot(fit_list[[i]], "obs"))
  dev.off()
}


for (i in (seq_along(fit_list))) {
  jpeg(paste0("images/y", colnames(df)[i+1], ".jpg"))
  plot(fit_list[[i]], which = "fit")
  dev.off()
}

Created on 2022-09-25 by the reprex package (v2.0.1)

Thank you for your response. I tried using my data it took sometime but this time my code worked for all wells.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.