View source: R/weightedStats.R
whist | R Documentation |
Computes the weighted histogram of a set of observations with a given set of weights.
whist(x, breaks, weights = NULL, method=c("C", "interpreted"))
x |
Numeric vector of observed values. |
breaks |
Vector of breakpoints for the histogram. |
weights |
Numeric vector of weights for the observed values. |
method |
Developer use only.
A character string specifying whether to use internal C code
( |
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.
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.
and \rolf
with thanks to Peter Dalgaard.
x <- rnorm(100)
b <- seq(-1,1,length=21)
w <- runif(100)
whist(x,b,w)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.