【问题标题】:Non-uniform bins in Histogram in RR中直方图中的非均匀箱
【发布时间】:2021-02-22 10:15:47
【问题描述】:

我想将数据集(数值向量)划分为一些区间,并生成频率直方图以查看哪些值属于每个区间。如果我使用hist(dataset, breaks = 10),这会将数据集分成 10 个相等的间隔。相反,我想将数据集分成(例如)10 个 bin,每个区间至少包含 5% 的数据点。

【问题讨论】:

    标签: r histogram


    【解决方案1】:

    您可以使用quantile() 函数来定义等大小的箱子。

    这是一个关于指数分布数据的示例:

    # Seed for the random number generation (for repeatability)
    seed = 1313
    
    # Sample size
    N = 150
    
    # Size of each bin (as proportion of N)
    binsize = 0.05
    
    # Sample data
    x = rexp(N)
    
    # Regular histogram (equal-width bins)
    hist(x, breaks=20, freq=TRUE, main="Histogram on 20 equal-width bins", col="red")
    

    # Quantiles of size `binsize`
    x.quantiles = quantile(x, probs=seq(0, 1, binsize))
    
    # Histogram on the equal-size breaks
    hist(x, breaks=x.quantiles, freq=TRUE, main=paste("Approx. equal-size-bin 'Histogram' (bin-size=", binsize*100, "% of ", N, ")", sep=""), col="cyan")
    

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多