Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting deviations from regression line

Tags:

r

scatter-plot

I want to plot a regression line with (a = 0 and b = 1) and add the individual point deviations from this along with identifying the data point with name.

set.seed(123)
namelab <- paste ("ET", 1:10, sep = "")
xvar <- 1:10
yvar <- rnorm(10, 5, 5)
myd <- data.frame(namelab, xvar, yvar)
plot(xvar, yvar)
abline (a= 0, b = 1, col = "red", lty = 2)

Just manual sketch of my intention, I just labelled a single point just for example. The line drawn need a slim. enter image description here

like image 759
jon Avatar asked Nov 10 '11 04:11

jon


People also ask

How do you find the standard deviation of a regression line?

S(errors) = (SQRT(1 minus R-squared)) x STDEV. So, if you know the standard deviation of Y, and you know the correlation between Y and X, you can figure out what the standard deviation of the errors would be be if you regressed Y on X.

What is deviation from regression?

The standard deviation of the residuals calculates how much the data points spread around the regression line. The result is used to measure the error of the regression line's predictability.


1 Answers

dev.new(width=4, height=4)
plot(xvar, yvar, asp=1)

a = 0
b = 1

abline (a, b, col = "red", lty = 2)

myd$xint = with(myd, (b*yvar + xvar - b*a) / (b^2 + 1))
myd$yint = with(myd, (b*yvar + b*xvar + a) / (b^2 + 1))

with(myd, segments(xvar, yvar, xint, yint))
with(myd, text(xvar, yvar, labels=namelab, pos=3, cex=0.5))

enter image description here

like image 161
John Colby Avatar answered Sep 30 '22 17:09

John Colby