【问题标题】:Get the A and B coefficients from exponential lm() model in R从 R 中的指数 lm() 模型中获取 A 和 B 系数
【发布时间】:2022-09-23 20:58:51
【问题描述】:

我试图将指数模型的 A 和 B 项写为:

mod <- lm(log(y) ~ x)

当我调用summary(mod) 时,我知道我应该取 x 的exp() 来获取 B。我如何处理截距来获取 A,以便我可以将其写成以下形式:

Y = A*B^x

  • Y=A*B^x 。使用日志规则 log(Y) = log(A) + x log(B)。我会用 B 的斜率系数 exp 和截距的 exp 来得到 A。
  • 啊,好吧-这两个术语的指数。谢谢

标签: r model lm exponentiation


【解决方案1】:

为了确定 Y=AB^x 的线性化形式的系数,您需要了解一点对数规则。首先,我们取双方的对数,得到 log(Y)=log(AB^x)。对数中的乘法与加法相同,所以我们拆分A和B^x,log(Y)=log(A)+log(B^x)。最后,对数中的指数与乘法相同,因此 log(Y)=log(A)+xlog(B)。这给出了一般线性方程 y=mx+b,其中 m = log(B),b = log(A)。当您运行线性回归时,您需要将 A 计算为 exp(截距),将 B 计算为 exp(斜率)。这是一个例子:

library(tidyverse)

example_data <- tibble(x = seq(1, 5, by = 0.1),
                       Y = 10*(4^{x}) +runif(length(x),min = -1000, max = 1000))

example_data |>
  ggplot(aes(x, Y))+
  geom_point()

model <- lm(log(Y) ~ x, data = example_data)

summary(model)
#> 
#> Call:
#> lm(formula = log(Y) ~ x, data = example_data)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -1.9210 -0.3911  0.1394  0.3597  1.9107 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)   3.6696     0.4061   9.036 2.55e-10 ***
#> x             1.0368     0.1175   8.825 4.40e-10 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.7424 on 32 degrees of freedom
#>   (7 observations deleted due to missingness)
#> Multiple R-squared:  0.7088, Adjusted R-squared:  0.6997 
#> F-statistic: 77.88 on 1 and 32 DF,  p-value: 4.398e-10

A <- exp(summary(model)$coefficients[1,1]) #intercept
B <- exp(summary(model)$coefficients[2,1]) #slope

example_data |>
  ggplot(aes(x, Y))+
  geom_point()+
  geom_line(data = tibble(x = seq(1,5, by = 0.1),
                          Y = A*B^x), color = "blue") # plot model as check

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多