R中具有多个解释变量的细菌生长曲线(logistic/sigmoid)
bacterial growth curve (logistic/sigmoid) with multiple explanatory variables in R
目标:我想获得多次处理的生长曲线的回归(ggplot 曲线和模型参数)。
我有在营养源 N={x,y} 上生长的细菌培养物 C={a,b,c,d} 的数据。
他们理想化的生长曲线(每小时测量细胞培养物的浊度)看起来像这样:
有 8 种不同的曲线可以获取系数和曲线。我怎样才能一次性完成我的数据框,将不同的处理作为非线性回归的不同组提供?
谢谢!!!
此问题类似于 here 上发布的一个未回答的问题。
(理想化数据的源代码,抱歉,我不是计算机科学家,所以不够优雅):
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")
如果您熟悉 dplyr
中的 group_by
函数(包含在 tidyverse
中),那么您可以按文化和营养对数据进行分组,并使用 broom
。我认为 vignette 正是您想要完成的目标。这是一次性的代码:
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))
如果一口气没问题,请拆开 %>%
以便更好地理解。我在这里使用了过拟合的 GAM,但您可以将其替换为您想要的任何模型,包括 sigmoid。
目标:我想获得多次处理的生长曲线的回归(ggplot 曲线和模型参数)。
我有在营养源 N={x,y} 上生长的细菌培养物 C={a,b,c,d} 的数据。
他们理想化的生长曲线(每小时测量细胞培养物的浊度)看起来像这样:
有 8 种不同的曲线可以获取系数和曲线。我怎样才能一次性完成我的数据框,将不同的处理作为非线性回归的不同组提供?
谢谢!!!
此问题类似于 here 上发布的一个未回答的问题。
(理想化数据的源代码,抱歉,我不是计算机科学家,所以不够优雅):
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")
如果您熟悉 dplyr
中的 group_by
函数(包含在 tidyverse
中),那么您可以按文化和营养对数据进行分组,并使用 broom
。我认为 vignette 正是您想要完成的目标。这是一次性的代码:
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))
如果一口气没问题,请拆开 %>%
以便更好地理解。我在这里使用了过拟合的 GAM,但您可以将其替换为您想要的任何模型,包括 sigmoid。