强制R将直方图绘制为概率(相对频率) [英] Force R to plot histogram as probability (relative frequency)

查看:175
本文介绍了强制R将直方图绘制为概率(相对频率)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将直方图绘制为pdf(概率)

I am having trouble plotting a histogram as a pdf (probability)

我希望所有片段的总和等于一个的面积,以便在数据集之间进行比较比较容易.出于某种原因,无论何时指定中断(默认值为4或任何可怕的值),它都不再希望将bin绘制为概率,而是将bin绘制为频率计数.

I want the sum of all the pieces to equal an area of one so it's easier to compare across datasets. For some reason, whenever I specify the breaks (the default of 4 or whatever is terrible), it no longer wants to plot bins as a probability and instead plots bins as a frequency count.

hist(data[,1], freq = FALSE, xlim = c(-1,1), breaks = 800)

该行应更改为什么?我需要概率分布和大量垃圾箱. (我有600万个数据点)

What should I change this line to? I need a probability distribution and a large number of bins. (I have 6 million data points)

这在R帮助中,但我不知道如何覆盖它:

This is in the R help, but I don't know how to override it:

频率逻辑;如果为TRUE,则直方图图形表示 频率,结果的计数成分;如果为假,则概率 绘制密度,成分密度(以便直方图具有 总面积为一).当且仅当中断是 等距(且未指定概率).

freq logical; if TRUE, the histogram graphic is a representation of frequencies, the counts component of the result; if FALSE, probability densities, component density, are plotted (so that the histogram has a total area of one). Defaults to TRUE if and only if breaks are equidistant (and probability is not specified).

谢谢

详细信息

嗯,所以我的情节超过了1,如果有概率的话,这很令人困惑.我现在看到它与纸槽宽度有何关系.我或多或少希望使每个垃圾箱都值1分,同时又要有很多垃圾箱.换句话说,除非箱高不直接位于1.0且所有其他箱均为0.0,否则箱高度不应高于1.0.就目前情况而言,我有一个垃圾桶,大约在15.0左右驼峰

hmm so my plot goes above 1 which is quite confusing if it's a probability. I see how it has to do with the bin width now. I more or less want to make every bin worth 1 point while still having a lot of bins. In other words, no bin height should be above 1.0 unless it is directly at 1.0 and all the other bins are 0.0. As it stands now, I have a bins that make a hump around 15.0

箱中高度以%points为单位 @Dwin:那我该如何绘制概率呢?我意识到由于x轴上的单位,取积分仍然会给我1.0,但这不是我想要的.假设我有100分,其中5分落入第一个垃圾箱,那么该垃圾箱的高度应为0.05.这就是我要的.我做错了吗,还有另一种方法吗?

edit: height by %points in bin @Dwin : So how do I plot the probability? I realize taking the integral will still give me 1.0 due to the units on the x axis, but this isn't what I want. Say I have 100 points and 5 of them fall into the first bin, then that bin should be at .05 height. This is what I want. Am I doing it wrong and there is another way this is done?

我知道我有几分.有没有办法将频率直方图中的每个bin数除以这个数字?

推荐答案

回答绘制概率而不是密度的请求:

To answer the request to plot probabilities rather than densities:

h <- hist(vec, breaks = 100, plot=FALSE)
h$counts=h$counts/sum(h$counts)
plot(h)

这篇关于强制R将直方图绘制为概率(相对频率)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆