作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 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值不同,两次回归应该不同。另外,我正在寻找如何制作 y = exp(ax+b) + c fitting
在 R 中。
最佳答案
你的代码是正确的,你的理论就是问题所在。模型应该是相同的。
最简单的方法是考虑对数尺度,就像您在代码中所做的那样。从 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)
作为回应。不同的基数模型拟合/预测不会有任何不同,您可以转换基数 e
coefs 到基数 5
coefs 乘以 log(5)
。 a = c*log(5)
和b = d*log(5)
.
这有点像想要比较线性模型 y = ax + b 其中 x
以米为单位 vs y = ax + b 其中 x
以厘米为单位测量。系数将发生变化以适应比例,但拟合没有任何不同。
关于r - 如何在 r 中拟合指数回归?(又名基数变化幂),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57881543/
我是一名优秀的程序员,十分优秀!