Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different lowess curves in plot and qplot in R

I am comparing two graphs with a non-parametric lo(w)ess curve superimposed in each case. The problem is that the curves look very different, despite the fact that their arguments, such as span, are identical.

enter image description here

enter image description here

y<-rnorm(100)
x<-rgamma(100,2,2)
qplot(x,y)+stat_smooth(span=2/3,se=F)+theme_bw()
plot(x,y)
lines(lowess(y~x))

There seems to be a lot more curvatute in the graph generated by qplot(). As you know detecting curvature is very important in the diagnostics of regression analysis and I fear that If I am to use ggplot2, I would reach erroneous conclusions.

Could you please tell me how I could produce the same curve in ggplot2?

Thank you

like image 640
JohnK Avatar asked Dec 13 '25 09:12

JohnK


1 Answers

Or, you can use loess(..., degree=1). This produces a very similar, but not quite identical result to lowess(...)

set.seed(1)    # for reproducibility
y<-rnorm(100)
x<-rgamma(100,2,2)
plot(x,y)
points(x,loess(y~x,data.frame(x,y),degree=1)$fitted,pch=20,col="red")
lines(lowess(y~x))

With ggplot

qplot(x,y)+stat_smooth(se=F,degree=1)+
  theme_bw()+
  geom_point(data=as.data.frame(lowess(y~x)),aes(x,y),col="red")

like image 100
jlhoward Avatar answered Dec 16 '25 00:12

jlhoward