0

I am trying to do a exponential regression in ggplot2. So first my skript:

g <- ggplot(data, aes(x=datax, y=datay), color="black") +
 geom_point(shape=1) +  stat_smooth(method = 'nls', formula = y~a*exp(b*x), aes(colour = 'Exponential'), se = FALSE)
g <- g + theme_classic()
g <- g + theme(panel.grid.major=element_blank())
g <- g + theme(panel.grid.minor=element_blank())
g <- g + theme(axis.line.x=element_line(color="black"),
           axis.line.y=element_line(color="black"),
           panel.border=element_blank(),
           panel.background=element_blank())
g <- g + labs(x="\ndatax",y="datay\n")
g <- g + theme(axis.text.y=element_text(size=14))
g <- g + theme(axis.text.x=element_text(size=14))
g <- g + theme(axis.title.y=element_text(size=18,vjust=1))
g <- g + theme(axis.title.x=element_text(size=18,vjust=1))
g

This is the image that I got

enter image description here

As a R-beginner I did the script by mixing scripts of mine and the internet. I always get the following error:

"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"

I did not found a better way to do the exponential graph, yet.

In addition, I would like to change the color of the graph into black and delete the legend and I would like to have the R² and p value in the graph. (maybe as well the confidence intervals?)

user20650
  • 24,654
  • 5
  • 56
  • 91
Sofia
  • 75
  • 1
  • 7
  • I would advise to try and run the `nls` function, and starting values outside of ggplot first, so you can see what you are doing. not really related but r2 does not make sense for nonlinear regression (http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2892436/) – user20650 Jul 18 '16 at 18:34
  • Could you please provide some data to test? – Eugen Jul 18 '16 at 18:38
  • It doesnt look like an error, as you get a plot. All is says is that because you didnt specify starting values for your parameters (as you need to do in nls) that they have been initialised at one. Note that not specifying sensible starting values will often result in nls not converging so its worth lookng into this. – user20650 Jul 18 '16 at 18:54

1 Answers1

3

It's not easy to answer without a reproducible example and so many questions. Are you sure the message you reported is an error and not a warning instead? On my own laptop, with dataset 'iris', I got a warning...

However, how you can read on ?nls page on R documentation, you should provide through the parameter "start" an initial value for starting the estimates to help finding the convergence. If you don't provide it, nls() itself should use some dummy default values (in your case, a and b are set to 1).

You could try something like this:

g <- ggplot(data, aes(x=datax, y=datay), color="black") +
     geom_point(shape=1) +  stat_smooth(method = 'nls', 
     method.args = list(start = c(a=1, b=1)), 
     formula = y~a*exp(b*x), colour = 'black', se = FALSE)

You told R that the colour of the plot is "Exponential", I think that so is going to work (I tried with R-base dataset 'iris' and worked). You can notice that I passed the start parameter as an element of a list passed to 'method.args': this is a new feature in ggplot v2.0.0.

Hope this helps

Edit: just for completeness, I attach the code I reproduced on my laptop with default dataset: (please take care that it has no sense an exponential fit with such a dataset, but the code runs without warning)

library(ggplot2)
data('iris')
g1 <- ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
      geom_point(color='green') +geom_smooth(method = 'nls',
      method.args = list(start=c(a=1, b=1)), se = FALSE, 
      formula = y~a*exp(b*x), colour='black')
g1
Eugen
  • 442
  • 1
  • 9
  • 16