简体   繁体   中英

R best fit of 45 degree line

(i know it must be incredibely easy, but i'm strugling with it in R:)

i have dataset of x and y values saved in X and Y vectors. I know that plot of the data should follow exactly -45 degree line (see image below)

在此处输入图片说明

How do i find such -45 degree line that best fits the data (+ all these statistics available from summary(lm(...))? I've tried lm, but i can't force it to abandon fitting the slope parameter

Thank you

After trying: lm(y~1,offset=-x) and applying abline(coefficient, -1) i obtain following plot (see below) 在此处输入图片说明

black line is abline plot, yellow one is mine guess of fit -- what's wrong with lm or do i miss totally something?

Since you state that:

y = -1*x + b

then

y+x = b

So calculate the mean of (y+x) and you get the average value of b

mean(y+x)

I believe the solution from @BenBolker is correct and perhaps you are using the wrong coefficient:

lm1 <- lm(y~1,offset=-x,data=df)
plot(df)
abline(coefficients(lm1),-1)

This produces:

在此处输入图片说明

This fit looks like the correct fit to me. The intercept is -2.217.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM