2

I'm attempting to fit a basic power trendline on a set of 3 data point, as you could do in Excel to mimic the y = Ax^b function. I have a very simple data set loaded into LCurve.data as follows:

MDPT = {4, 10.9, 51.6}

AUC = {287069.4, 272986.0, 172426.1}

fm0 <- nls(log(LCurve.data$AUC) ~ log(a) + b * log(LCurve.data$MDPT), data = LCurve.data, start = list (a = 1, b =1))
  ggplot(LCurve.data, aes(x=MDPT, y = AUC)) + geom_line() +
    geom_point() +
    stat_smooth(method = 'nls', formula = y ~ a * x ^ b, method.args = list(
      start=c(a = coef(fm0)[1], b = coef (fm0)[2]))) 

For some reason this code produces the following error.

Warning: Computation failed in stat_smooth(): singular gradient matrix at initial parameter estimates

The fm0 array has the correct values in the "a" and "b" fields, or coef(fm0)[1] and [2] respectively. I confirmed that with a print of the variable.

I cannot figure out why I am unable to graph the NLS trendline in stat_smooth though. Any ideas? I've been googling for almost 2 hours now and keep running into dead ends. Thanks!

Jonebone
  • 21
  • 1
  • I'm not getting the same messages as you, but I think you need `se = FALSE` for nls models in `stat_smooth()`. And I think the starting values should be a list instead of a vector: `start=list(a = coef(fm0)[1], b = coef (fm0)[2]))`. – aosmith Aug 15 '18 at 22:31
  • [This answer](https://stackoverflow.com/a/25031125/2461552) shows the things I mentioned. – aosmith Aug 15 '18 at 22:32
  • I added the se= FALSE flag and it doesn't make a difference. The method.args code is coded properly, if you do start=list then it breaks. I had referenced that answer previously to build my existing code. Other ideas? – Jonebone Aug 16 '18 at 11:55
  • If I make those changes the code runs for me (although I thought the estimated line was pretty odd; it is only three points). What version of ggplot2 are you using? – aosmith Aug 16 '18 at 14:36
  • I'm using the newest version where you need to use method.args instead of start = ... And yes, I'm only using 3 points to try and get the function working before I extrapolate to larger data sets. – Jonebone Aug 17 '18 at 10:31
  • I'm using R 3.5.1 and ggplot2 3.0.0, and the changes I mentioned allow `stat_smooth()` to work for me. Hope you figure it out! – aosmith Aug 17 '18 at 13:29

0 Answers0