Fitting with ggplot2, geom_smooth and nls

后端 未结 1 1679
日久生厌
日久生厌 2020-12-01 13:02

I am trying to fit data on an exponential decay function (RC like system) with equation:

相关标签:
1条回答
  • 2020-12-01 13:18

    There are several problems:

    1. formula is a parameter of nls and you need to pass a formula object to it and not a character.
    2. ggplot2 passes y and x to nls and not fold and t.
    3. By default, stat_smooth tries to get the confidence interval. That isn't implemented in predict.nls.

    In summary:

    d <- ggplot(test,aes(x=t, y=fold))+ 
             #to make it obvious I use argument names instead of positional matching
      geom_point()+
      geom_smooth(method="nls", 
                  formula=y~1+Vmax*(1-exp(-x/tau)), # this is an nls argument, 
                                                    #but stat_smooth passes the parameter along
                  start=c(tau=0.2,Vmax=2), # this too
                  se=FALSE) # this is an argument to stat_smooth and 
                            # switches off drawing confidence intervals
    

    Edit:

    After the major ggplot2 update to version 2, you need:

    geom_smooth(method="nls", 
                  formula=y~1+Vmax*(1-exp(-x/tau)), # this is an nls argument
                  method.args = list(start=c(tau=0.2,Vmax=2)), # this too
                  se=FALSE)
    
    0 讨论(0)
提交回复
热议问题