【问题标题】:add a logarithmic regression line to a scatterplot (comparison with Excel)向散点图添加对数回归线(与 Excel 比较)
【发布时间】:2012-10-04 14:18:48
【问题描述】:

在 Excel 中,很容易拟合一组给定趋势线的对数趋势线。只需单击添加趋势线,然后选择“对数”。切换到R 以获得更多功能,我有点不知道应该使用哪个函数来生成它。

为了生成图表,我使用ggplot2 和以下代码。

ggplot(data, aes(horizon, success)) + geom_line() + geom_area(alpha=0.3)+
  stat_smooth(method='loess')

但代码会进行局部多项式回归拟合,该拟合基于对许多小的线性回归进行平均。我的问题是R 中是否有类似的日志趋势线在 Excel 中使用。

编辑:我正在寻找的另一种方法是获取形式为 y = (c*ln(x))+b; 的对数方程是否有 coef() 函数来获取“c”和“b”?

Edit2:由于我的声望更高,我现在可以发布更多关于我正在努力做的事情。让我的数据是:

0.599885189,0.588404133,0.577784156,0.567164179,0.556257176,0.545350172,0.535112897,
0.52449292,0.51540375,0.507271336,0.499904325,0.498851894,0.498851894,0.497321087,
0.4964600,0.495885955,0.494068121,0.492154612,0.490145427,0.486892461,0.482395714,
0.477229238,0.471010333

上述数据是 y 点,而 x 点只是从 1:length(y) 开始的整数,以 1 为增量。在 Excel 中:我可以简单地绘制它并添加对数趋势线,结果如下所示:

黑色是原木。在 R 中,如何处理上述数据集?

【问题讨论】:

  • 您可以使用log() 函数获取数据日志,并使用loess() 函数拟合平滑器。使用plot() 绘制数据图,并使用lines() 添加(平滑)线。
  • 是否有可能只得到方程式? excel中的等式是y=(c*ln(x))+b

标签: r regression


【解决方案1】:

我更喜欢使用基本图形而不是ggplot2

#some data with a linear model
x <- 1:20
set.seed(1)
y <- 3*log(x)+5+rnorm(20)

#plot data
plot(y~x)

#fit log model
fit <- lm(y~log(x))
#look at result and statistics
summary(fit)
#extract coefficients only
coef(fit)

#plot fit with confidence band
matlines(x=seq(from=1,to=20,length.out=1000),
         y=predict(fit,newdata=list(x=seq(from=1,to=20,length.out=1000)),
                   interval="confidence"))

#some data with a non-linear model
set.seed(1)
y <- log(0.1*x)+rnorm(20,sd=0.1)

#plot data
plot(y~x)

#fit log model
fit <- nls(y~log(a*x),start=list(a=0.2))
#look at result and statistics
summary(fit)

#plot fit
lines(seq(from=1,to=20,length.out=1000),
      predict(fit,newdata=list(x=seq(from=1,to=20,length.out=1000))))

【讨论】:

    【解决方案2】:

    您可以轻松指定替代平滑方法(例如lm(),线性最小二乘拟合)和替代公式

    library(ggplot2)
    g0 <- ggplot(dat, aes(horizon, success)) + geom_line() + geom_area(alpha=0.3)
    g0 + stat_smooth(method="lm",formula=y~log(x),fill="red")
    

    置信带会自动包含在内:我更改了颜色以使其可见,因为它们非常窄。您可以在stat_smooth 中使用se=FALSE 将其关闭。

    另一个答案向您展示了如何获得系数:

    coef(lm(success~log(horizon),data=dat))
    

    我可以想象您接下来可能想要将方程添加到图表中:请参阅 Adding Regression Line Equation and R2 on graph

    【讨论】:

    • 除了geom_line之外,您使用geom_area还有什么原因吗?我不确定它是否使情节易于理解
    • 我从 OP 的示例中复制了它:我认为他们有它是因为他们想要它,但我想他们可能只是从一个示例中复制它自己...
    【解决方案3】:

    我很确定一个简单的 +scale_y_log10() 会得到你想要的。 GGPlot stats 是在转换后计算的,因此 loess() 将在对数转换后的数据上进行计算。

    【讨论】:

      【解决方案4】:

      我刚刚写了一个blog post here,描述了如何精确匹配 Excel 的对数曲线拟合。方法的核心围绕lm()函数:

      # Set x and data.to.fit to the independent and dependent variables
      data.to.fit <- c(0.5998,0.5884,0.5777,0.5671,0.5562,0.5453,0.5351,0.524,0.515,0.5072,0.4999,0.4988,0.4988,0.4973,0.49,0.4958,0.4940,0.4921,0.4901,0.4868,0.4823,0.4772,0.4710)
      x <- c(seq(1, length(data.to.fit)))
      data.set <- data.frame(x, data.to.fit)
      
      # Perform a logarithmic fit to the data set
      log.fit <- lm(data.to.fit~log(x), data=data.set)
      
      # Print out the intercept, log(x) parameters, R-squared values, etc.
      summary(log.fit)
      
      # Plot the original data set
      plot(data.set)
      
      # Add the log.fit line with confidence intervals
      matlines(predict(log.fit, data.frame(x=x), interval="confidence"))
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-24
        • 1970-01-01
        • 2020-11-01
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        相关资源
        最近更新 更多