簡體   English   中英

如何繪制適合 ggplot2 的 nls 模型的輸出

[英]How to plot the output from an nls model fit in ggplot2

我有一些數據,我想使用 nls 將非線性模型擬合到數據的每個子集,然后使用 ggplot2 將擬合模型疊加到數據點圖上。 具體來說,模型的形式是

y~V*x/(K+x)

您可以將其識別為 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

以下是我一直在使用的一些示例數據:

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)

在我的圖中,濃度為 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)

其中models[[]] 是nls 返回的模型參數列表。

關於如何在 geom_smooth 中對數據框進行子集化以便我可以使用 nls 擬合中的起始參數單獨繪制曲線的任何想法?

理想的解決方案是使用ggplot繪制nls()的結果,但這里有一個基於幾個觀察結果的“快速而骯臟”的解決方案。

首先,您可以確定,如果您對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))

因此,繪制曲線的最快方法就是將葯物映射到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)))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM