gpt4 book ai didi

r - 如何在 r 中拟合指数回归?(又名基数变化幂)

转载 作者:行者123 更新时间:2023-12-03 08:52:49 26 4
gpt4 key购买 nike

我正在 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 enter image description here

情节2 enter image description here

由于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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com