I'm currently working on modifying some R-code I got provided to fit my needs.
The situation is the following:
We are plotting ~200 lines. They then used LOWESS to get a best-fit curve.
It looks like this right now:
lines(lowess(x.lowess, y.lowess), lwd = 3)
where x.lowess and y.lowess are the corresponding coordinates, each in a vector, such as:
> dput(x.lowess)
c(0.268309377138946, 0.511978097193703, 0.785763175906913, 0.974674880219028, ... )
> dput(y.lowess)
c(0.8, 0.5, 0.8, 0.5, ... )
I am now looking the get a running median curve instead of a LOWESS best-fit curve.
Is there any simple way/function for doing this?
for an example of the plot, seee this on flickr (sorry, couldn't upload it directly, i'm new here and it's not allowed :) plot with lowess smoothing curve in red
Generate some sample data:
set.seed(1001)
x <- runif(1000)
y <- runif(1000)
dat <- data.frame(x,y)
Use the quantreg package to find the median as a function of x:
library(quantreg)
q1 <- rq(y~x,data=data.frame(x,y))
xvec <- seq(0,1,length=101)
pq <- predict(q1,newdata=data.frame(x=xvec))
Draw in base graphics:
plot(x,y,pch=".")
lines(lowess(x,y))
lines(xvec,pq,col=2)
Or using ggplot2:
library(ggplot2)
theme_set(theme_bw())
qplot(x,y,data=dat,size=I(0.8),alpha=I(0.2))+
geom_smooth(method="loess")+
stat_quantile(quantiles=0.5,formula=y~x,colour="red")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With