0

I'd love some help with this. I'm trying to put an exponential decay curve onto some vehicle data I have. I've been searching through Stack Overflow and none of the answers have been helpful.

This is my current code that's not working. It's based off the ggplot2 documentation and it's still not working.

plot <- ggplot(data = rawData, aes(x = Mileage, y = Cost, color = Car)) + geom_point() + stat_smooth(method = 'nls', formula = y ~ a*exp(b *-x), se = FALSE, start = list(a=1,b=1))

plot

It plots my data but doesn't show a curve.

I can't embed photos for some reason so here it is

The current warning messages I receive are:

1: In (function (formula, data = parent.frame(), start, control = nls.control(), : No starting values specified for some parameters. Initializing ‘a’, ‘b’ to '1.'. Consider specifying 'start' or using a selfStart model 2: Computation failed in stat_smooth(): singular gradient matrix at initial parameter estimates

I tried these other options too, to no avail.

ggplot(mtcars, aes(x = Mileage, y = Cost)) + geom_point() +
stat_smooth(method = "nls", formula = y ~ a * exp(x * b), se = FALSE, method.args = list(start = list(a = 1, b = 1)))

Which resulted in an error message of:

Computation failed in stat_smooth(): Missing value or an infinity produced when evaluating the model

And I tried this too

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
stat_smooth(method = "nls", formula = y ~ a * exp(x * -b), se = FALSE, method.args = list(start = list(a = 1, b = 1), lower = c(0), algorithm = "port"))

Which resulted in an error message of:

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

UPDATE If I divide all my values by 100,000, all of sudden the trendline works, albeit without confidence intervals. I have no idea why this works and doesn't provide me with an acceptable answer since all my axis values are now off by 100,000.

rawData %>% mutate(Mileage = Mileage / 100000, Cost = Cost / 100000) %>% ggplot(aes(x = Mileage, y = Cost, color = Car)) + geom_point() + stat_smooth(method = "nls", formula = y ~ a * exp(x * -b), se = FALSE)

Here is my data - https://docs.google.com/spreadsheets/d/1SKhkqHK-qFGG8IST67iUhMIIdvA_k6htVid7lAwCb3A/edit?usp=sharing

Jon A
  • 1
  • 1
  • 1
    Please do not post data at an external site. Either post data here in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or use a built in data set for testing. What does "it's still not working" mean exactly? Are you getting an error? What's the error message? If you are getting a plot you weren't expecting, what should the plot look like? – MrFlick Oct 28 '19 at 16:18
  • Ok, duly noted. My dataset is 474 lines long so I don't think anyone would want it posted here. I'll share pictures instead. "It's still not working" means that an exponential decay trendline does not show up on the graph. I get error messages that don't seem to help. I will edit my post to share. – Jon A Oct 28 '19 at 17:15
  • There, I edited my original post to share more information. – Jon A Oct 28 '19 at 17:21
  • "singular gradient matrix at initial parameter estimates" makes me think you should try getting "better" starting values. Your starting values give a y-intercept of `a = 1`, but looking at your data a good y-intercept might be more like `a = 30000`. Similarly, if I eyeball the line should pass some where near x = 100000, y = 10000, with that y-intercept of 30000, the decay factor should be something like `b = 1e-5`. – Gregor Thomas Oct 28 '19 at 17:28
  • Thanks, I got some info from someone else that for some odd reason, if I divide my values by 100,000 into smaller numbers, it might work. Turns out it did, which makes no sense. (code in my original post above). That resulted in a trendline, but no confidence intervals unfortunately. – Jon A Oct 28 '19 at 17:32
  • That's the same difference - rescaling your data to get better in line with starting parameters is equivalent to rescaling your staring parameters to get them more in line with your data. If you want confidence intervals, `log` your y data and use `method = "lm"`. – Gregor Thomas Oct 28 '19 at 17:38
  • Thanks, I appreciate the help. It turned out I could rescale it, and then it works, or I could just take the log of the y-values and run a linear regression and then it works too. Strange behavior – Jon A Oct 28 '19 at 17:44

0 Answers0