如何将loess.smoothing应用于图并提取点? [英] How to apply loess.smoothing to both plot and then extract points?

查看:426
本文介绍了如何将loess.smoothing应用于图并提取点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to apply loess smoothing to a scatterplot (i.e. between two quantitative variables). I would like to plot where the loess smoothing occurs in the scatterplot, and then would like to extract only the data points in the scatterplot that are above that smoothing.

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

For instance, if this is my scatterplot:

qplot(mpg, cyl, data=mtcars)

我想叠加平滑器:

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

这会导致错误:不知道如何在绘图中添加o".

This results in the error: "Don't know how to add o to a plot".

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

Then, assuming I could get that superimposition to work, I would like to extract only the cars that are above that line.

推荐答案

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

[Disclaimer: this answer is incomplete]

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

ggplot2 has a function for adding a loess smoother: stat_smooth(), e.g.

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")

功能帮助页面还指出,它会返回data.frame预测,可用于提取点. 此SO答案通过了它.不幸的是,它通常将其拆分为80个点,这些点可能与数据不一致,因此您必须进行一些插值操作才能使这些点位于上方/下方.

The function help page also states it returns a data.frame with predictions, which you can use to extract points. This SO answer goes through it. Unfortunately it splits it into typically 80 points, which might not align with the data, so you'll have to do some interpolation to get the points above/below.

P.S.这是两个问题-我建议以后再拆分.

P.S. this is kind of two questions - I'd recommend splitting them in the future.

这篇关于如何将loess.smoothing应用于图并提取点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆