kde                 package:KernGPLM                 R Documentation

_K_e_r_n_e_l _d_e_n_s_i_t_y _e_s_t_i_m_a_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Calculates a kernel density estimate (univariate or multivariate).

_U_s_a_g_e:

     kde(x, bandwidth = NULL, grid = TRUE, p = 2, q = 2,
         product = TRUE, sort = TRUE)

_A_r_g_u_m_e_n_t_s:

       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)

       p: integer or text, see 'kernel.function'

       q: integer, see 'kernel.function'

 product: (if d>1) product or spherical kernel

    sort: sort the data, necessary to use the DLL code

_D_e_t_a_i_l_s:

     The kernel density estimator is calculated as frac{1}{n} sum_i
     K_h(x_i - grid_{j}) for i=1,...,n and j=1,...,m.

_V_a_l_u_e:

     List with components: 

       x: m x d matrix, where density has been calculated

       y: m x 1 vector, density estimates

bandwidth: bandwidth used for calculation

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

_A_u_t_h_o_r(_s):

     Marlene Mueller

_S_e_e _A_l_s_o:

     'kernel.function', 'convol', 'kreg'

_E_x_a_m_p_l_e_s:

       n <- 1000
       x <- rnorm(n)
       plot(kde(x))

       ## mixed normals
       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
       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)
       grid2 <- seq(min(x2),max(x2),length=25)
       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))

