Various low curve curves in graph and qplot in R
I am comparing two plots with a nonparametric lo (w) ess curve overlaid in each case. The problem is that curves look very different, even though their arguments, such as span, are identical.
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))
The plot generated qplot()
seems to have a lot more curvature. As you know, curvature detection is very important in regression analysis diagnostics, and I'm afraid that if I use ggplot2, I would draw wrong conclusions.
Could you please tell me how I could create the same curve in ggplot2?
thank
source to share
Or you can use loess(..., degree=1)
. This gives a very similar, but not exactly identical, result: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))
FROM 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")
source to share
Here is a new function stat
to use with ggplot2
that uses lowess()
: https://github.com/harrelfe/Hmisc/blob/master/R/stat-plsmo.r . To do this, you need to download the package proto
. I like to use lowess
it because it is fast for any sample size and allows you to turn off outlier detection for binary Y. But it does not provide confidence ranges.
source to share