local_density_1d: 1d local density estimate.

Description Usage Arguments Details Value See Also Examples

Description

1d local density estimate.

Usage

1
2
local_density_1d(x, weight, scale=TRUE, nn=0.7, h=0,
    kernel="tcub", degree=2, bounded=FALSE, na.rm=FALSE)

Arguments

x

a numeric vector of x positions

weight

NULL or a numeric vector providing weights for each observation

nn

span, or nearest neighbour fraction, the proportion of the total observations used in predict

h

constant bandwidth. Use if you want a constant bandwith, rather than a bandwidth that varies. If both nn and h are set, each is calculated for each location and the largest is used. If scale = TRUE the unit of this value is standard deviations, otherwise, it's in the units of the data.

kernel

Weight function, default is tricubic. Other choices are "rect", '"trwt"', '"tria"', '"epan"', '"bisq"' and '"gauss"'. Choices may be restricted when derivatives are required; e.g. for confidence bands and some bandwidth selectors.

degree

degree of polynomial used for smoothing

bounded

if TRUE bounds density to range of data, otherwise it takes non-zero values over the entire real line.

scale

if TRUE rescale x values to have standard deviation 1.

na.rm

If TRUE missing values will be silently removed, otherwise they will be removed with a warning.

Details

This convenience function wraps up the important arguments of locfit and lp, and hopefully provides enough documentation so that you don't need to refer to the original documentation and book.

Adaptive penalties are not available in this convenience function because they typically require a two-stage fit, where the first fit is used to estimate the local variance.

If you recieve an error newsplit: out of vertex space, that indicates that your bandwidth is too small.

Value

a function of a single that returns the density at that location

See Also

"Local Regression and Likelihood," C. Loader. Springer, 1999.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
data(baseball, package = "plyr")
dens <- local_density_1d(baseball$g)
plot(dens)

# nn is like span in loess - it's a proportion
plot(local_density_1d(baseball$g, nn = 0.1))
plot(local_density_1d(baseball$g, nn = 0.2))
plot(local_density_1d(baseball$g, nn = 0.5))
plot(local_density_1d(baseball$g, nn = 1))

# when bounded = TRUE assumes that there are no possible values outside
# of the data range
plot(local_density_1d(baseball$g, nn = 0.2))
plot(local_density_1d(baseball$g, nn = 0.2, bounded = TRUE))

# When scale = FALSE, the bandwidth is specified in terms of the
# range of the original data.  When scale = TRUE, the data has been
# scaled to have sd 1
plot(local_density_1d(baseball$g, h = 0.5, nn = 0, scale = TRUE))
plot(local_density_1d(baseball$g, h = 0.5, nn = 0, scale = FALSE))

hadley/densityvis documentation built on May 17, 2019, 9:56 a.m.