用R计算位于回归线上方和下方的点数 [英] Calculating the number of dots lie above and below the regression line with R

查看:113
本文介绍了用R计算位于回归线上方和下方的点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I calculate the number of dots that lie above and below the regression line on a scatter plot?

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)

推荐答案

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)

修改: 如果您(出于某些未知原因)这样做了:

If you did (for some unknown reason) something like this:

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)

这篇关于用R计算位于回归线上方和下方的点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆