Fitting with ggplot2, geom_smooth and nls

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)

Leave a Comment