Liking cljdoc? Tell your friends :D

fastmath.ml.regression.contrast

Categorical data manipulation and various encoding methods.

A coding (contrast) replaces a categorical variable with n-1 numeric columns, which can be used as terms of a regression model. A coding is a map with the keys :levels, :names (names of the columns) and :mapping (a map from a level to a vector of n-1 coefficients).

Levels are collected from the data with ->levels. The order of levels matters: the first level is the reference of dummy and simple, the last one is dropped by deviation. To use a different reference, as contr.treatment(base=) of R, pass an explicit order to ->levels. A coding is applied to a column with encode, the meaning of the coefficients is returned by mean-contrasts.

Codings: dummy, simple, deviation, polynomial, helmert, reverse-helmert, reverse-helmert-unscaled, forward-difference, backward-difference and difference.

Categorical data manipulation and various encoding methods.

A coding (contrast) replaces a categorical variable with `n-1` numeric columns, which can be used as terms of a regression model. A coding is a map with the keys `:levels`, `:names` (names of the columns) and `:mapping` (a map from a level to a vector of `n-1` coefficients).

Levels are collected from the data with [[->levels]]. The order of levels matters: the first level is the reference of [[dummy]] and [[simple]], the last one is dropped by [[deviation]]. To use a different reference, as `contr.treatment(base=)` of R, pass an explicit `order` to [[->levels]]. A coding is applied to a column with [[encode]], the meaning of the coefficients is returned by [[mean-contrasts]].

Codings: [[dummy]], [[simple]], [[deviation]], [[polynomial]], [[helmert]], [[reverse-helmert]], [[reverse-helmert-unscaled]], [[forward-difference]], [[backward-difference]] and [[difference]].
raw docstring

->levelsclj

(->levels cs)
(->levels cs order)

Returns the levels of a categorical variable.

Parameters:

  • cs - a sequence of categorical values
  • order (optional) - a sequence of all levels, in the required order (the first level is the reference level of the codings)

Without order the levels are returned in the order of the first appearance in cs (R sorts them instead).

Throws ex-info when order does not contain exactly the levels of cs.

See also dummy, encode.

Returns the levels of a categorical variable.

Parameters:

- `cs` - a sequence of categorical values
- `order` (optional) - a sequence of all levels, in the required order (the first level is the reference level of the codings)

Without `order` the levels are returned in the order of the first appearance in `cs` (R sorts them instead).

Throws `ex-info` when `order` does not contain exactly the levels of `cs`.

See also [[dummy]], [[encode]].
raw docstring

backward-differenceclj

(backward-difference levels)

Backward difference coding.

Compares the means of pairs of consecutive levels, each level to the preceding one. The intercept is the mean of all levels.

Parameters:

  • levels - a sequence of at least two different, ordered levels

Returns a coding: a map with :levels, :names (level.vs.previous-level) and :mapping.

Throws ex-info for fewer than two levels or duplicated levels.

See also forward-difference, difference, encode, mean-contrasts.

Backward difference coding.

Compares the means of pairs of consecutive levels, each level to the preceding one. The intercept is the mean of all levels.

Parameters:

- `levels` - a sequence of at least two different, ordered levels

Returns a coding: a map with `:levels`, `:names` (`level.vs.previous-level`) and `:mapping`.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[forward-difference]], [[difference]], [[encode]], [[mean-contrasts]].
raw docstring

deviationclj

(deviation levels)

Deviation (sum) coding.

Compares each level (except the last one) to the mean of all levels. The intercept is the mean of all levels.

Parameters:

  • levels - a sequence of at least two different levels, the last one is dropped

Returns a coding: a map with :levels, :names (level.vs.all) and :mapping. The last level is a row of -1.0.

It is contr.sum of R.

Throws ex-info for fewer than two levels or duplicated levels.

See also simple, helmert, encode, mean-contrasts.

Deviation (sum) coding.

Compares each level (except the last one) to the mean of all levels. The intercept is the mean of all levels.

Parameters:

- `levels` - a sequence of at least two different levels, the last one is dropped

Returns a coding: a map with `:levels`, `:names` (`level.vs.all`) and `:mapping`. The last level is a row of `-1.0`.

It is `contr.sum` of R.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[simple]], [[helmert]], [[encode]], [[mean-contrasts]].
raw docstring

differenceclj

(difference levels)

Cumulative difference coding.

Compares each level to the preceding one, the same comparisons as backward-difference. The columns are cumulative sums of dummy columns, so the intercept is the mean of the first level (not the mean of all levels).

Parameters:

  • levels - a sequence of at least two different, ordered levels

Returns a coding: a map with :levels, :names (level.vs.previous-level) and :mapping. The first level is a row of zeros, the level with the index k (counted from 0) has 1.0 in the first k columns.

Throws ex-info for fewer than two levels or duplicated levels.

See also backward-difference, dummy, encode, mean-contrasts.

Cumulative difference coding.

Compares each level to the preceding one, the same comparisons as [[backward-difference]]. The columns are cumulative sums of dummy columns, so the intercept is the mean of the first level (not the mean of all levels).

Parameters:

- `levels` - a sequence of at least two different, ordered levels

Returns a coding: a map with `:levels`, `:names` (`level.vs.previous-level`) and `:mapping`. The first level is a row of zeros, the level with the index k (counted from 0) has `1.0` in the first k columns.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[backward-difference]], [[dummy]], [[encode]], [[mean-contrasts]].
raw docstring

dummyclj

(dummy levels)

Dummy (treatment) coding.

Compares each level to the reference (first) level. The intercept is the mean of the first level.

Parameters:

  • levels - a sequence of at least two different levels, the first one is the reference

Returns a coding: a map with :levels, :names (level.vs.reference) and :mapping. The reference level is a row of zeros, other levels have a single 1.0.

It is contr.treatment of R. Use ->levels with order to select the reference level: the last level as the reference is contr.SAS.

Throws ex-info for fewer than two levels or duplicated levels.

See also simple, encode, mean-contrasts.

Dummy (treatment) coding.

Compares each level to the reference (first) level. The intercept is the mean of the first level.

Parameters:

- `levels` - a sequence of at least two different levels, the first one is the reference

Returns a coding: a map with `:levels`, `:names` (`level.vs.reference`) and `:mapping`. The reference level is a row of zeros, other levels have a single `1.0`.

It is `contr.treatment` of R. Use [[->levels]] with `order` to select the reference level: the last level as the reference is `contr.SAS`.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[simple]], [[encode]], [[mean-contrasts]].
raw docstring

encodeclj

(encode {:keys [levels mapping]} column)

Applies a coding to a categorical column.

Parameters:

  • coding - a coding returned by one of the coding functions of this namespace
  • column - a sequence of categorical values, each of them has to be one of the :levels of the coding

Returns a vector of rows, one vector of coefficients for each value of column. The rows can be used as terms of a regression model.

Throws ex-info with the :value and the :levels when a value is not a level of the coding.

See also ->levels, mean-contrasts.

Applies a coding to a categorical column.

Parameters:

- `coding` - a coding returned by one of the coding functions of this namespace
- `column` - a sequence of categorical values, each of them has to be one of the `:levels` of the coding

Returns a vector of rows, one vector of coefficients for each value of `column`. The rows can be used as terms of a regression model.

Throws `ex-info` with the `:value` and the `:levels` when a value is not a level of the coding.

See also [[->levels]], [[mean-contrasts]].
raw docstring

forward-differenceclj

(forward-difference levels)

Forward difference coding.

Compares the means of pairs of consecutive levels, each level to the following one. The intercept is the mean of all levels.

Parameters:

  • levels - a sequence of at least two different, ordered levels

Returns a coding: a map with :levels, :names (level.vs.next-level) and :mapping.

Throws ex-info for fewer than two levels or duplicated levels.

See also backward-difference, difference, encode, mean-contrasts.

Forward difference coding.

Compares the means of pairs of consecutive levels, each level to the following one. The intercept is the mean of all levels.

Parameters:

- `levels` - a sequence of at least two different, ordered levels

Returns a coding: a map with `:levels`, `:names` (`level.vs.next-level`) and `:mapping`.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[backward-difference]], [[difference]], [[encode]], [[mean-contrasts]].
raw docstring

helmertclj

(helmert levels)

Helmert coding.

Compares each level (except the last one) to the mean of all following levels. The intercept is the mean of all levels. The coefficients of the model are the differences of the means.

Parameters:

  • levels - a sequence of at least two different levels

Returns a coding: a map with :levels, :names (level.vs.later) and :mapping.

Throws ex-info for fewer than two levels or duplicated levels.

See also reverse-helmert, reverse-helmert-unscaled, encode, mean-contrasts.

Helmert coding.

Compares each level (except the last one) to the mean of all following levels. The intercept is the mean of all levels. The coefficients of the model are the differences of the means.

Parameters:

- `levels` - a sequence of at least two different levels

Returns a coding: a map with `:levels`, `:names` (`level.vs.later`) and `:mapping`.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[reverse-helmert]], [[reverse-helmert-unscaled]], [[encode]], [[mean-contrasts]].
raw docstring

mean-contrastsclj

(mean-contrasts coding)
(mean-contrasts {:keys [names levels mapping]} approx?)

Returns mean contrasts for given coding scheme.

The result shows how the coefficients of a model with the given coding are related to the means of the levels: each coefficient is the sum of the means of levels weighted by the vector assigned to the coefficient.

Parameters:

  • coding - a coding returned by one of the coding functions of this namespace
  • approx? (default: true) - if true or a number, values are rounded to the given number of decimal digits (10 for true)

Returns a map from the name of the coefficient (and :$intercept) to a vector of weights of the means of levels (in the order of :levels).

Returns mean contrasts for given coding scheme.

The result shows how the coefficients of a model with the given coding are related to the means of the levels: each coefficient is the sum of the means of levels weighted by the vector assigned to the coefficient.

Parameters:

- `coding` - a coding returned by one of the coding functions of this namespace
- `approx?` (default: `true`) - if `true` or a number, values are rounded to the given number of decimal digits (`10` for `true`)

Returns a map from the name of the coefficient (and `:$intercept`) to a vector of weights of the means of levels (in the order of `:levels`).
raw docstring

polynomialclj

(polynomial levels)

Orthonormal polynomials coding.

Creates n-1 orthonormal polynomials of increasing degree (linear, quadratic, cubic, ...) over equally spaced levels, which should be ordered. The intercept is the mean of all levels.

Parameters:

  • levels - a sequence of at least two different, ordered levels

Returns a coding: a map with :levels, :names (^1, ^2, ...) and :mapping. The columns are orthonormal.

It is contr.poly of R.

Throws ex-info for fewer than two levels or duplicated levels.

See also deviation, encode, mean-contrasts.

Orthonormal polynomials coding.

Creates `n-1` orthonormal polynomials of increasing degree (linear, quadratic, cubic, ...) over equally spaced levels, which should be ordered. The intercept is the mean of all levels.

Parameters:

- `levels` - a sequence of at least two different, ordered levels

Returns a coding: a map with `:levels`, `:names` (`^1`, `^2`, ...) and `:mapping`. The columns are orthonormal.

It is `contr.poly` of R.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[deviation]], [[encode]], [[mean-contrasts]].
raw docstring

reverse-helmertclj

(reverse-helmert levels)

Reverse Helmert coding.

Compares each level (except the first one) to the mean of all preceding levels. The intercept is the mean of all levels. The coefficients of the model are the differences of the means.

Parameters:

  • levels - a sequence of at least two different levels

Returns a coding: a map with :levels, :names (level.vs.earlier) and :mapping. The k-th column is the k-th column of reverse-helmert-unscaled divided by k+1.

Throws ex-info for fewer than two levels or duplicated levels.

See also helmert, reverse-helmert-unscaled, encode, mean-contrasts.

Reverse Helmert coding.

Compares each level (except the first one) to the mean of all preceding levels. The intercept is the mean of all levels. The coefficients of the model are the differences of the means.

Parameters:

- `levels` - a sequence of at least two different levels

Returns a coding: a map with `:levels`, `:names` (`level.vs.earlier`) and `:mapping`. The k-th column is the k-th column of [[reverse-helmert-unscaled]] divided by `k+1`.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[helmert]], [[reverse-helmert-unscaled]], [[encode]], [[mean-contrasts]].
raw docstring

reverse-helmert-unscaledclj

(reverse-helmert-unscaled levels)

Reverse Helmert coding with integer coefficients.

Compares each level (except the first one) to the mean of all preceding levels. The intercept is the mean of all levels. The coefficients of the model are the differences of the means divided by the number of levels compared (k+1 for the k-th column).

Parameters:

  • levels - a sequence of at least two different levels

Returns a coding: a map with :levels, :names (level.vs.earlier) and :mapping. The k-th column contains -1.0 for the first k levels, k for the level k and 0.0 for the following ones.

It is contr.helmert of R. reverse-helmert contains the same columns divided by k+1.

Throws ex-info for fewer than two levels or duplicated levels.

See also helmert, encode, mean-contrasts.

Reverse Helmert coding with integer coefficients.

Compares each level (except the first one) to the mean of all preceding levels. The intercept is the mean of all levels. The coefficients of the model are the differences of the means divided by the number of levels compared (`k+1` for the k-th column).

Parameters:

- `levels` - a sequence of at least two different levels

Returns a coding: a map with `:levels`, `:names` (`level.vs.earlier`) and `:mapping`. The k-th column contains `-1.0` for the first `k` levels, `k` for the level k and `0.0` for the following ones.

It is `contr.helmert` of R. [[reverse-helmert]] contains the same columns divided by `k+1`.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[helmert]], [[encode]], [[mean-contrasts]].
raw docstring

simpleclj

(simple levels)

Simple coding.

Compares each level to the reference (first) level. The intercept is the mean of all levels.

Parameters:

  • levels - a sequence of at least two different levels, the first one is the reference

Returns a coding: a map with :levels, :names (level.vs.reference) and :mapping. Each column contains 1-1/n for its level and -1/n for others.

Throws ex-info for fewer than two levels or duplicated levels.

See also dummy, deviation, encode, mean-contrasts.

Simple coding.

Compares each level to the reference (first) level. The intercept is the mean of all levels.

Parameters:

- `levels` - a sequence of at least two different levels, the first one is the reference

Returns a coding: a map with `:levels`, `:names` (`level.vs.reference`) and `:mapping`. Each column contains `1-1/n` for its level and `-1/n` for others.

Throws `ex-info` for fewer than two levels or duplicated levels.

See also [[dummy]], [[deviation]], [[encode]], [[mean-contrasts]].
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close