Learn R Programming

ks (version 1.7.0)

plot.kde: Kernel density estimate plot for 1- to 3-dimensional data

Description

Kernel density estimate plot for 1- to 3-dimensional data.

Usage

## S3 method for class 'kde':
plot(x, ...)

Arguments

x
an object of class kde (output from kde function)
...
other graphics parameters

Value

  • Plot of 1-d and 2-d kernel density estimates are sent to graphics window. Plot for 3-d is generated by the misc3d and rgl libraries and is sent to RGL window.

Details

The function headers for the different dimensional data are ## univariate plot(x, xlab, ylab="Density function", add=FALSE, drawpoints=FALSE, ptcol="blue", col="black", jitter=TRUE, ...)

## bivariate plot(x, display="slice", cont=c(25,50,75), abs.cont, approx.cont=FALSE, xlab, ylab, zlab="Density function", add=FALSE, drawpoints=FALSE, drawlabels=TRUE, theta=-30, phi=40, d=4, ptcol="blue", col="black", ...)

## trivariate plot(x, cont=c(25,50,75), abs.cont, approx.cont=FALSE, colors, add=FALSE, drawpoints=FALSE, alpha, alphavec, xlab, ylab, zlab, size=3, ptcol="blue", ...) The arguments are ll{ display type of display, "slice" for contour plot, "persp" for perspective plot, "image" for image plot, "filled.contour" for filled contour plot (1st form), "filled.contour2" (2nd form) (2-d plot) cont vector of percentages for contour level curves abs.cont vector of absolute density estimate heights for contour level curves approx.cont flag to compute approximate contour levels. Default is FALSE. ptcol plotting colour for data points col plotting colour for density estimate (1-d, 2-d plot) colors vector of colours for each contour (3-d plot) jitter flag to jitter rug plot (1-d plot). Default is TRUE. xlab,ylab,zlab axes labels add flag to add to current plot. Default is FALSE. theta,phi,d graphics parameters for perspective plots (2-d plot) drawpoints flag to draw data points on density estimate. Default is FALSE. drawlabels flag to draw contour labels (2-d plot). Default is TRUE. alpha transparency value of plotting symbol (3-d plot) alphavec vector of transparency values for contours (3-d plot) size size of plotting symbol (3-d plot) } The 1-d plot is a standard plot of a 1-d curve. If drawpoints=TRUE then a rug plot is added. There are different types of plotting displays for 2-d data available, controlled by the display parameter. (a) If display="slice" then a slice/contour plot is generated using contour. (b) If display is "filled.contour" or "filled.contour2" then a filled contour plot is generated. The default contours are at 25%, 50%, 75% or cont=c(25,50,75) which are upper percentages of highest density regions. See below for alternative contour levels. (c) display="persp" then a perspective/wire-frame plot is generated. The default z-axis limits zlim are the default from the usual persp command. (d) If display="image" then an image plot is generated. Default colours are the default from the usual image command.

For 3-dimensional data, the interactive plot is a series of nested 3-d contours. The default contours are cont=c(25,50,75). See below for alternative contour levels. The default colors are heat.colors and the default opacity alphavec ranges from 0.1 to 0.5.

To specify contours, either one of cont or abs.cont is required. cont specifies upper percentages which correspond to probability contour regions. If abs.cont is set to particular values, then contours at these levels are drawn. This second option is useful for plotting multiple density estimates with common contour levels. See contourLevels for details on computing contour levels. If approx=FALSE, then the exact KDE at x is computed. Otherwise the exact KDE is replaced by the KDE at the nearest grid point. This can dramatically reduce computation time for large data sets.

See Also

kde

Examples

Run this code
## univariate example
x <- rnorm.mixt(n=100, mus=1, sigmas=1, props=1)
fhat <- kde(x=x, h=hpi(x))  
plot(fhat)

## bivariate example
data(unicef)
H.scv <- Hscv(x=unicef)
fhat <- kde(x=unicef, H=H.scv, compute.cont=TRUE)

plot(fhat, drawpoints=TRUE, drawlabels=FALSE, col=3, lwd=3, cex=0.1)
plot(fhat, display="persp", border=NA, col="grey96", shade=0.75)
plot(fhat, display="image", col=rev(heat.colors(100)))
plot(fhat, display="filled.contour2", cont=seq(10,90,by=10))

## pair of densities with same absolute contour levels
x <- rmvnorm.mixt(n=100, mus=c(0,0), Sigmas=diag(2), props=1)
Hx <- Hpi(x)
fhatx <- kde(x=x, H=Hx, xmin=c(-4,-4), xmax=c(4,4)) 
y <- rmvnorm.mixt(n=100, mus=c(0.5,0.5), Sigmas=0.5*diag(2), props=1)
Hy <- Hpi(y)
fhaty <- kde(x=y, H=Hy, xmin=c(-4,-4), xmax=c(4,4))
lev <- contourLevels(fhatx, prob=c(0.25, 0.5, 0.75))
plot(fhatx, abs.cont=lev)
plot(fhaty, abs.cont=lev, col=3, add=TRUE) 

## large sample from bivariate normal 
x <- rmvnorm.mixt(5000, c(0,0), invvech(c(1, 0.8, 1)))    
H <- Hpi(x, binned=TRUE)
fhat <- kde(x, H=H, compute.cont=TRUE, approx.cont=TRUE)
plotmixt(mus=c(0,0), Sigmas=invvech(c(1, 0.8, 1)), props=1)      
plot(fhat, drawlabels=FALSE, add=TRUE, col=2)  

## trivariate example
library(MASS)
x <- iris[,1:3]
H.pi <- Hpi(x, pilot="samse")
fhat <- kde(x, H=H.pi, compute.cont=TRUE)  
plot(fhat, axes=FALSE, box=FALSE, drawpoints=TRUE); axes3d(c('x','y','z'))

Run the code above in your browser using DataLab