Learn R Programming

bda (version 3.2.0-3)

lprde: Density Estimation for Weighted Data via Non-parametric Regression

Description

Estimate the probability density of the weighted data via local polynomial nonparametric regression (degree=1).

Usage

lprde(x, w, bandwidth, nclass,  binwidth, lb, gridsize = 512L,
        range.x, freq=FALSE, truncate = TRUE, na.rm=TRUE)

Arguments

x
vector of observations from the distribution whose density is to be estimated. Missing values are not allowed.
w
The weights of x. The weight w_i of any observation x_i should be non-negative. If x_i=0, x_i will be removed from the analysis.
bandwidth
the smoothing parameter. Larger values of bandwidth make smoother estimates, smaller values of bandwidth make less smooth estimates. LSCV bandwidth selectors are developed.
nclass
the number of classes to bin the data. It can be specified as an integer, or as a character variable.
binwidth
bin width.
lb
Lower bound of the first bin.
freq
An indicator showing whether w is a vector of frequecies (counts) or weights.
gridsize
the number of equally spaced points at which to estimate the density.
range.x
vector containing the minimum and maximum values of x at which to compute the estimate. The default is the minimum and maximum data values, extended by the support of the kernel.
truncate
logical flag: if TRUE, data with x values outside the range specified by range.x are ignored.
na.rm
logical flag: if TRUE, NA values will be ignored; otherwise, the program will be halted with error information.

Value

  • a list containing the following components:
  • xvector of sorted x values at which the estimate was computed.
  • yvector of density estimates at the corresponding x.
  • bwoptimal bandwidth.
  • spsensitivity parameter, none NA if adaptive bandwidth selector is used.

encoding

UTF-8

Details

When the data are highly discretized, one can use option binned=TRUE.

References

Wang, B. and Wang, X-F. (2012). To be updated. "Weighted Kernel Density Estimation". JMBMS.

Examples

Run this code
mu = 34.5; s=2.5; n = 10000
 x = round(rnorm(n, mu, s),1)
 x0 = seq(min(x)-s,max(x)+s, length=100)
 f0 = dnorm(x0,mu, s);  ymax <- max(f0*1.2)

 xt = table(x); n = length(x)
 x1 = as.numeric(names(xt))
 w1 = as.numeric(xt)
 
 est1 <- lprde(x1,w1,lb=round(min(x))-0.5,binwidth=1,freq=TRUE)

 est0 <- density(x1,bw="SJ",weights=w1/sum(w1)); 

 plot(f0~x0, xlim=c(min(x),max(x)), ylim=c(0,ymax), 
   xlab="x", ylab="Density", type="l")
 lines(est0, col=1, lty=2, lwd=2)

 lines(est1, col=2)
 legend(max(x),ymax,xjust=1,yjust=1,cex=.8,
  legend=c("N(34.5,1.5)", "SJ", "lprde(x,w)"),
  col = c(1,1,2), 
  lty = c(1,2,1),
  lwd = c(1,2,1))

Run the code above in your browser using DataLab