Dan Kehila
Dan Kehila

Reputation: 25

bacterial growth curve (logistic/sigmoid) with multiple explanatory variables in R

Goal: I want to obtain regression (ggplot curves and model parameters) for growth curves with multiple treatments.

I have data for bacterial cultures C={a,b,c,d} growing on nutrient sources N={x,y}.

Their idealized growth curves (measuring turbidity of cell culture every hour) look something like this: enter image description here

There are 8 different curves to obtain coefficients and curves for. How can I do it in one go for my data frame, feeding the different treatments as different groups for the nonlinear regression?

Thanks!!!

This question is similar to an unanswered question posted here.

(sourcecode for idealized data, sorry it's not elegant as I'm not a computer scientist):

a<-1:20
a[1]<-0.01
for(i in c(1:19)){
  a[i+1]<-1.3*a[i]*(1-a[i])
}
b<-1:20
b[1]<-0.01
for(i in c(1:19)){
  b[i+1]<-1.4*b[i]*(1-b[i])
}
c<-1:20
c[1]<-0.01
for(i in c(1:19)){
  c[i+1]<-1.5*c[i]*(1-c[i])
}
d<-1:20
d[1]<-0.01
for(i in c(1:19)){
  d[i+1]<-1.6*d[i]*(1-d[i])
}
sub.data<-cbind(a,b,c,d)
require(reshape2)
data<-melt(sub.data, value.name = "OD600")
data$nutrition<-rep(c("x", "y"), each=5, times=4)
colnames(data)[1:2]<-c("Time", "Culture")


ggplot(data, aes(x = Time, y = OD600, color = Culture, group=nutrition)) +
  theme_bw() + xlab("Time/hr") + ylab("OD600") +
  geom_point() +  facet_wrap(~nutrition, scales = "free")

Upvotes: 2

Views: 2588

Answers (1)

Jeff Parker
Jeff Parker

Reputation: 1979

If you are familiar group_by function from dplyr (included in tidyverse), then you can group your data by Culture and nutrition and create models for each group using broom. I think this vignette is getting at exactly what you are trying to accomplish. Here is the code all in one go:

library(tidyverse)
library(broom)
library(mgcv)  #For the gam model

data %>%
      group_by(Culture, nutrition) %>%
      do(fit = gam(OD600 ~ s(Time), data = ., family=gaussian())) %>% # Change this to whatever model you want (e.g., non-linear regession, sigmoid)
      #do(fit = lm(OD600 ~ Time, data = .,)) %>% # Example using linear regression
      augment(fit) %>% 
      ggplot(aes(x = Time, y = OD600, color = Culture)) + # No need to group by nutrition because that is broken out in the facet_wrap
      theme_bw() +  xlab("Time/hr") + ylab("OD600") +
      geom_point() + facet_wrap(~nutrition, scales = "free") +
      geom_line(aes(y = .fitted, group = Culture))

If you are ok without one go, break apart the %>% for better understanding. I used GAM which overfits here but you could replace this with whatever model you want, including sigmoid.

enter image description here

Upvotes: 3

Related Questions