kde: Kernel density estimation

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Calculates a kernel density estimate (univariate or multivariate).

Usage

1
2
kde(x, bandwidth = NULL, grid = TRUE, kernel = "biweight",
    product = TRUE, sort = TRUE)

Arguments

x

n x d matrix, data

bandwidth

scalar or 1 x d, bandwidth(s)

grid

logical or m x d matrix (where to calculate the density)

kernel

text string, see kernel.function

product

(if d>1) product or spherical kernel

sort

logical, TRUE if data need to be sorted

Details

The kernel density estimator is calculated as \frac{1}{n} ∑_i K_h(x_i - grid_{j}) for i=1,...,n and j=1,...,m. The default bandwidth vector is computed by Scott's rule of thumb (adapted to the chosen kernel function).

Value

List with components:

x

m x d matrix, where density has been calculated

y

m x 1 vector, density estimates

bandwidth

bandwidth vector used for calculation

rearrange

if sort=TRUE, index to rearrange x and y to its original order.

Author(s)

Marlene Mueller

See Also

kernel.function, convol, kreg

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  n <- 1000
  x <- rnorm(n)
  plot(kde(x), type="l")

  ## mixed normal data
  n <- 1000
  u <- runif(n)
  thresh <- 0.4
  x <- rnorm(n)*(u<thresh) +rnorm(n,mean=3)*(u>=thresh)
  h <- 1
  fh <- kde(x,bandwidth=h)
  plot(kde(x,bandwidth=h),type="l",lwd=2); rug(x)
  lines(kde(x,bandwidth=h*1.2),col="red")
  lines(kde(x,bandwidth=h*1.4),col="orange")
  lines(kde(x,bandwidth=h/1.2),col="blue")
  lines(kde(x,bandwidth=h/1.4),col="cyan")

  ## two-dimensional data
  n <- 1000
  u <- runif(n)
  thresh <- 0.4
  x1 <- rnorm(n)*(u<thresh) +rnorm(n,mean=3)*(u>=thresh)
  x2 <- rnorm(n)*(u<thresh) +rnorm(n,mean=9)*(u>=thresh)

  grid1 <- seq(min(x1),max(x1),length=20)  ## grid for x1
  grid2 <- seq(min(x2),max(x2),length=25)  ## grid for x2

  fh <- kde( cbind(x1,x2), grid=create.grid(list(grid1,grid2)) )
  o <- order(fh$x[,2],fh$x[,1])
  density <- (matrix(fh$y[o],length(grid1),length(grid2)))
  
  par(mfrow=c(2,2))
  plot(kde(x1),type="l",main="x1"); rug(x1)
  plot(kde(x2),type="l",main="x2"); rug(x2)
  persp(grid1,grid2,density,main="KDE",
        theta=30,phi=30,expand=0.5,col="lightblue",shade=0.5)
  contour(grid1,grid2,density, main="KDE Contours")
  points(x1,x2,col="red",pch=18,cex=0.5)
  par(mfrow=c(1,1))

Example output

Loading required package: AER
Loading required package: car
Loading required package: lmtest
Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Loading required package: sandwich
Loading required package: survival

gplm documentation built on May 2, 2019, 2:10 a.m.