散点图的连续分位数

4

我有一个数据集,我使用 ggplot2stat_smooth 绘制了回归曲线:

ggplot(data = mydf, aes(x=time, y=pdm)) + geom_point() + stat_smooth(col="red") 

plot1

我希望能够使用相同的方法来获取分位数(如果更简单,只获取四分位数也可以)。我只能得到以下结果:
ggplot(data = mydf, aes(x=time, y=pdm, z=surface)) + geom_point() + stat_smooth(col="red") + stat_quantile(quantiles = c(0.25,0.75)) 

enter image description here

很不幸,我无法在stat_quantile()中放置method="loess",如果我没有弄错的话,这将解决我的问题。
(如果不清楚,期望的行为=分位数的非线性回归,因此Q25和Q75的回归曲线在我的红色曲线下方和上方(分别),如果绘制Q50,则是我的红色曲线)。
谢谢。
1个回答

4
stat_quantile 默认情况下会在每个 x 值处绘制 25% 和 75% 的最佳拟合线。 stat_quantile 使用 quantreg 包中的 rq 函数(在 stat_quantile 调用中隐式使用 method="rq")。据我所知,rq 不执行 loess 回归。但是,您可以使用其他灵活的函数进行分位数回归。以下是两个示例:

B-Spline:

library(splines)

stat_quantile(formula=y ~ bs(x, df=4), quantiles = c(0.25,0.75))

二次多项式:

stat_quantile(formula=y ~ poly(x, 2), quantiles = c(0.25,0.75))

stat_quantile 仍然使用 rq,但是 rq 接受上述类型的公式(如果您不提供公式,则 stat_quantile 隐含地使用 formula=y~x)。如果您在 geom_smooth 中使用与 stat_quantile 相同的公式,则用于分位数和期望均值的回归方法将保持一致。


感谢您的输入。 在formula参数中放置来自statsloess()是不是可能的? - François M.
我正在尝试使用loess,代码如下:stat_quantile(formula = loess(mydf$pdm ~ mydf$time), quantiles = c(0.25, 0.5, 0.75)),但是出现了以下错误提示:Warning messages:
1: 'newdata' had 100 rows but variables found have 6816 rows 2: Computation failed in 'stat_quantile()':
replacement has 6816 rows, data has 100,我不知道它想告诉我什么。
- François M.
我无法在stat_quantile中使用loessstat_quantile使用quantreg包中的rq函数。 rq处理具有多项式和样条的公式(如我的答案),但不执行loess回归(据我所知)。 关于语法的说明:如果loess函数可以执行分位数回归,则调用将类似于此:stat_quantile(method =“loess”,quantiles = c(0.25,0.75))(这也隐含地使用formula = y〜x)。 - eipi10
是的,我也试过了,但没有成功。还是谢谢你的尝试。虽然我不确定这是否正是我想要的,但最终我做了这里 https://stat.ethz.ch/pipermail/r-help/2011-October/291575.html 中的操作,将分位数放入DF中,并在每个分位数上绘制 stat_smooth()ggplot(data = qdfm, aes(x=time, y=pdm)) + geom_point() + geom_smooth(col="red") + stat_smooth(data = qdfm, aes(x = time, y = Q25)) + stat_smooth(data = qdfm, aes(x = time, y = Q75)) - François M.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接