Search code examples
rtime-serieslinear-regressionforecastingexponential

How to fit exponential regression in r?(a.k.a changing power of base)


I am making exponential regressions in r.

Actually I want to compare y = exp^(ax+b) with y = 5^(ax+b).

# data 
set.seed(1)
y <- c(3.5, 2.9, 2.97,4.58,6.18,7.11,9.50,9.81,10.17,10.53,
       12.33,14.14,18, 22, 25, 39, 40, 55, 69, 72) + rnorm(20, 10, 1)
x <- 1:length(y)


df = data.frame(x = x, y = y)
predata = data.frame(x = 1:20)

# plot
plot(df, ylim = c(0,100), xlim = c(0,40))


# simple linear regression
fit_sr = lm(y~x, data = df)
pre_sr = predict(fit_sr, newdata = predata, 
                 interval ='confidence',
                 level = 0.90)
lines(pre_sr[,1], col = "red")

# exponential regression 1
fit_er1 = lm(log(y, base = exp(1))~x, data = df)
pre_er1 = predict(fit_er1, newdata = predata, 
                  interval ='confidence',
                  level = 0.90)
pre_er1 = exp(1)^pre_er1 # correctness
lines(pre_er1[,1], col = "dark green")


# exponential regression 2
fit_er2 = lm(log(y, base = 5) ~ x, data = df)
pre_er2 = predict(fit_er2, newdata = predata, 
                  interval ='confidence',
                  level = 0.90)
pre_er2 = 5^pre_er2 # correctness
lines(pre_er2[,1], col = "blue")

I expect something like this(plot1), but exponential regression 1 and 2 are totally the same(plot2). plot1 enter image description here

plot2 enter image description here

The two regression should be different because of the Y value is different. Also, I am looking for how to make y = exp(ax+b) + c fitting in R.


Solution

  • Your code is correct, your theory is where the problem is. The models should be the same.

    Easiest way is to think on the log scale, as you've done in your code. Starting with y = exp(ax + b) we can get to log(y) = ax + b, so a linear model with log(y) as the response. With y = 5^(cx + d), we can get log(y) = (cx + d) * log(5) = (c*log(5)) * x + (d*log(5)), also a linear model with log(y) as the response. Yhe model fit/predictions will not be any different with a different base, you can transform the base e coefs to base 5 coefs by multiplying them by log(5). a = c*log(5) and b = d*log(5).

    It's a bit like wanting to compare the linear models y = ax + b where x is measured in meters vs y = ax + b where x is measured in centimeters. The coefficients will change to accommodate the scale, but the fit isn't any different.