【问题标题】:The regression line don't pass through the cloud of points回归线不通过点云
【发布时间】:2025-11-24 04:25:01
【问题描述】:

代码是这里的这个。

altura <- read.table("altura.txt", header=T, quote="\"")
altura <- cbind(altura, altura$Esposa/altura$X.Marido, altura$X.Marido/altura$Esposa)
is.data.frame(altura)
names(altura) <- c("marido","esposa","r1","r2")
with(altura,plot(marido~esposa))
g1 <- lm(altura$esposa~altura$marido)
summary(g1)
abline(g1$coefficients)
abline(0,1,lty=5)
with(altura,plot(esposa~marido))
g2 <- lm(altura$marido~altura$esposa)
summary(g2)
abline(g2$coefficients)
abline(0,1,lty=5)
cor(altura$marido,altura$esposa)

简单的回归线不会穿过点云。 abline 正在使用来自汇总函数的正确截距。这不是第一次发生这种情况。如您所见,在这两个图表中,我都是这个问题。一条线经过这些点,另一条在下方。

【问题讨论】:

  • 请任何人帮忙?我认为计算错误的系数有可能吗?正常吗?
  • 您可以发布您的数据或图表图片吗?

标签: r plot regression linear-regression


【解决方案1】:

我想我明白了:您的 g2 模型: g2 &lt;- lm(altura$marido~altura$esposa 应该与 with(altura,plot(marido~esposa)) 一起使用,您可以与 with(altura,plot(esposa~marido)) 一起使用

例如

set.seed(1021)

x <- rnorm(100)
y <- 3*x + rnorm(100)
m1 <- lm(y~x)
plot(y~x)
abline(m1$coefficients)
m2 <- lm(x~y)
abline(m2$coefficients, col = 'red')

你在你想要黑色的地方绘制红线,反之亦然。

【讨论】:

  • 不幸的是,如果你有一个分类预测器,这似乎不起作用,想知道为什么:x