问题描述
如果我使用latticeExtra
包的ecdfplot()
函数,如何获取计算出的实际值,即与~x|g
输入相对应的y值?
If I use the ecdfplot()
function of the latticeExtra
package how do I get the actual values calculated i.e. the y-values which correspond to the ~x|g
input?
我一直在查看?ecdfplot
,但没有说明.对于通常的高级功能ecdf()
,它可与命令plot=FALSE
一起使用,但不适用于ecdfplot()
.
I've been looking at ?ecdfplot
but there's not discription to it. For the usual highlevel function ecdf()
it works with the command plot=FALSE
but this does not work for ecdfplot()
.
我要使用ecdfplot()
而不是ecdf()
的原因是我需要计算分组变量的ecdf()
值.我知道我也可以这样做,但我深信也有一条高速公路.
The reason I want to use ecdfplot()
rather than ecdf()
is that I need to calculate the ecdf()
values for a grouping variable. I know I could do this handish too but I'm quite convinced that there is a highroad too.
这里有个小例子
u <- rnorm(100,0,1)
mygroup <- c(rep("group1",50),rep("group2",50))
ecdfplot(~u, groups=mygroup)
我想提取给定每个组对应的x值的y值.
I would like to extract the y-values given each group for the corresponding x-values.
推荐答案
我知道您说过您不想使用ecdf
,但是在这种情况下,使用它很多而不是从ecdfplot
返回的网格对象中获取数据. (毕竟,这就是ecdfplot
的全部工作-它只是在后台进行的工作.)
I know you said you don't want to use ecdf
, but in this case it is much easier to use it than to get the data out of the trellis object that ecdfplot
returns. (After all, that's all that ecdfplot
is doing- it's just doing it behind the scenes).
在您的示例中,以下内容将为您每个ECDF的y值矩阵(其中x
是整个输入u
,尽管您可以选择其他输入):
In the case of your example, the following will get you a matrix of the y values (where x
is your entire input u
, though you could choose a different one) for each ECDF:
ecdfs = lapply(split(u, mygroup), ecdf)
ys = sapply(ecdfs, function(e) e(u))
# output:
# group1 group2
# [1,] 0.52 0.72
# [2,] 0.68 0.78
# [3,] 0.62 0.78
# [4,] 0.66 0.78
# [5,] 0.72 0.80
# [6,] 0.86 0.94
# [7,] 0.10 0.26
# [8,] 0.90 0.94
# ...
ETA:如果只希望每列对应于该列中的50个x值,则可以执行以下操作:
ETA: If you just want each column to correspond to the 50 x-values in that column, you could do:
ys = sapply(split(u, mygroup), function(g) ecdf(g)(g))
(请注意,如果每个组中的值数量不同,则最终将以列表形式而不是带有列的矩阵形式出现.)
(Note that if the number of values in each group aren't identical, this will end up as a list rather than a matrix with columns).
这篇关于如何从ecdfplot()中提取ecdf值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!