CreateDensity {frechet} | R Documentation |
Create density functions from raw data, histogram objects or frequency tables with bins
Description
Create kernel density estimate along the support of the raw data using the HADES method.
Usage
CreateDensity(
y = NULL,
histogram = NULL,
freq = NULL,
bin = NULL,
optns = list()
)
Arguments
y |
A vector of raw readings. |
histogram |
A |
freq |
A frequency vector. Use this option when frequency table is only available, but not the raw sample or the histogram object. The corresponding |
bin |
A bin vector having its length with |
optns |
A list of options control parameters specified by |
Details
Available control options are
- userBwMu
The bandwidth value for the smoothed mean function; positive numeric - default: determine automatically based on the data-driven bandwidth selector proposed by Sheather and Jones (1991)
- nRegGrid
The number of support points the KDE; numeric - default: 101.
- delta
The size of the bin to be used; numeric - default:
diff(range(y))/1000
. It only works when the raw sample is available.- kernel
smoothing kernel choice,
"rect"
,"gauss"
,"epan"
,"gausvar"
,"quar"
- default:"gauss"
.- infSupport
logical if we expect the distribution to have infinite support or not; logical - default:
FALSE
.- outputGrid
User defined output grid for the support of the KDE, it overrides
nRegGrid
; numeric - default:NULL
.
Value
A list containing the following fields:
bw |
The bandwidth used for smoothing. |
x |
A vector of length |
y |
A vector of length |
References
-
H.-G. Müller, J.L. Wang and W.B. Capra (1997). "From lifetables to hazard rates: The transformation approach." Biometrika 84, 881–892.
-
S.J. Sheather and M.C. Jones (1991). "A reliable data-based bandwidth selection method for kernel density estimation." JRSS-B 53, 683–690.
-
H.-G. Müller, U. Stadtmüller, and T. Schmitt. (1987) "Bandwidth choice and confidence intervals for derivatives of noisy data." Biometrika 74, 743–749.
Examples
### compact support case
# input: raw sample
set.seed(100)
n <- 100
x0 <-seq(0,1,length.out=51)
Y <- rbeta(n,3,2)
f1 <- CreateDensity(y=Y,optns = list(outputGrid=x0))
# input: histogram
histY <- hist(Y)
f2 <- CreateDensity(histogram=histY,optns = list(outputGrid=x0))
# input: frequency table with unequally spaced (random) bins
binY <- c(0,sort(runif(9)),1)
freqY <- c()
for (i in 1:(length(binY)-1)) {
freqY[i] <- length(which(Y>binY[i] & Y<=binY[i+1]))
}
f3 <- CreateDensity(freq=freqY, bin=binY,optns = list(outputGrid=x0))
# plot
plot(f1$x,f1$y,type='l',col=2,lty=2,lwd=2,
xlim=c(0,1),ylim=c(0,2),xlab='domain',ylab='density')
points(f2$x,f2$y,type='l',col=3,lty=3,lwd=2)
points(f3$x,f3$y,type='l',col=4,lty=4,lwd=2)
points(x0,dbeta(x0,3,2),type='l',lwd=2)
legend('topleft',
c('true','raw sample','histogram','frequency table (unequal bin)'),
col=1:4,lty=1:4,lwd=3,bty='n')
### infinite support case
# input: raw sample
set.seed(100)
n <- 200
x0 <-seq(-3,3,length.out=101)
Y <- rnorm(n)
f1 <- CreateDensity(y=Y,optns = list(outputGrid=x0))
# input: histogram
histY <- hist(Y)
f2 <- CreateDensity(histogram=histY,optns = list(outputGrid=x0))
# input: frequency table with unequally spaced (random) bins
binY <- c(-3,sort(runif(9,-3,3)),3)
freqY <- c()
for (i in 1:(length(binY)-1)) {
freqY[i] <- length(which(Y>binY[i] & Y<=binY[i+1]))
}
f3 <- CreateDensity(freq=freqY, bin=binY,optns = list(outputGrid=x0))
# plot
plot(f1$x,f1$y,type='l',col=2,lty=2,lwd=2,
xlim=c(-3,3),ylim=c(0,0.5),xlab='domain',ylab='density')
points(f2$x,f2$y,type='l',col=3,lty=3,lwd=2)
points(f3$x,f3$y,type='l',col=4,lty=4,lwd=2)
points(x0,dnorm(x0),type='l',lwd=2)
legend('topright',
c('true','raw sample','histogram','frequency table (unequal bin)'),
col=1:4,lty=1:4,lwd=3,bty='n')