创建加权值的直方图 [英] Create a histogram for weighted values

查看:121
本文介绍了创建加权值的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个向量(例如v<-runif(1000)),则可以绘制其直方图(由于v是均匀分布的样本,因此它或多或少看起来像一条水平线).

If I have a vector (e.g., v<-runif(1000)), I can plot its histogram (which will look, more or less, as a horizontal line because v is a sample from the uniform distribution).

但是,假设我有一个向量及其关联的权重(例如,除了v<-sort(runif(1000))之外的w<-seq(1,1000)).例如,这是table()在更大数据集上的结果.

However, suppose I have a vector and its associated weights (e.g., w<-seq(1,1000) in addition to v<-sort(runif(1000))). E.g., this is the result of table() on a much larger data set.

如何绘制新的直方图? (在此示例中,它看起来应该更像y=x行).

How do I plot the new histogram? (it should look more of less like the y=x line in this example).

我想我可以通过使用rep(hist(rep(v,w)))来反转table的效果,但是这个解决方案"看起来很丑陋且占用大量资源(创建大小为sum(w)的中间向量),并且仅支持整数权重.

I guess I could reverse the effects of table by using rep (hist(rep(v,w))) but this "solution" seems ugly and resource-heavy (creates an intermediate vector of size sum(w)), and it only supports integer weights.

推荐答案

软件包plotrix 具有函数weighted.hist可以满足您的需求:

Package plotrix has a function weighted.hist which does what you want:

w<-seq(1,1000)
v<-sort(runif(1000))
weighted.hist(v, w)

>

这篇关于创建加权值的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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