I'm currently trying to fit an exponetial curve to a set of data that includes the equation label. I would also like to possibly include an R^2 value and p-value. I know that one way to do this is to use the stat_fit_tidy using a nls. Using the provided mtcars dataset
ggplot(mtcars, aes(x = disp, y = mpg, colour = factor(cyl))) + geom_point() + stat_smooth(method = "nls" , formula = y ~ A * exp( B * x) , method.args = list( start = c( A = 2, B = 0.2 )) stat_fit_tidy(method = "nls", method.args = list(formula = y~A*exp(B*x)), size = 3, label.x = "center", label.y = "bottom", parse = TRUE)
I'm struggling to get A and B to be correct values, I keep getting the error
Computation failed in
singular gradient "
I think this is because A or B isn't close enough to their actual values. However, even when I try this on other dataset and get A and B correct, I still can't figure out how to use stat_fit_tidy correctly. How would I do this correctly? Is there a easier way of doing this. It's sort of frustrating that this is something that's very simple to do in Excel but complicated in R