为什么图中显示的线性模型的系数不同?在 R

Why isn't the coefficient for the linear model showing up the same on the plot? in R

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

我对一些数据进行了线性回归,如下所示:

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

> 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

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

看来你只是误读了情节——截距是直线穿过 x=0:

的地方

在绘制 "vanilla R" 绘图时,我通常会在 "true" 轴上用

标记为灰色
abline(h=0,v=0,col=8)

如果你把它放在行 abline(fit) 之前,你会得到更清楚的拦截指示。