An unnormalised version of kernel density estimation where the weights are not required to sum to 1. The weights may be positive, negative or zero.
unnormdensity(x, ..., weights = NULL, defaults)
Object of class "density"
as described in
Numeric vector of data
Optional arguments passed to density.default
.
Arguments must be named.
`
Optional numeric vector of weights for the data. The default is equivalent to assuming a weight of 1 for each observation.
Optional, named list of arguments passed to
density.default
. These will be overridden by
arguments in ...
.
If weights
is not specified,
the default is to assign a weight \(w_i=1\) to each
observation \(x_i\).
This is not the same behaviour as in density.default
which
effectively assumes a weight of \(1/n\) for each observation \(x_i\)
where n=length(x)
.
Adrian Baddeley Adrian.Baddeley@curtin.edu.au and Rolf Turner rolfturner@posteo.net
This is an alternative to the standard R kernel density estimation function
density.default
.
The standard density.default
requires the weights
to be nonnegative numbers that add up to 1,
and returns a probability density (a function that integrates to 1).
This function unnormdensity
does not impose any requirement
on the weights
except that they be finite. Individual weights may be
positive, negative or zero. The result is a function that does not
necessarily integrate to 1 and may be negative. The result is
the convolution of the kernel \(k\) with the weighted data,
$$
f(x) = \sum_i w_i k(x- x_i)
$$
where \(x_i\) are the data points and \(w_i\) are the
weights.
The argument weights
should be a numeric vector of the same
length as x
, or a single numeric value. The default is to
assume a weight of 1 for each observation in x
.
The algorithm first selects the kernel bandwidth by
applying density.default
to the data
x
with normalised, positive weight vector
w = abs(weights)/sum(abs(weights))
and
extracting the selected bandwidth.
Then the result is computed by applying
applying density.default
to x
twice
using the normalised positive and negative parts of the weights.
Note that the arguments ...
must be passed by name,
i.e. in the form (name=value
). Arguments that do not match
an argument of density.default
will be ignored
silently.
d <- unnormdensity(1:3, weights=c(-1,0,1), bw=0.3)
if(interactive()) plot(d)
Run the code above in your browser using DataLab