【问题标题】:Calculating the number of dots lie above and below the regression line with R [closed]用 R 计算位于回归线上方和下方的点数[关闭]
【发布时间】:2012-10-09 11:52:49
【问题描述】:

如何计算散点图上回归线上方和下方的点数?

data = read.csv("info.csv")
par(pty = "s")
plot(data$col1, data$col2, xlab = "xaxis", ylab = "yaxis", xlim = c(0, 
  1), cex.lab = 1.5, cex.axis = 1.5, ylim = c(0, 1), col.lab = "red", 
  col = "blue", pch = 19)
abline(a = -1.21, b = 2.21)

【问题讨论】:

  • 感谢您展示您的代码,但由于我们无权访问info.csv,它仍然不是可重现代码。请看tinyurl.com/reproducible-000 ...

标签: r linear-regression


【解决方案1】:
x <- 1:10
set.seed(1)
y <- 2*x+rnorm(10)

plot(y~x)

fit <- lm(y~x)
abline(fit)

resi <- resid(fit)
#below the fit:
sum(resi < 0)
#above the fit:
sum(resi > 0)

编辑: 如果你做了(出于某种未知原因)这样的事情:

x <- 1:10
set.seed(1)
y <- 2*x+rnorm(10)

plot(y~x)
abline(-0.17,2.05)

你可以这样做:

yfit <- 2.05 * x - 0.17
resi <- y - yfit

sum(resi < 0)
sum(resi > 0)

【讨论】:

  • 非常感谢您的回答。我用一个方程来画出这样的最佳拟合线。适合= abline(a=-1.21,b=2.12)。绘图是正确的,但拟合的值为空。如何更改代码以使用此等式?
  • @user1731629 查看我对答案的编辑。但是,我想知道你从哪里得到abline() 的参数。
  • 请查看我编辑的问题。我已经展示了我的程序。
  • 你应该能够弄清楚:x &lt;- data$col1; y &lt;- data$col2.
  • @Roland 真的很棒!!!谢谢!!
【解决方案2】:

如果我正确阅读了这个问题,答案应该是。

  1. 确定回归线的方程 - 它是直的,形式为 y = mx +b,其中 m 是直线的斜率,b 是 y 截距。
  2. 计算 x 域中每个 x 的 y 值。
  3. 使用数据中的 y 值,确定它是否大于、等于或小于 y 的计算值

使用上述方法应该足以找到您所追求的数字(计数)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2022-07-23
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    相关资源
    最近更新 更多