r - R 中具有多个解释变量的细菌生长曲线(逻辑/S 形)

标签 r ggplot2 nls non-linear-regression

目标:我想获得多次处理的生长曲线的回归(ggplot曲线和模型参数)。

我有在营养源 N={x,y} 上生长的细菌培养物 C={a,b,c,d} 的数据。

他们理想的生长曲线(每小时测量细胞培养物的浊度)如下所示: enter image description here

有 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。

enter image description here

关于r - R 中具有多个解释变量的细菌生长曲线(逻辑/S 形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728424/

相关文章:

r - ggplot2 中更简单的人口金字塔

r - ggplot : fit a curve (geom_smooth method ="nls") with CI95% bands

arcgis - 有什么方法可以在我们的小部件中使用 Web App Builder NLS(在构建器文件夹中)?

r - 当 se.fit=TRUE 时,Predict 无法显示预测的标准误差

r - log-pdf的核估计

r - 如何缩小图例框的内边距

r - ggplot2 和一组抖动/闪避点

r - 使用 dplyr 将函数应用于表的每一行?

r - ggplot facet_wrap 中的 Unicode(en_US.UTF-8 语言环境)

R + ggplot + pdf 设备 + LaTeX : is it possible to embed fonts one time