2017-10-17 107 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