【问题标题】:How to find the frequency of second histogram based on quantile of first如何根据第一个分位数找到第二个直方图的频率
【发布时间】:2019-11-16 15:52:36
【问题描述】:

我有两组数据。在第一组中,我可以计算第 25 个百分位数并输出 x 值。然后,我想将该 x 值用于第二个数据集,并确定同一点的频率和百分位数。

例如,这里是一些基于随机变量的代码(实际数据不是随机的)

data1 = rnorm(10000, mean=8, sd=1.3)
data2 = rnorm(10000, mean=4, sd=1.0)

#plot data1 histogram with 40 bins
hist(data1, breaks=40, col="red", xlim=c(2,14), ylim=c(0,800),
     main="Gaussian deviates :  mean=8, sigma=1.3", col.main="blue")
segments(quantile(data1,0.25), 0, quantile(data1,0.25), 600, col="green", lwd=4, lty=1)

#plot data2 histogram with 40 bins
hist(data2, breaks=40, col="red", xlim=c(2,14), ylim=c(0,900),
     main="Gaussian deviates :  mean=4, sigma=1.0", col.main="blue")
print(quantile(data1,0.25))

【问题讨论】:

    标签: r random quantile


    【解决方案1】:

    您可以对第二组数据使用ecdf() 函数来创建经验累积分布函数。然后,您可以输入值并查看它们在经验分布中的位置。所以在这种情况下,data1 的第 25 个百分位是data299.93 百分位。我不太清楚“频率”指的是什么。

    set.seed(100)
    data1 = rnorm(10000, mean=8, sd=1.3)
    data2 = rnorm(10000, mean=4, sd=1.0)
    
    #plot data1 histogram with 40 bins
    hist(data1, breaks=40, col="red", xlim=c(2,14), ylim=c(0,800),
         main="Gaussian deviates :  mean=8, sigma=1.3", col.main="blue")
    segments(quantile(data1,0.25), 0, quantile(data1,0.25), 600, col="green", lwd=4, lty=1)
    
    
    
    q1 <- quantile(data1, 0.25)
    #plot data2 histogram with 40 bins
    hist(data2, breaks=40, col="red", xlim=c(2,14), ylim=c(0,900),
         main="Gaussian deviates :  mean=4, sigma=1.0", col.main="blue")
    
    
    print(quantile(data1,0.25))
    #>      25% 
    #> 7.133474
    ecdf(data2)(q1)
    #> [1] 0.9993
    

    【讨论】:

    • 谢谢。这将是频率分布的 y 值。
    • 您是指包含 7.133474 的 bin 的 y 值吗? 7.133474 之前发生的观察次数?
    • 我相信 OP 希望将找到的百分位点的密度表示为频率。
    【解决方案2】:

    首先,让我们从第一个分布中找到25th 百分位数:

    q25 <- quantile(data1,0.25)
    

    其次,让我们为第二个数据集创建一个经验累积分布函数:

    dist2 <- ecdf(data2)
    

    第三,让我们为第二个数据集创建一个近似分布函数:

    df <- approxfun(density(data2))
    

    所以,第二个分布的百分位数是:

    dist2.percentile <- dist2(q25)
    
    > dist2.percentile
    [1] 0.9986
    

    dist2.percentile 上的密度为:

    dist2.density <- df(q25)
    > dist2.density
    [1] 0.003380966
    

    大致频率为:

    dist2.frequency <- dist2.density * 10000
    > dist2.frequency
    [1] 33.80966
    

    最后检查近似密度函数下的面积是否足够准确,约等于1。

    > sum(df(quantile(dist2)[1]:quantile(dist2)[5]))
    [1] 1.014089
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多