Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a histogram for weighted values

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).

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.

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

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.

like image 322
sds Avatar asked Nov 07 '13 16:11

sds


People also ask

What is weighted histogram analysis?

The Weighted Histogram Analysis Method (WHAM) is a standard technique used to compute potentials of mean force (PMFs) from a set of umbrella sampling simulations. Here, we present a new WHAM implementation, termed g_wham, which is distributed freely with the GROMACS molecular simulation suite.

Is there a scale in histogram?

The histogram is a popular graphing tool. It is used to summarize discrete or continuous data that are measured on an interval scale.


1 Answers

library(ggplot2)
w <- seq(1,1000)
v <- sort(runif(1000))

foo <- data.frame(v, w)

ggplot(foo, aes(v, weight = w)) + geom_histogram()

enter image description here

like image 82
Jake Burkhead Avatar answered Oct 23 '22 05:10

Jake Burkhead