Search code examples
rmodel-fitting

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)

enter image description here

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) enter image description here

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


Solution

  • 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:

    enter image description here

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