如何在 r 中拟合指数回归?(a.k.a 改变基数的幂)

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

我正在 r 中进行指数回归。

实际上我想比较 y = exp^(ax+b)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")

我期待这样的结果(plot1),但指数回归 1 和 2 完全相同(plot2)。 地块1

剧情2

两个回归应该是不同的,因为Y值不同。 另外,我正在寻找如何在 R 中制作 y = exp(ax+b) + c fitting

你的代码是正确的,你的理论就是问题所在。型号应该是一样的。

最简单的方法是在对数尺度上思考,就像您在代码中所做的那样。从 y = exp(ax + b) 开始,我们可以到达 log(y) = ax + b,因此一个以 log(y) 作为响应的线性模型。使用 y = 5^(cx + d),我们可以得到 log(y) = (cx + d) * log(5) = (c*log(5)) * x + (d*log(5)),也是一个以 log(y) 作为响应的线性模型。 Yhe 模型 fit/predictions 与不同的基数没有任何不同,您可以通过将基数 e 系数乘以 log(5) 将它们转换为基数 5 系数。 a = c*log(5)b = d*log(5).

这有点像想要比较线性模型 y = ax + b 其中 x 以米为单位与 y = ax + b 其中 x 以厘米为单位进行测量。系数会发生变化以适应比例,但拟合度没有任何不同。

第一部分已由 回答,第二部分 "...我正在寻找如何使 y = exp(ax+b) + c 适合 R" 可以用 nls:

完成
fit_er3  <-  nls(y ~ exp(a*x+b) + c, data = df, start=list(a=1,b=0,c=0))