R ggplot:加权CDF [英] R ggplot: Weighted CDF
问题描述
我想使用ggplot
绘制加权CDF.一些非SO的旧讨论(例如,来自的此 2012年)表明这是不可能的,但我想加薪.
I'd like to plot a weighted CDF using ggplot
. Some old non-SO discussions (e.g. this from 2012) suggest this is not possible, but thought I'd reraise.
例如,考虑以下数据:
df <- data.frame(x=sort(runif(100)), w=1:100)
我可以使用以下方式显示未加权的CDF
I can show an unweighted CDF with
ggplot(df, aes(x)) + stat_ecdf()
我如何用w
加权呢?对于此示例,我希望使用x^2
外观的函数,因为较大的数字具有较高的权重.
How would I weight this by w
? For this example, I'd expect an x^2
-looking function, since the larger numbers have higher weight.
推荐答案
您的答案有误.
这是计算加权ECDF的正确代码:
This is the right code to compute the weighted ECDF:
df <- df[order(df$x), ] # Won't change anything since it was created sorted
df$cum.pct <- with(df, cumsum(w) / sum(w))
ggplot(df, aes(x, cum.pct)) + geom_line()
ECDF是一个函数F(a)
,它等于观察值的权重(概率)之和,其中x<a
除以权重之和.
The ECDF is a function F(a)
equal to the sum of weights (probabilities) of observations where x<a
divided by the total sum of weights.
但是这里有一个更令人满意的选项,它只是修改了ggplot2 stat_ecdf的原始代码: https://github.com/NicolasWoloszko/stat_ecdf_weighted
But here is a more satisfying option that simply modifies the original code of the ggplot2 stat_ecdf: https://github.com/NicolasWoloszko/stat_ecdf_weighted
这篇关于R ggplot:加权CDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!