I want to measure the distance between a set of points and a 1:1 line. I can build a linear model and get the residuals from the best fit, but I cant get the measure from a 1:1 line. Any helpful hints?
#build a df of random numbers
x=runif(100, 0, 100)
y=runif (100, 0, 100)
df=cbind (x,y)
df=as.data.frame(df)
#build a linear model
lm1<-lm(y~x, data=df)
summary (lm1)
#plot the data, lm best fit and 1:1 (red) line)
plot (y~x, data=df, pch=16)
line (lm1)
abline abline(0,1, col="red")
#get residulas for the linear model
y.resid= resid (lm1)
I suggest using y-x, just like @vpipkt suggested. Just for the sake of completeness: you can also create a linear model with fixed coefficients y-x ~ 0 and take the residual there.
resid(lm(y-x ~ 0))
Of course this is just more complicated and gives the same result as y-x, but it explicitely states that you are taking residuals and not calculating the minimal distance to the line (cf @user3969377's answer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With