user1923975
user1923975

Reputation: 1389

Regression line in ggplot doesn't match computed regression

Im using R and created a chart using ggplot2.

ggplot(Month.Data, aes(y=Measure, x=Samples)) +geom_point()  + geom_smooth(method = "lm", se = FALSE)

I then create a regression so I can make some predicitions

regression <- lm(Samples ~ Measure, data = Month.Data)

I pass my data frame of "Measures" to the predict function

predict(regression, Measures)

I'd expect the predictions to be the same as if I used the regression line on the chart, but they aren't the same. Why would this be the case? Is there a setting in ggplot or is my expectation incorrect?

Upvotes: 1

Views: 1043

Answers (1)

Roland
Roland

Reputation: 132706

ggplot(Month.Data, aes(y=Measure, x=Samples)) + ...

Here your y values are Measure and your x values Samples.

regression <- lm(Samples ~ Measure, data = Month.Data)

Here your y values are Samples and your x values Measure.

Those are different models and predictions will be different since OLS minimizes the sum of squared residuals in y direction.

Upvotes: 3

Related Questions