2017-10-17 111 views
0

我有一些數據,我想利用NLS使用,以適應非線性模型對數據的每個子集,然後疊加擬合模型到數據點的圖表GGPLOT2。具體地,模型的形式爲如何繪製從NLS模型擬合的輸出GGPLOT2

y~V*x/(K+x) 

,你可能識別爲米氏的。一種方法是使用geom_smooth,但如果我使用geom_smooth,則無法檢索模型擬合的係數。或者,我可以使用nls來擬合數據,然後使用geom_smooth擬合繪製的線條,但是我怎麼知道geom_smooth繪製的曲線與我的nls fit所給出的曲線相同?我無法通過從我的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) 

其中模型[[]]是由NLS返回模型的參數列表。

我如何能子集geom_smooth數據幀,所以我可以用開始從我的NLS參數適合單獨繪製曲線任何想法?

+1

[幾個參數GGPLOT2繪圖功能]的可能的複製(https://開頭stackoverflow.com/questions/42598375/ggplot2-plot-function-with-several-arguments) –

+1

無關,但'plot','file'作爲變量名是不是一個好主意(這些名稱存在功能)。 – neilfws

+1

另外:這將有助於看到生成'models'的代碼。 – neilfws

回答

1

理想的解決辦法繪製的使用ggplotnls()的結果,但這裏是基於一些觀察的「快速和骯髒」的解決方案。

首先,你可以肯定的是,如果你使用相同的公式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))) 

enter image description here

+0

謝謝!那很完美。對不起,我的代碼上面的錯誤 – HappyDog