Skip to content
Prev 263955 / 398502 Next

Kernel Density Estimation at manually specified points

May be a kludge, but it might be simpler to write your own density function for a few specified points.

For example

my.density <- function(x, bw = 'nrd0', at) {
    x<-na.omit(x)
    
    #####
    #Borrowed from density.default for compatibility
    
    if (is.character(bw)) {
        if (length(x) < 2) 
            stop("need at least 2 points to select a bandwidth automatically")
        bw <- switch(tolower(bw), nrd0 = bw.nrd0(x), nrd = bw.nrd(x), 
            ucv = bw.ucv(x), bcv = bw.bcv(x), sj = , `sj-ste` = bw.SJ(x, 
                method = "ste"), `sj-dpi` = bw.SJ(x, method = "dpi"), 
            stop("unknown bandwidth rule"))
    }
    ######
    at <- matrix(at, ncol=1)
    y <- apply(at, 1, FUN=function(a, x, bw) sum(dnorm(a, x, bw)/length(x)), x=x, bw=bw )
    return(list(x=at, y=y))

}

x<-rnorm(500)
plot(density(x))
at<-seq(-2, 2, 0.25)
points(my.density(x, at=at))
This email and any attachments are confidential. Any use...{{dropped:8}}