【问题标题】:Why isn't the coefficient for the linear model showing up the same on the plot? in R [duplicate]为什么线性模型的系数在图中显示不一样?在 R [重复]
【发布时间】:2017-05-03 08:19:40
【问题描述】:

我被 R 难住了。这可能不是解决这类问题的最佳论坛,所以如果我应该在其他地方发布此问题,我们深表歉意。

我对一些看起来像这样的数据进行了线性回归:

但是当我调用线性模型的系数时,它显示为:

> fit$coefficients
(Intercept)   data$logA 
3.1370219   0.1718147 

显然,y 截距更像是 2 而不是 3。这怎么可能?!

这是我的代码:

data <- read.csv("Diamond1976_Table1.csv",header=T,sep=",") # read the csv file 
# Make the first species-area curve
Area <- data$A_mi2
Slow <- data$S_low
plot(Area, Slow, xlab = "Area (mi^2)",
     ylab = "Species Richness",
     pch = 15, cex = 1, col = "skyblue")

# Take the logarithm of the data
data$logS_low = log(data$S_low)
data$logS_mt = log(data$S_mt)
data$logA = log(data$A_mi2)

# Log plot
plot(data$logA, data$logS_low, xlab = "Log area",
     ylab = "Log species richness", main = "Log-log plot Diamond (1976)",
     pch = 15, cex = 1, col = "skyblue")
fit <- lm(data$logS_low ~ data$logA) # tilda is "explained by"
abline(fit)
summary(fit)
fit$coefficients

谁能找到一个明显的错误?如果有帮助,我可以发布数据,但我觉得有人可以解释为什么在没有数据的情况下会发生这种情况。

【问题讨论】:

  • 当 x 轴上的值为 0 时,截距为 y 轴上的值
  • 所以调用类似 ``fit[y="0?]` 的东西应该让我得到 y 截距?
  • fit 预测任何 x 值的 y 值:predict(fit, newdata=data.frame(logA=0))logA=0 会给你 y 截距。除了logA=0,您可以输入任何所需x 值的向量,例如predict(fit, newdata=data.frame(logA=seq(-6,6,length.out=100)))
  • @eip10 我得到错误:Warning message: 'newdata' had 100 rows but variables found have 50 rows
  • 我认为您误读了图表。您说“显然 y 截距更像 2 而不是 3”。最左边的高度是 2,但不是 x=0,而是大约 x = 6.2。如果考虑到 x 轴和 y 轴上的比例不同,那么显示的斜率看起来是正确的。

标签: r plot linear-regression


【解决方案1】:

看起来你只是误读了情节——截距是线与 x=0 相交的地方:

在做“vanilla R”绘图时,我通常会在“真实”轴上用灰色标记

abline(h=0,v=0,col=8)

如果您将其放在行 abline(fit) 之前,您将获得更清晰的拦截指示。

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 2021-07-05
    • 1970-01-01
    • 2017-08-28
    • 2019-07-11
    • 2017-04-19
    • 2023-03-14
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多