[R] bivariate weighted kernel density estimator

Erich Neuwirth erich.neuwirth at univie.ac.at
Mon Apr 24 23:02:56 CEST 2006


Dear Roger,
thanks for the suggestion.
That is the solution, just modifying kde2d.
I did it slightly differently.
Since I have up to 200000 points,
diag(Z) from your code becomes too large.
But since t(matrix(dnorm(ay),n,nx)) only needs to be
multiplied with the weights rowwise and
* applied to vectors repeats the shorter vector cyclically,
Z * t(matrix(dnorm(ay)))
does the same thing as
diag(Z) %*% t(matrix(dnorm(ay),n,nx))

and does not need too much memory.

I also have to add an excuse:
In the original posting I stated that I need WEIGHTED kernel
density estimators in the subject,
but did not mention weighted in the text.
That was imprecise and probably therefore I mislead some list
participants.

Erich


Roger Bivand wrote:
> kde2d.G is just kde2d with two changes - it takes the grid from the GRASS
> region, and it allows weights in the Z argument. Please have a look at
the
> code and see if you can't simply retro-fit it to kde2d:
>
>     if (!is.null(Z)) {
>         if (length(Z) != nx)
>             stop("Data vectors must be the same length")
>         z1 <- matrix(dnorm(ax), n, nx) %*% diag(Z) %*%
t(matrix(dnorm(ay),
>             n, nx))/(nx * h[1] * h[2])
>         z <- z1/z
>     }
>
> This was put into the function to make a very crude kernel density
> interpolator with the grid cell values scaled in the units of the Z
> variable.
>

-- 
Erich Neuwirth, University of Vienna
Faculty of Computer Science
Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-39464 Fax: +43-1-4277-39459




More information about the R-help mailing list