ggplot2 - Linear regression different using R plot() and qplot() -
if create scatterplot using plot() lm(x~y) on data intercept @ 500 , when observe qplot on same data stat_smooth(method=lm), intercept @ 1000 on y axis. although slope looks visually similar on simple plot(). hope makes sense. cannot understand why difference. full functions given below. appreciated.
plot():
plot (my[[12]],my[[8]]) abline(lm(my[[12]]~my[[8]]),col="red") qplot():
mygg<-qplot(x=my[[12]],y=my[[8]]) # pretty scatterplot mygg<-mygg + stat_smooth(fullrange=true,method="lm")
it seems me variables in regressions not correspond. in lm variable my[[12]] dependent, in qplot variant independent one. using lm(my[[8]]~my[[12]] should make equivalent.
it common mistake mix variables when using plot , lm. note axis right, order of variables changes in lm compared plot.
x <- rnorm(100) y <- rnorm(100) plot(x,y) abline(lm(y ~x)) to make less confusing might use formula interface in plot well.
plot(y ~ x) abline(lm(y ~x))
Comments
Post a Comment