compute_density: Compute density of data.

Description Usage Arguments Value Examples

View source: R/compute_density.R

Description

Compute density of data.

Usage

1
2
compute_density(x, x_var, w_var = NULL, kernel = "gaussian", trim = FALSE,
  n = 256L, na.rm = FALSE, ...)

Arguments

x

Dataset (data frame, grouped_df or ggvis) object to work with.

x_var, w_var

Names of variables to use for x position, and for weights.

kernel

Smoothing kernel. See density for details.

trim

If TRUE, the default, density estimates are trimmed to the actual range of the data. If FALSE, they are extended by the default 3 bandwidths (as specified by the cut parameter to density).

n

Number of points (along x) to use in the density estimate.

na.rm

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

...

Additional arguments passed on to density.

Value

A data frame with columns:

pred_

regularly spaced grid of n locations

resp_

density estimate

Examples

1
2
3
4
mtcars %>% compute_density(~mpg, n = 5)
mtcars %>% group_by(cyl) %>% compute_density(~mpg, n = 5)
mtcars %>% ggvis(~mpg) %>% compute_density(~mpg, n = 5) %>%
  layer_points(~pred_, ~resp_)

Example output

      pred_        resp_
1  2.969963 1.142299e-04
2 12.559981 3.648257e-02
3 22.150000 5.285952e-02
4 31.740019 1.843844e-02
5 41.330037 6.481419e-05
# A tibble: 15 x 3
# Groups:   cyl [3]
     cyl     pred_        resp_
   <dbl>     <dbl>        <dbl>
 1     4 13.862190 0.0003608151
 2     4 20.756095 0.0557316200
 3     4 27.650000 0.0570477944
 4     4 34.543905 0.0317105107
 5     4 41.437810 0.0001868163
 6     6 15.140627 0.0009707982
 7     6 17.370314 0.1127744633
 8     6 19.600000 0.1908332694
 9     6 21.829686 0.1446388632
10     6 24.059373 0.0010594070
11     8  8.201102 0.0008757959
12     8 11.500551 0.0272083941
13     8 14.800000 0.2223223191
14     8 18.099449 0.0649711085
15     8 21.398898 0.0004829170

ggvis documentation built on May 29, 2017, 9:37 p.m.