0

我正在尝试过滤掉太接近或低于黄土曲线的点:

结果如下所示:黄土曲线散点图

显然不是想要的结果。

但是,如果我使用 scatter.smooth 函数,我会得到一个正确的曲线: 带有 scatter.smooth 曲线的散点图

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

4

1 回答 1

2

主要是,我们应该检查predict函数返回的内容:

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

它是一个向量,所以当我们将它传递给 时lines,会R说,“好吧,你没有指定 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)

}
于 2017-03-17T18:07:14.833 回答