Computes the weighted histogram of a set of observations with a given set of weights.
whist(x, breaks, weights = NULL)
A numeric vector of length N-1
containing the
histogram values, where N = length(breaks)
.
The return value also has attributes "low"
and "high"
giving the total weight of all observations that are less than the lowest breakpoint, or greater than the highest breakpoint, respectively.
Numeric vector of observed values.
Vector of breakpoints for the histogram.
Numeric vector of weights for the observed values.
Adrian Baddeley Adrian.Baddeley@curtin.edu.au
and Rolf Turner r.turner@auckland.ac.nz
with thanks to Peter Dalgaard.
This low-level function computes (but does not plot) the weighted
histogram of a vector of observations x
using a given
vector of weights
.
The arguments x
and weights
should be numeric vectors of
equal length. They may include NA
or infinite values.
The argument breaks
should be a numeric vector whose entries
are strictly increasing. These values define the boundaries between the
successive histogram cells.
The breaks do not have to span the range
of the observations.
There are N-1
histogram cells, where N = length(breaks)
.
An observation x[i]
falls in the j
th cell if
breaks[j] <= x[i] < breaks[j+1]
(for j < N-1
)
or
breaks[j] <= x[i] <= breaks[j+1]
(for j = N-1
).
The weighted histogram value h[j]
for the j
th cell is
the sum of weights[i]
for all observations x[i]
that
fall in the cell.
Note that, in contrast to the function hist
,
the function whist
does not require the breakpoints to span the
range of the observations x
. Values of x
that fall outside the
range of breaks
are handled separately; their total weight
is returned as an attribute of the histogram.
x <- rnorm(100)
b <- seq(-1,1,length=21)
w <- runif(100)
whist(x,b,w)
Run the code above in your browser using DataLab