Calculating the number of dots lie above and below the regression line with R [closed]

南笙酒味 提交于 2019-12-01 17:39:11
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)

Edit: 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)

You can do this:

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

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

If I've read the question properly, the answer would be.

  1. Determine the equation of the regression line - it is straight and there will be of the form y = mx +b where m is the slope of the line and b is the y intercept.
  2. Calculate the y value for each x in the domain of x.
  3. Using the value of y that you have in your data, determine whether it is greater, equal to or less than the calculated value of y

Using the above should be sufficient to find the numbers (counts) you are after.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!