如何将 loess.smoothing 应用于绘图然后提取点?

How to apply loess.smoothing to both plot and then extract points?

我正在尝试将黄土平滑应用于散点图(即两个定量变量之间)。我想绘制散点图中发生黄土平滑的位置,然后只提取散点图中高于该平滑的数据点。

例如,如果这是我的散点图:

qplot(mpg, cyl, data=mtcars)

我想叠加更平滑的:

qplot(mpg, wt, data=mtcars) + with(mtcars, loess.smooth(mpg, wt))

这会导致错误:"Don't know how to add o to a plot"。

然后,假设我可以使该叠加起作用,我只想提取该线上方的汽车。

[免责声明:此答案不完整]

ggplot2有添加黄土平滑剂的功能:stat_smooth(),例如

qplot(mpg, cyl, data=mtcars) + stat_smooth()
# For datasets with n < 1000 default is loess, to hard-code:
qplot(mpg, cyl, data=mtcars) + stat_smooth(method="loess")

function help page also states it returns a data.frame with predictions, which you can use to extract points. This SO answer 通过它。不幸的是,它通常将其分成 80 个点,这可能与数据不一致,因此您必须进行一些插值才能获得这些点 above/below.

P.S。这是两个问题 - 我建议以后将它们分开。