0

I would like to use non-linear regression within ggplot, such that I may plot data and fit a curve to the reaction rate r = (k1*PA*PB^0.5)/(1+k2*PA^0.5) where PA and PB are tabulated against reaction rate r. I would like to find constants k1 and k2. I have been able to use nls() to carry out regression, but I need to lay the curve on top of ggplot data.

1. r      PA      PB
2. 0.02   0.002   0.005
3. 0.05   0.004   0.009
4. 0.09   0.005   0.019

plot <- qplot(data = melt.data[which(melt.data$variable ==     c('PA','PB')),], 
              x = Experimental.rate,
              y = value, 
              color = variable)

plot <- plot + stat_smooth(method = nls, 
              formula = with(data, 'y ~ k1 * PA * PB^0.5 / (1 + k2 * PA^0.5 )'), 
              start = list(k1 = 0.1, k2 = 0.1))

Something of this sort...data is for example only I have ~30 rows in series. Let me know what else I can provide to make this an easier to understand example -

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
deposition
  • 535
  • 2
  • 4
  • 9
  • 1
    Are you asking how to perform the non-linear regression or how to include that layer in `ggplot` (your example shows neither of the two)? However, for the latter, you may want to have a look at http://docs.ggplot2.org/0.9.3.1/geom_smooth.html, should you already have the regression data. – gented Nov 19 '15 at 00:24
  • 2
    It seems to me there are a couple minor things that may be producing the error: first, you need to pass a string to `method`, so it should be `stat_smooth(method="nls",...)`. Second, the formula needs to be a formula, not a text. Should be something like `formula=y ~ k1 * PA * PB^0.5 / (1 + k2 * PA^0.5 )` (WITHOUT the quotes!). I couldn't test if it works, as you didn't provide data to make it reproducible, though. Keep in mind that you need to add the `se=FALSE` to the call to, as it's not implemented for nls. – PavoDive Nov 19 '15 at 00:26

0 Answers0