binned: Binned One-Way Data/Plot

Description Usage Arguments Value Examples

Description

Calculate the (weighted) average value of a binned set of data, and plot it.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
binned_one_way_data(
  x,
  yData,
  weight = rep(1, length(x)),
  scaleWeight = TRUE,
  type = "quantile",
  bins = 10
)

binned_one_way_plot(
  x,
  yData,
  weight = rep(1, length(x)),
  scaleWeight = TRUE,
  type = "quantile",
  bins = 10,
  fontSize = 10,
  showWeights = TRUE,
  plotly = FALSE,
  bgColor = "#CCDDFF",
  xlab = "Bins",
  ylab = "Response",
  wlab = "Weight",
  title = paste("One-Way Plot", if (!missing(xlab)) paste0("of ", xlab), if
    (!missing(ylab)) paste0("by ", ylab))
)

Arguments

x

The vector to perform binning by. If numeric, it will be binned according to type. If non-numeric, it will be binned according to all the unique values.

yData

A data.frame-like object (or numeric vector) whose numerical columns will be used to calculate average values by bin. Must have the same number of rows (or length) as the length of x.

weight

An (optional) vector of weights for weighted binning, and for taking weighted averages. Must be the same length as x.

scaleWeight

If TRUE, the weights will be scaled from 0 to 1 (i.e. as a percent).

type

The type of binning desired. Valid options are: r == raw, which will create a bin for each distinct value in x. e == equal, which will do the same as p == pretty. p == pretty, which will cut the range of x into equally sized bins, with the endpoints of each bin being chosen to be nice round numbers. q == quantile, which will cut x into bins according to weighted quantiles. m == min == minimum, which will try to ensure there are a minimum of bins observations in each bin.

bins

The number of bins to create (or, if type is in m == min == minimum, the desired minimum number of observations per bin).

fontSize

Size of the font to use in the plot.

showWeights

If TRUE, the weights plot will be shown as well.

plotly

Will return a plotly object instead of a ggplot one.

bgColor

Color to use for the background of the plot.

xlab

The x-axis label of the plot.

ylab

The y-axis label of the data section of the plot.

wlab

The y-axis label of the weight section of the plot.

title

The title of the plot.

Value

binned_one_way_data: a data.table holding the binned data.

binned_one_way_plot: a ggplot object, or a plotly object if plotly is TRUE.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
diamonds <- data.table::data.table(ggplot2::diamonds)
binnedData1 <- binned_one_way_data(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)],
                                   diamonds[, price])
binnedData2 <- binned_one_way_data(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)],
                                   diamonds[, price], scaleWeight = FALSE)
binnedData3 <- binned_one_way_data(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)],
                                   diamonds[, price], type = "equal")
binnedData4 <- binned_one_way_data(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)], 
                                   diamonds[, price], bins = 5)
binnedPlot1 <- binned_one_way_plot(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)],
                                   diamonds[, price])
binnedPlot2 <- binned_one_way_plot(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)],
                                   diamonds[, price], scaleWeight = FALSE)
binnedPlot3 <- binned_one_way_plot(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)],
                                   diamonds[, price], type = "equal")
binnedPlot4 <- binned_one_way_plot(diamonds[, carat], diamonds[, .(x = x + 0.5, y, z)],
                                   diamonds[, price], bins = 5)

KO112/KO documentation built on Oct. 2, 2020, 9:21 a.m.