3

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

4

1 回答 1

2

生成一些示例数据:

set.seed(1001)
x <- runif(1000)
y <- runif(1000)
dat <- data.frame(x,y)

使用quantreg包求中值作为 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))

在基础图形中绘制:

plot(x,y,pch=".")
lines(lowess(x,y))
lines(xvec,pq,col=2)

或使用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")
于 2013-01-20T21:51:25.850 回答