用R计算回归线上方和下方的点数

use*_*629 6 r linear-regression

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

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)
Run Code Online (Sandbox Code Playgroud)

Rol*_*and 14

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)
Run Code Online (Sandbox Code Playgroud)

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

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

plot(y~x)
abline(-0.17,2.05)
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

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

sum(resi < 0)
sum(resi > 0)
Run Code Online (Sandbox Code Playgroud)