Library: | smoother |
See also: | denbwcrit denbbwcrit denbwsel denrot denxest Rdenxest |
Quantlet: | denxbwcrit | |
Description: | an exact variant of denbwcrit. All kernel estimates of the integrated squared densities or density derivatives are computed exactly using Rdenxest. |
Usage: | {hopt, ch} = denbwcrit(crit, x ,h {,K}) | |
Input: | ||
crit | string, criterion for bandwidth selection: "lscv" = Least Squares Cross Validation, "bcv" = Biased Cross Validation, "scv" = Smoothed Cross Validation, "jmp" = Jones, Marron and Park Cross Validation, "pm" = Park and Marron Plug-in, "sj" = Sheather and Jones Plug-in. | |
x | n x 1 vector, the data | |
h | p x 1 vector, bandwidth grid | |
K | optional string, determining the kernel function which is used; default = "gau". | |
Output: | ||
hopt | scalar, optimal bandwidth. (If negative in case of "pm" or "sj", denbwcrit needs to be run again with different h range.) | |
ch | p x 2 vector, the criterion function for h values. |
- Internally, the Gaussian kernel is used for all computations. If a different kernel function K is given as input, the asymptotically equivalent bandwidth for K is calculated via the canonical kernel approach.
- The optimal bandwidth for "lscv", "bcv", "scv" and "jmp" is the global minimum of the criterion function for the given vector of bandwidths.
- The optimal bandwidth for "sj" and "pm" is a zero of the linearly interpolated criterion function for the given vector of bandwidths.
library("smoother") library("xplore") library("plot") randomize(0) n = 500 example = "Marron_Wand_7" h = grid(0.01, 0.02, 25) ; bandwidth grid xgrid = grid(-3.1, 0.05, 125) ; appropriate for MW examples ; generate variates {w, mu, sigma} = normalmixselect(example) x = normalmix(n, w, mu, sigma) ; find bandwidths {hopt, ch} = denxbwcrit("sj", x, h) hrot = denrot(x, "gau") hopt~hrot ; compute density estimates fh1 = denxest(x, hrot, "gau", xgrid) fh2 = denxest(x, hopt, "gau", xgrid) fx = normalmixdens(xgrid, w, mu, sigma) ; plotting disp = createdisplay(2,2) p1 = setmask(ch, "line", "green") p2 = setmask(fx, "line", "thin", "red") p3 = setmask(fh1, "line", "blue") p4 = setmask(fh2, "line", "blue") show(disp, 1, 2, p1, gryline(0, h)) show(disp, 1, 1, p2) show(disp, 2, 1, p2, p3) show(disp, 2, 2, p2, p4) setgopt(disp,1,1,"title",string("Example: %s",example)) setgopt(disp,1,2,"title","SJ-criterion function") setgopt(disp,2,1,"title",string("KDE with h_NR = %f", hrot)) setgopt(disp,2,2,"title",string("KDE with h_SJ = %f", hopt))
Shows a graphic with four windows representing: a bimodal normal mixture density, the criterion function of the Sheather-and-Jones bandwidth selector, a KDE using Silverman's normal reference bandwidth, and a KDE using the optimal Sheather-and-Jones one. Contents of _tmp [1,] 0.19536 0.48772