6

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)
user5249203
  • 4,436
  • 1
  • 19
  • 45
  • 3
    thanks for showing your code, but it's still not **reproducible** code since we don't have access to `info.csv`. Please take a look at http://tinyurl.com/reproducible-000 ... – Ben Bolker Oct 09 '12 at 13:31

2 Answers2

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)

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)
Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thank you very much for your answer. I used an equation to draw the line of best fit like this. fit= abline(a=-1.21, b=2.12). Plot is correct but the value of fit is null. How to change your code to use this equation? – user1731629 Oct 09 '12 at 12:43
  • @user1731629 See my edit to the answer. However, I wonder where you got the parameters for `abline()`. – Roland Oct 09 '12 at 12:49
  • Please see my edited question. I have shown my program. – user1731629 Oct 09 '12 at 13:16
  • You should be able to figure that out: `x <- data$col1; y <- data$col2`. – Roland Oct 09 '12 at 13:41
1

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.

Big Bream
  • 152
  • 3