[R] kernel density in 2D
John.Fieberg at dnr.state.mn.us
Fri Aug 1 22:39:07 CEST 2003
Has anyone written code in R to do kernel density estimation in 2-D with
sphered data? In other words:
1. Transform the data to have unit covariance matrix.
2. Use one of the 2-D kernel estimators in R (e.g., kde2d, bkde2D,
sm.density...) to obtain fhat(x).
3. Transform back to the original scale.
I have data for which the two dimensions are highly correlated and my
thinking was that this approach might be worth exploring. I attempted
to write a function to do this (below), but think there must be
something wrong w/ the code I wrote - as things look fine until I do
the back-transformation. Any help would be greatly appreciated!
# Function applies the bkde2D kernel density function to the sphered
data # and then transforms back
# Inputs: x data matrix with 2 columns
# Sphere data using cholesky decomposition
# bn = S^(-1/2)
# mu = mean of data
# Estimate bandwidth using eq 4.14 in Silverman (1986)
est1<-bkde2D(newdata, bandwidth=c(hsp,hsp), gridsize=c(50,50))
# Translate grid back
# re-order the data for producing contour plots (if necessary)
More information about the R-help