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

From: Adelchi Azzalini <>
Date: Thu 22 Dec 2005 - 21:00:44 EST

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

SM> Hello Dr. Adelchi Azzalini,

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> #The code below creates 3 point locations
SM> x.locs = c(74, 75, 77)
SM> y.locs = c(64, 63, 61)
SM> points = cbind(x.locs, y.locs)
SM> #If I send this data into sm.density everything works fine.
SM> dens = sm.density(points, h=c(1, 1))
SM> #However, if I only wish to send 1 point location to sm.density,
SM> #i.e.,
SM> points.2 =points[1,]
SM> dens.2 = sm.density(points.2, h=c(1, 1)) 
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> 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?  

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> -----Original Message-----
SM> From: Adelchi Azzalini [] 
SM> Sent: Friday, December 16, 2005 2:42 AM
SM> To: Strickland, Matthew
SM> Cc:;
SM> Subject: Re: [R] bivariate kernel density estimates at point
SM> locations (rather than at grid locations)
SM> On Thu, 15 Dec 2005 14:21:17 -0500, Strickland, Matthew wrote:
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> 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> Best wishes,
SM> Adelchi Azzalini
SM> --
SM> Adelchi Azzalini  <> Dipart.Scienze
SM> Statistiche, UniversitÓ di Padova, Italia tel. +39 049 8274147, 
SM> ______________________________________________
SM> mailing list
SM> PLEASE do read the posting guide!

______________________________________________ mailing list PLEASE do read the posting guide! Received on Thu Dec 22 21:06:12 2005

This archive was generated by hypermail 2.1.8 : Thu 22 Dec 2005 - 23:42:25 EST