【发布时间】:2017-03-17 17:57:47
【问题描述】:
【问题讨论】:
标签: r regression curve loess
【问题讨论】:
标签: r regression curve loess
主要是检查predict函数返回的内容:
head(predict(afit))
[1] 0.8548271 0.8797704 0.8584954 0.8031563 0.9012096 0.8955874
它是一个向量,所以当我们将它传递给lines 时,R 会说,“好吧,你没有指定 x 值,所以我将只使用 x 值的索引”(试试@ 987654326@ 了解我的意思)。
所以,我们需要做的是指定一个 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)
}
【讨论】: