如何绘制适合 ggplot2 的 nls 模型的输出

Hap*_*Dog 2 r ggplot2

我有一些数据,我想使用 nls 将非线性模型拟合到数据的每个子集,然后使用 ggplot2 将拟合模型叠加到数据点图上。具体来说,模型的形式是

y~V*x/(K+x)
Run Code Online (Sandbox Code Playgroud)

您可以将其识别为 Michaelis-Menten。一种方法是使用 geom_smooth,但如果我使用 geom_smooth,我将无法检索模型拟合的系数。或者,我可以使用 nls 拟合数据,然后使用 geom_smooth 绘制拟合线,但是我怎么知道 geom_smooth 绘制的曲线与我的 nls 拟合给出的曲线相同?我不能将我的 nls 拟合系数传递给 geom_smooth 并告诉它使用它们,除非我可以让 geom_smooth 只使用数据的一个子集,然后我可以指定起始参数,这样就可以了,但是......每个有一次我试过,读到的错误如下:

Aesthetics must be either length 1 or the same as the data (8): x, y, colour
Run Code Online (Sandbox Code Playgroud)

以下是我一直在使用的一些示例数据:

Concentration <- c(500.0,250.0,100.0,62.5,50.0,25.0,12.5,5.0,
                   500.0,250.0,100.0,62.5,50.0,25.0,12.5,5.0)

drug <- c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2)

rate <- c(1.889220,1.426500,0.864720,0.662210,0.564340,0.343140,0.181120,0.077170,
          3.995055,3.011800,1.824505,1.397237,1.190078,0.723637,0.381865,0.162771)

file<-data.frame(Concentration,drug,rate)
Run Code Online (Sandbox Code Playgroud)

在我的图中,浓度为 x,速率为 y;药物将是颜色变量。如果我编写以下内容,则会出现该错误:

plot <- ggplot(file,aes(x=file[,1],y=file[,3],color=Compound))+geom_point()

plot<-plot+geom_smooth(data=subset(file,file[,2]==drugNames[i]),method.args=list(formula=y~Vmax*x/(Km+x),start=list(Vmax=coef(models[[i]])[1],Km=coef(models[[i]])[2])),se=FALSE,size=0.5)
Run Code Online (Sandbox Code Playgroud)

其中models[[]] 是nls 返回的模型参数列表。

关于如何在 geom_smooth 中对数据框进行子集化以便我可以使用 nls 拟合中的起始参数单独绘制曲线的任何想法?

nei*_*fws 5

理想的解决方案是绘制nls()using的结果ggplot,但这里有一个基于几个观察结果的“快速而肮脏”的解决方案。

首先,您可以确定如果对nls()和使用相同的公式geom_smooth(method = "nls"),您将获得相同的系数。那是因为后者在调用前者。

其次,使用您的示例数据nls(),无论起始值如何,都会收敛到相同的 Vmax 和 Km 值(每种药物不同)。换句话说,无需使用每种药物范围内的起始值来构建模型。以下任何一项对药物 1 给出相同的结果(对于药物 2 类似):

library(dplyr)
# use maximum as start
df1 %>% 
  filter(drug == 1) %>% 
  nls(rate ~ Vm * Concentration/(K + Concentration), 
      data = ., 
      start = list(K = max(.$Concentration), Vm = max(.$rate)))

# use minimum as start
df1 %>% 
  filter(drug == 1) %>% 
  nls(rate ~ Vm * Concentration/(K + Concentration), 
      data = ., 
      start = list(K = min(.$Concentration), Vm = min(.$rate)))

# use arbitrary values as start
df1 %>% 
  filter(drug == 1) %>% 
  nls(rate ~ Vm * Concentration/(K + Concentration), 
      data = ., 
      start = list(K = 50, Vm = 2))
Run Code Online (Sandbox Code Playgroud)

因此绘制曲线的最快方法就是将药物映射到ggplot美学上,例如颜色。这将从nls相同的起始值构建单独的曲线,然后您可以返回(nls()如果需要)以获取系数,知道模型应该与绘图相同。

使用您的示例数据file(但不要调用它file,我使用了df1):

library(ggplot2)
df1 <- structure(list(Concentration = c(500, 250, 100, 62.5, 50, 25, 12.5, 5, 
                                        500, 250, 100, 62.5, 50, 25, 12.5, 5), 
                      drug = c(1, 1, 1, 1, 1, 1, 1, 1, 
                               2, 2, 2, 2, 2, 2, 2, 2), 
                      rate = c(1.88922, 1.4265, 0.86472, 0.66221, 0.56434, 0.34314, 
                               0.18112, 0.07717, 3.995055, 3.0118, 1.824505, 1.397237, 
                               1.190078, 0.723637, 0.381865, 0.162771)),
                      .Names = c("Concentration", "drug", "rate"), 
                      row.names = c(NA, -16L), 
                      class = "data.frame")

# could use e.g. Km = min(df1$Concentration) for start
# but here we use arbitrary values
ggplot(df1, aes(Concentration, rate)) + 
  geom_point() + 
  geom_smooth(method = "nls", 
              method.args = list(formula = y ~ Vmax * x / (Km + x),
                                 start = list(Km = 50, Vmax = 2)), 
              data = df1,
              se = FALSE,
              aes(color = factor(drug)))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明