2017-03-17 88 views
0

我试图筛选出过于接近或低于黄土曲线点正确安装黄土曲线:的R - 不是通过点

结果看起来是这样的:Scatterplot with loess curve

显然不是理想的结果。

然而,如果我使用scatter.smooth功能我得到正确的寻找曲线: Scatterplot with scatter.smooth curve

我该如何正确通过我的数据拟合曲线黄土?

回答

2

主要是,我们应该检查一下predict函数返回:

head(predict(afit)) 
[1] 0.8548271 0.8797704 0.8584954 0.8031563 0.9012096 0.8955874 

这是一个载体,所以当我们把它传递给linesR说,“没关系,你没有指定的x值,所以我只是使用索引为x值“(尝试plot(2:10)看看我的意思)。

因此,我们需要做的是指定了2列矩阵传递给lines,而不是:

cbind(sort(means), predict(afit, newdata = sort(means)))

应该做的伎俩。你的功能可以写成:

FilterByVariance<-function(dat, threshold = 0.90, span = 0.75){ 
means <- apply(dat,1,mean) 
sds <- apply(dat,1,sd) 
cv <- sqrt(sds/means) 

afit<-loess(cv~means, span = span) 
resids<-afit$residuals 
# good<-which(resids >= quantile(resids, probs = threshold)) 
# points above the curve will have a residual > 0 
good <- which(resids > 0) 
#plots 

plot(cv~means) 
lines(cbind(sort(means), predict(afit, newdata = sort(means))), 
     col="blue",lwd=3) 
points(means[good],cv[good],col="red",pch=19) 

} 
+0

非常感谢!这明确地解决了错误绘制曲线的问题。然而,红点仍沿着错误的黄土曲线分散。我可以将它们放在绘制的黄土曲线上吗? – Flomp

+0

你是什么意思,他们分散在错误的曲线?你是否希望看到低于黄土的点__和___? – bouncyball

+0

选定的点仍然是“随机”散布在曲线周围。 http://imgur.com/a/fmKXm 我想只选择曲线上方的那些数据点。 – Flomp