Question

I have created a scatter plot of two vectors, using R, combined with a line (using abline) which represents the x=y diagonal. I wish to calculate the standard deviation of the dots from the diagonal, and color the area which is found between the first and third quantiles.
I have no idea how to do this, and would appreciate all help!!! Thanks in advance. Haj.

Was it helpful?

Solution

Well, what you want to do is this :

# sample data
x <- rnorm(50,0,2)
y <- x+rnorm(50,0,2)

# construct polygons
div <- quantile(y-x,c(0.25,0.75))
x1 <- min(c(x,y))
x2 <- max(c(x,y))


plot(x,y,type="n")
polygon(x=c(x1,x1,x2,x2),y=c(x1+div,(x2+div)[c(2,1)]),col="grey")
abline(0,1)
points(x,y)

What I would do is this :

qplot(x,y,geom="point") + stat_smooth(method="lm")

The standard deviation you'd like to calculate is

sd(y-x)

The correct measure you're probably looking for is:

sd(residuals(lm(y~x)))

You should think in terms of a linear model of y on x to get any meaningful result, unless you have very good reasons not to do so. If the relation between x and y is not 1 on 1, then assuming the correct model is so won't make sense. And if the relation of x on y is not 1 on 1, y-x will not be normally distributed and hence the sd will be difficult to interprete in a meaningful way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top