# [R] bivariate kernel density estimates at point locations (rather than at grid locations)

Thu Dec 22 11:00:44 CET 2005

On Wed, 21 Dec 2005 12:21:54 -0500, Strickland, Matthew wrote:

SM>

Dr. Strickland,

your message was directed to the whole r-help list with no CC to
myself, and sometimes I do not have the chance to browse the
r-help list for weeks..

SM> Thank you for your quick response to my question that I posted on
SM> the r-help board about bivariate kernel density estimates.  I have
SM> been using your sm package the past few days and have encountered
SM> a problem with estimating the density for only 1 point. I am using
SM> R 2.2.0, sm version 2.1-0 on a Windows machine.  Example code
SM> below:
SM>
SM> #The code below creates 3 point locations
SM>
SM> x.locs = c(74, 75, 77)
SM> y.locs = c(64, 63, 61)
SM> points = cbind(x.locs, y.locs)
SM>
SM> #If I send this data into sm.density everything works fine.
SM>
SM> dens = sm.density(points, h=c(1, 1))
SM>
SM> #However, if I only wish to send 1 point location to sm.density,
SM> #i.e.,
SM>
SM> points.2 =points[1,]
SM> dens.2 = sm.density(points.2, h=c(1, 1))
SM>
SM> R returns to me the error:length(h) != 1

formally, the error is due to this

R> is.vector(points.2)
[1] TRUE

sm.density receives a vector of length 2, and it  works for that
case: estimation of a one-dimensional density from which you
supplied two data values. Then it finds a two-dimensional h
and there it complains.

On the statistical side, I cannot follow the logic of estimating
nonparametrically a density function on the basis of only one
(supposed bivariate) observation.

SM>
SM> It appears to me that sm.density thinks that my 1 point is a
SM> 1-dimensional location rather than a 2-dimensional location, and I
SM> am getting an error when I request a bivariate kernel.  Do you
SM> have any suggestions?
SM>

I am not sure to grasp what you have in mind; is it perhaps that you
want the following?

dens.2 = sm.density(points, h=c(1, 1), eval.points=points, eval.grid=FALSE)
print(dens.2\$estimate[1])

best regards,

SM> Best,
SM> Matt
SM>
SM>
SM> -----Original Message-----
SM> From: Adelchi Azzalini [mailto:azzalini at stat.unipd.it]
SM> Sent: Friday, December 16, 2005 2:42 AM
SM> To: Strickland, Matthew
SM> Cc: r-help at stat.math.ethz.ch; adrian at stats.gla.ac.uk
SM> Subject: Re: [R] bivariate kernel density estimates at point
SM> locations (rather than at grid locations)
SM>
SM> On Thu, 15 Dec 2005 14:21:17 -0500, Strickland, Matthew wrote:
SM>
SM> SM> Hi,
SM> SM>
SM> SM> My data consists of a set of point locations (x,y).
SM> SM>
SM> SM> I would like to know if there is a procedure for bivariate
SM> SM> kernel  density estimation in R that returns the density
SM> SM> estimates at the  observed point locations rather than at grid
SM> SM> locations. I have  looked at a number of different routines
SM> SM> and they all seem to return  estimates at grid locations.
SM> SM>
SM>
SM> One option is to use (from package sm),
SM>   sm.density(xy, eval.points=xy, eval.grid=FALSE) where xy in a
SM>   (n\times 2) matrix.
SM>
SM> Best wishes,
SM>
SM> --
SM> Adelchi Azzalini  <azzalini at stat.unipd.it> Dipart.Scienze
SM> Statistiche, Università di Padova, Italia tel. +39 049 8274147,
SM> http://azzalini.stat.unipd.it/
SM>
SM> ______________________________________________
SM> R-help at stat.math.ethz.ch mailing list
SM> https://stat.ethz.ch/mailman/listinfo/r-help