2
$\begingroup$

I'm trying to do a simple scatterplot and trend line in R, but it doesn't look right. Have I messed up something blatantly obvious? Any ideas as to why the line doesn't fit the data?

the trend line does not fit the data

Here is the code I used.

> plot(x, y, pch=".")
> model <- lm(x ~ y)
> summary(model)

Call:
lm(formula = x ~ y)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.23043 -0.04340 -0.00533  0.03761  0.47882 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.051154   0.001384   36.97   <2e-16 ***
y           0.462881   0.003739  123.80   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.06365 on 71514 degrees of freedom
Multiple R-squared: 0.1765, Adjusted R-squared: 0.1765 
F-statistic: 1.533e+04 on 1 and 71514 DF,  p-value: < 2.2e-16 

> abline(model, col="red")
$\endgroup$
10
$\begingroup$

You need to change

model <- lm(x ~ y)

to

model <- lm(y ~ x).

Currently your fitline is showing x as a function of y instead of verse-vica.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.