我已经阅读了其他文章(例如here),以获得分位数的“反向”-即,获得与一系列值中的某个值相对应的百分位数。
但是,对于相同的数据序列,答案并不能为我提供与分位数相同的值。
我还研究了分位数提供9种不同的算法来计算百分位数。
所以我的问题是:是否有可靠的方法来获得分位数函数的反函数?ecdf没有采用“类型”参数,因此似乎无法确保它们使用相同的方法。
可重现的示例:
# Simple data
x = 0:10
pcntile = 0.5
# Get value corresponding to a percentile using quantile
(pcntile_value <- quantile(x, pcntile))
# 50%
# 5 # returns 5 as expected for 50% percentile
# Get percentile corresponding to a value using ecdf function
(pcntile_rev <- ecdf(x)(5))
# [1] 0.5454545 #returns 54.54% as the percentile for the value 5
# Not the same answer as quantile produces
Run Code Online (Sandbox Code Playgroud) 我有几个累积的经验密度函数,我想在彼此之上绘制,以说明两条曲线的差异.正如之前的一个问题所指出的那样,绘制ECDF的功能很简单plot(Ecdf())
.当我阅读精细的手册页时,我确定我可以使用以下内容绘制多个ECDF:
require( Hmisc )
set.seed(3)
g <- c(rep(1, 20), rep(2, 20))
Ecdf(c( rnorm(20), rnorm(20)), group=g)
Run Code Online (Sandbox Code Playgroud)
然而,我的曲线有时会重叠一点,很难说哪个是哪个,就像上面产生这个图的例子一样:
我真的想让这两个CDF的颜色不同.但是,我无法弄清楚如何做到这一点.有小费吗?
有没有一种简单快速的方法来获得R中整数向量中出现的每个整数的频率?
以下是我到目前为止的尝试:
x <- floor(runif(1000000)*1000)
print('*** using TABLE:')
system.time(as.data.frame(table(x)))
print('*** using HIST:')
system.time(hist(x,breaks=min(x):(max(x)+1),plot=FALSE,right=FALSE))
print('*** using SORT')
system.time({cdf<-cbind(sort(x),seq_along(x)); cdf<-cdf[!duplicated(cdf[,1]),2]; c(cdf[-1],length(x)+1)-cdf})
print('*** using ECDF')
system.time({i<-min(x):max(x); cdf<-ecdf(x)(i)*length(x); cdf-c(0,cdf[-length(i)])})
print('*** counting in loop')
system.time({h<-rep(0,max(x)+1);for(i in seq_along(x)){h[x[i]]<-h[x[i]]+1}; h})
#print('*** vectorized summation') #This uses too much memory if x is large
#system.time(colSums(matrix(rbind(min(x):max(x))[rep(1,length(x)),]==x,ncol=max(x)-min(x)+1)))
#Note: There are some fail cases in some of the above methods that need patching if, for example, there is a chance that some integer bins are unoccupied
Run Code Online (Sandbox Code Playgroud)
以下是结果:
[1] "*** using …
Run Code Online (Sandbox Code Playgroud) 我知道statsmodels.tools.tools.ECDF,但由于计算一个empricial累积分布函数(ECDF)是非常简单的,我想最小化项目中的依赖项,我想手动编码.
在给定的list()
/中np.array()
Pandas.Series
,每个元素的ECDF可以按维基百科中给出的方式计算:
我有Pandas DataFrame,dfser
下面我想获得该values
列的ecdf .我也给出了两个单线解决方案.
有更快的方法吗?速度在我的应用中很重要.
# Note that in my case indices are unique identifiers so I cannot reset them.
import numpy as np
import pandas as pd
# all indices are unique, but there may be duplicate measurement values (that belong to different indices).
dfser = pd.DataFrame({'group':['a','b','b','a','d','c','e','e','c','a','b','d','d','c','d','e','e','a'],
'values':[2.01899E-06, 1.12186E-07, 8.97467E-07, 2.91257E-06, 1.93733E-05,
0.00017889, 0.000120963, 4.27643E-07, 3.33614E-07, 2.08352E-12,
1.39478E-05, 4.28255E-08, 9.7619E-06, 8.51787E-09, 1.28344E-09,
3.5063E-05, 0.01732035,2.08352E-12]}, …
Run Code Online (Sandbox Code Playgroud) 我想在同一个图中绘制多个变量的 CDF 图。变量的长度不同。为了简化细节,我使用以下示例代码:
library("ggplot2")
a1 <- rnorm(1000, 0, 3)
a2 <- rnorm(1000, 1, 4)
a3 <- rnorm(800, 2, 3)
df <- data.frame(x = c(a1, a2, a3),ggg = gl(3, 1000))
ggplot(df, aes(x, colour = ggg)) + stat_ecdf()+ coord_cartesian(xlim = c(0, 3)) + scale_colour_hue(name="my legend", labels=c('AAA','BBB', 'CCC'))
Run Code Online (Sandbox Code Playgroud)
可以看到,a3的长度是800,与a1、a2不同。当我运行代码时,它显示:
> df <- data.frame(x = c(a1, a2, a3),ggg = gl(3, 1000))
Error in data.frame(x = c(a1, a2, a3), ggg = gl(3, 1000)) :
arguments imply differing number of rows: 2800, 3000
> ggplot(df, aes(x, …
Run Code Online (Sandbox Code Playgroud) 我试图用stat_ecdf()
累积成功作为预测模型创建的排名得分的函数.
#libraries
require(ggplot2)
require(scales)
# fake data for reproducibility
set.seed(123)
n <- 200
df <- data.frame(model_score= rexp(n=n,rate=1:n),
obs_set= sample(c("training","validation"),n,replace=TRUE))
df$model_rank <- rank(df$model_score)/n
df$target_outcome <- rbinom(n,1,1-df$model_rank)
# Plot Gain Chart using stat_ecdf()
ggplot(subset(df,target_outcome==1),aes(x = model_rank)) +
stat_ecdf(aes(colour = obs_set), size=1) +
scale_x_continuous(limits=c(0,1), labels=percent,breaks=seq(0,1,.1)) +
xlab("Model Percentile") + ylab("Percent of Target Outcome") +
scale_y_continuous(limits=c(0,1), labels=percent) +
geom_segment(aes(x=0,y=0,xend=1,yend=1),
colour = "gray", linetype="longdash", size=1) +
ggtitle("Gain Chart")
Run Code Online (Sandbox Code Playgroud)
我想做的就是强制ECDF从(0,0)开始,到(1,1)结束,这样曲线的开头或结尾就没有间隙了.如果可能的话,我想在语法中做到这一点ggplot2
,但我会满足于一个聪明的解决方法.
@Henrik这不是这个问题的重复,因为我已经用scale_x_
和定义了我的限制_y_continuous()
,并且添加expand_limits()
没有做任何事情.它不是PLOT的起源,而是需要修复的stat_ecdf()的端点.
我喜欢ggplot2 包的stat_ecdf()功能部分,我发现它对于探索数据系列非常有用。然而,这只是视觉上的,我想知道是否可行 - 如果可行,如何 - 获取关联的表?
请查看以下可重现的示例
p <- ggplot(iris, aes_string(x = "Sepal.Length")) + stat_ecdf() # building of the cumulated chart
p
attributes(p) # chart attributes
p$data # data is iris dataset, not the serie used for displaying the chart
Run Code Online (Sandbox Code Playgroud)
我有一组难以可视化的数据,但我认为添加了几个点和线的 ECDF 可以解决问题。我能够按照我想要的方式绘制事物;我的问题是正确着色。
我有以下代码,它将所有正确的线和点放在图上,但现在我想正确地为所有内容着色和标记。我仔细阅读了多篇文章并尝试了一百件事,但都做对了。我需要以不同的方式格式化我的数据吗?
我对传奇的看法是这样的:
生成示例图的代码在这里:
require(ggplot2)
require(reshape2)
s.a = rnorm(100)*100
s.b = rnorm(100)*100+50
d.a = -35
d.b = 20
sdata = data.frame(cbind(s.a,s.b))
ddata = data.frame(cbind(d.a,d.b))
sdata.m = melt(sdata)
ddata.m = melt(ddata)
ggplot(sdata.m, aes(x=value, color=variable)) +
geom_vline(data=ddata.m,
aes(xintercept = value,
color=variable),
linetype = 2,
size=2) +
stat_ecdf(size=1)+
labs(title = 'plotTitle',
color='colorLegendTitle') +
xlab('xLabel') +
ylab('yLabel')+
theme_bw(30) +
theme(
legend.position=c(.8, .2),
legend.box="horizontal",
text=element_text(family="Times"),
legend.key.size = unit(1,"cm")) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ecdf,但是我不确定自己是否做对了。我的最终目的是找到对应于特定值的分位数。举个例子:
sample_set <- c(20, 40, 60, 80, 100)
# Now I want to get the 0.75 quantile:
quantile(x = sample_set, probs = 0.75)
#result:
75%
80
# Let's use ecdf
ecdf(x = sample_set) (80)
#result
0.8
Run Code Online (Sandbox Code Playgroud)
为什么会有这种差异?我是在犯一些琐碎的错误,还是取决于分位数的计算方式?
谢谢,马克斯
我正在使用ggplot和stat_ecdf函数创建频率图.我想将Y值添加到图表中以获取特定的X值,但是无法弄清楚如何.geom_point或geom_text似乎是可能的选项,但是当stat_ecdf自动计算Y时,我不知道如何在geom_point/text映射中调用该值.
我的初始情节的示例代码是:
x = as.data.frame(rnorm(100))
ggplot(x, aes(x)) +
stat_ecdf()
Run Code Online (Sandbox Code Playgroud)
现在我将如何在此处添加特定的yx点,例如x = -1处的y值.