numeric.encoder {midr}R Documentation

Encoder for Quantitative Variables

Description

numeric.encoder() returns an encoder for a quantitative variable.

Usage

numeric.encoder(
  x,
  k,
  type = 1L,
  encoding.digits = NULL,
  tag = "x",
  frame = NULL,
  weights = NULL
)

numeric.frame(
  reps = NULL,
  breaks = NULL,
  type = NULL,
  encoding.digits = NULL,
  tag = "x"
)

## S3 method for class 'encoder'
print(x, digits = NULL, ...)

Arguments

x

a numeric vector to be encoded.

k

an integer specifying the coarseness of the encoding. If not positive, all unique values of x are used as sample points.

type

an integer specifying the encoding method. If 1, values are encoded to a [0, 1] scale based on linear interpolation of the knots. If 0, values are encoded to 0 or 1 using ont-hot encoding on the intervals.

encoding.digits

an integer specifying the rounding digits for the encoding in case type is 1.

tag

character string. The name of the variable.

frame

a "numeric.frame" object or a numeric vector that defines the sample points of the binning.

weights

optional. A numeric vector of sample weights for each value of x.

reps

a numeric vector to be used as the representative values (knots).

breaks

a numeric vector to be used as the binning breaks.

digits

the minimum number of significant digits to be used.

...

not used.

Details

numeric.encoder() selects sample points from the variable x and returns a list containing the encode() function to convert a vector into a dummy matrix. If type is 1, k is considered the maximum number of knots, and the values between two knots are encoded as two decimals, reflecting the relative position to the knots. If type is 0, k is considered the maximum number of intervals, and the values are converted using one-hot encoding on the intervals.

Value

numeric.encoder() returns a list containing the following components:

frame

an object of class "numeric.frame".

encode

a function to encode x into a dummy matrix.

n

the number of encoding levels.

type

the type of encoding, "linear" or "constant".

numeric.frame() returns a "numeric.frame" object containing the encoding information.

Examples

data(iris, package = "datasets")
enc <- numeric.encoder(x = iris$Sepal.Length, k = 5L, tag = "Sepal.Length")
enc$frame
enc$encode(x = c(4:8, NA))

frm <- numeric.frame(breaks = seq(3, 9, 2), type = 0L)
enc <- numeric.encoder(x = iris$Sepal.Length, frame = frm)
enc$encode(x = c(4:8, NA))

enc <- numeric.encoder(x = iris$Sepal.Length, frame = seq(3, 9, 2))
enc$encode(x = c(4:8, NA))

[Package midr version 0.5.0 Index]