Strickland, Matthew
2005-Dec-15 19:21 UTC
[R] bivariate kernel density estimates at point locations (rather than at grid locations)
Hi, My data consists of a set of point locations (x,y). I would like to know if there is a procedure for bivariate kernel density estimation in R that returns the density estimates at the observed point locations rather than at grid locations. I have looked at a number of different routines and they all seem to return estimates at grid locations. Any type of kernel is fine (i.e., Gaussian, Quartic, etc). Thank you for your help! Matt Strickland U.S. Centers for Disease Control and Prevention
Adelchi Azzalini
2005-Dec-16 07:41 UTC
[R] bivariate kernel density estimates at point locations (rather than at grid locations)
On Thu, 15 Dec 2005 14:21:17 -0500, Strickland, Matthew wrote: SM> Hi, SM> SM> My data consists of a set of point locations (x,y). SM> SM> I would like to know if there is a procedure for bivariate kernel SM> density estimation in R that returns the density estimates at the SM> observed point locations rather than at grid locations. I have SM> looked at a number of different routines and they all seem to SM> return estimates at grid locations. SM> One option is to use (from package sm), sm.density(xy, eval.points=xy, eval.grid=FALSE) where xy in a (n\times 2) matrix. Best wishes, Adelchi Azzalini -- Adelchi Azzalini <azzalini at stat.unipd.it> Dipart.Scienze Statistiche, Universit?? di Padova, Italia tel. +39 049 8274147, http://azzalini.stat.unipd.it/
Strickland, Matthew
2005-Dec-21 17:21 UTC
[R] bivariate kernel density estimates at point locations (rather than at grid locations)
Hello Dr. Adelchi Azzalini, Thank you for your quick response to my question that I posted on the r-help board about bivariate kernel density estimates. I have been using your sm package the past few days and have encountered a problem with estimating the density for only 1 point. I am using R 2.2.0, sm version 2.1-0 on a Windows machine. Example code below: #The code below creates 3 point locations x.locs = c(74, 75, 77) y.locs = c(64, 63, 61) points = cbind(x.locs, y.locs) #If I send this data into sm.density everything works fine. dens = sm.density(points, h=c(1, 1)) #However, if I only wish to send 1 point location to sm.density, i.e., points.2 =points[1,] dens.2 = sm.density(points.2, h=c(1, 1)) R returns to me the error:length(h) != 1 It appears to me that sm.density thinks that my 1 point is a 1-dimensional location rather than a 2-dimensional location, and I am getting an error when I request a bivariate kernel. Do you have any suggestions? Best, Matt -----Original Message----- From: Adelchi Azzalini [mailto:azzalini at stat.unipd.it] Sent: Friday, December 16, 2005 2:42 AM To: Strickland, Matthew Cc: r-help at stat.math.ethz.ch; adrian at stats.gla.ac.uk Subject: Re: [R] bivariate kernel density estimates at point locations (rather than at grid locations) On Thu, 15 Dec 2005 14:21:17 -0500, Strickland, Matthew wrote: SM> Hi, SM> SM> My data consists of a set of point locations (x,y). SM> SM> I would like to know if there is a procedure for bivariate kernel SM> density estimation in R that returns the density estimates at the SM> observed point locations rather than at grid locations. I have SM> looked at a number of different routines and they all seem to return SM> estimates at grid locations. SM> One option is to use (from package sm), sm.density(xy, eval.points=xy, eval.grid=FALSE) where xy in a (n\times 2) matrix. Best wishes, Adelchi Azzalini -- Adelchi Azzalini <azzalini at stat.unipd.it> Dipart.Scienze Statistiche, Universit?? di Padova, Italia tel. +39 049 8274147, http://azzalini.stat.unipd.it/
Adelchi Azzalini
2005-Dec-22 10:00 UTC
[R] bivariate kernel density estimates at point locations (rather than at grid locations)
On Wed, 21 Dec 2005 12:21:54 -0500, Strickland, Matthew wrote: SM> Hello Dr. Adelchi Azzalini, 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, Adelchi 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> Adelchi Azzalini 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 SM> PLEASE do read the posting guide! SM> http://www.R-project.org/posting-guide.html SM>