-1

I wonder how to fit these data with an exponential regression model and how to print the exponential regression equation and R2 on ggplot graph. It would be better to fit the data using geom_smooth(). I have found several possible solutions but exclusively for linear regression while I'm interested in the exponential one. This is my code:

library(ggplot2)

##### Creating the data frame
x <- c(0.54,0.59,0.57,0.54,0.10,0.07,0.17,0.24,0.51,0.57,0.55,0.52,0.40,0.43,0.45,0.43)
y <- c(156.9,234.8,191.9,203.2,59.8,36.0,87.2,23.8,168.7,182.3,155.8,205.1,101.2,115.5,118.8,159.1)
df <- data.frame(x, y)


##### Creating the plot
my_plot <- ggplot(data = df, mapping = aes(x, y)) + 
  geom_point()
my_plot

This is the resulted plot: R plot

Any help will be greatly appreciated.

  • 1
    I don't see "exponential regression" in your code. I see a simple quadratic regression (which *is* linear regression, you are using `lm` after all). – Roland Jul 21 '20 at 09:21

1 Answers1

0

It's not clear what form you expect the regression to take. I assume from the shape of the data you want to regress the log of y on x.

If this is the case, it is probably simplest to create the regression line using predict and building the text of the equation as a plotmath expression.

Here's a full reprex:

library(ggplot2)

x <- c(0.54, 0.59, 0.57, 0.54 ,0.10, 0.07, 0.17, 0.24, 
       0.51, 0.57, 0.55, 0.52, 0.40, 0.43, 0.45, 0.43)
y <- c(156.9, 234.8, 191.9, 203.2, 59.8, 36.0, 87.2, 23.8, 168.7,
       182.3, 155.8, 205.1, 101.2, 115.5, 118.8, 159.1)
df <- data.frame(x, y)

exp.mod    <- lm(log(y) ~ x, df)

new_x      <- seq(min(x), max(x), 0.01)
prediction <- exp(predict(exp.mod, newdata = list(x = new_x)))
exp_line   <- data.frame(x = new_x, y = prediction)

eq <- paste0('paste(y, " = ", italic(e^{',  round(exp.mod$coefficients[2], 2), 
             "*x ~~+~~ ", round(exp.mod$coefficients[1], 2),
             '}), ~~~~~~~~R^2~ "="~', round(summary(exp.mod)$r.squared, 2), ")")

my_plot <- ggplot(data = df, mapping = aes(x, y)) + 
  geom_point() +
  geom_line(data = exp_line) +
  geom_text(aes(x = min(x) + 0.1 * diff(range(x)), 
                y = min(y) + 0.9 * diff(range(y)), label = eq), 
            parse = TRUE, size = 5, check_overlap = TRUE, hjust = 0)

my_plot

Created on 2020-07-21 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks for the comment. You are both right and for this reason I updated the question as you can see above. My goal is to fit these data with an exponential regression model and to print the exponential regression equation and R2 on the graph. – Federico Grillo Jul 21 '20 at 10:07
  • The coefficients in the plot don't fit the plotted line. You need to use `exp.mod <- lm(y ~ poly(x, 2, raw = TRUE), df)`. – Roland Jul 21 '20 at 10:41
  • @Roland it seems that's not what the OP wanted anyway. I think it was a best fitting exponential line after all - now updated. – Allan Cameron Jul 21 '20 at 11:19
  • here is an integrated example you might like [here](https://stackoverflow.com/a/66050809/4927395) – Mark Neal Feb 04 '21 at 17:58