# [R] 3-d kernel smooth by the "kde" function

David L Carlson dcarlson at tamu.edu
Thu Jul 19 17:58:47 CEST 2012

```No you are not correct. The kde function estimates the density of 1 to 6
dimensions. To visualize, this try plotting the density of just the data

plot(density(elevation\$data))
rug(elevation\$data)

The elevations are plotted along the x-axis and their density is plotted on
the y-axis. Where data points are close together, the plot is higher. If you
had two variables, kde2d() would plot their joint density in a 3d dimension
and you would view them with contour() or persp(). With three dimensions of
data, the kde() function plots the joint density of the three variables in a
fourth dimension which is represented in the 3d plot by shading to indicate
areas of greater point density.

To smooth the z variable (elevation) so that you can interpolate values in
between your observations you have several options:

1. Polynomial regression using x and y to predict z, creating a regular grid
of x and y to predict z and use that with contour to show the results.

2. Use loess() to fit a smooth surface using x and y to predict z.

3. Use kriging (e.g. krig.conv in package geoR).

http://cran.r-project.org/web/views/Spatial.html

The geoR package has kriging. See

http://www.leg.ufpr.br/geoR/

for examples.

Also look at the StatDA package, particularly the Vignette for that package,
"Tutorial to the package StatDA" which discusses smoothing techniques
and kriging:

http://cran.r-project.org/web/packages/StatDA/vignettes/StatDA.pdf

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Gary Dong
> Sent: Wednesday, July 18, 2012 9:15 PM
> To: r-help at r-project.org
> Subject: [R] 3-d kernel smooth by the "kde" function
>
> Dear R community,
>
> I'm having hard time to understand the kde function in "ks" package.
> Let me
> use a 3-dimensional kernel smooth example to explain my question using
> the
> elevation data in geoR.
>
> ### here is what I did ###
>
> library(ks)
> require(geoR)
> data(elevation)
> elev.df <- data.frame(x = elevation\$coords[,"x"], y =
> elevation\$coords[,"y"], z = elevation\$data)
> H<-Hpi(elev.df)
> elev.smt<-kde(elev.df,H=H)
> plot(elev.smt, drawpoints=TRUE)
>
> If I understand it correctly, with the kde function, I'm using the two
> coordinate variables x and y to estimate (or say smooth) elevation (z).
> Is
> this correct?
>
> With this kernel smooth, my goal is to identify peaks, which are
> defined as
> areas that have estimated elevations >=950. Can someone show me how to
> do
> this?
>
> Thanks!
>
>
> Best
> Gary
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help