为什么线性模型的系数在图上不相同?在R

时间:2017-05-02 23:53:08

标签: r plot linear-regression

我被R难倒了。这可能不是这类问题的最佳论坛,如果我将其发布在其他地方,请道歉。

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

enter image description here

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

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

显然y-intercept更像是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

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

1 个答案:

答案 0 :(得分:4)

看起来你只是误读了情节 - 截距是线穿过x = 0的地方:

plot of data with a line drawn up along actual y-axis to fitted regression line and horizontal across to the plot margin

当做“香草R”情节时,我通常会在“真实”轴上用灰色标记

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

如果你把它放在行abline(fit)之前,你会更清楚地看到拦截。