This function creates \(k - 1\) variables for a categorical variable with \(k\) distinct levels. The coding system available in this function are dummy coding, simple coding, unweighted effect coding, weighted effect coding, repeated coding, forward Helmert coding, reverse Helmert coding, and orthogonal polynomial coding.
coding(..., data = NULL,
type = c("dummy", "simple", "effect", "weffect", "repeat",
"fhelm", "rhelm", "poly"), base = NULL,
name = c("dum.", "sim.", "eff.", "weff.", "rep.", "fhelm.", "rhelm.", "poly."),
append = TRUE, as.na = NULL, check = TRUE)
Returns a data frame with \(k - 1\) coded variables or a data frame with the
same length or same number of rows as ...
containing the coded variables.
a numeric vector with integer values, character vector or factor
Alternatively, an expression indicating the variable name in
data
. Note that the function can only deal with one
categorical variable.
a data frame when specifying a variable in the argument ...
.
Note that the argument is NULL
when specifying a numeric
vector with integer values, character vector or factor numeric
vector for the argument ...
.
a character string indicating the type of coding, i.e.,
dummy
(default) for dummy coding, simple
for
simple coding, effect
for unweighted effect coding,
weffect
for weighted effect coding, repeat
for repeated coding, fhelm
for forward Helmert coding,
rhelm
for reverse Helmert coding, and poly
for
orthogonal polynomial coding (see 'Details').
a numeric value or character string indicating the baseline group for dummy and simple coding and the omitted group in effect coding. By default, the first group or factor level is selected as baseline or omitted group.
a character string or character vector indicating the names
of the coded variables. By default, variables are named
"dum."
, "sim."
, "eff."
, "weff."
,
"rep."
, "fhelm."
, "rhelm."
,or "poly."
depending on the type
of coding with the category used
in the comparison (e.g., "dum.2"
and "dum.3"
).
Variable names can be specified using a character string (e.g.,
name = "dummy_"
leads to dummy_2
and dummy_3
)
or a character vector matching the number of coded variables
(e.g. name = c("x1_2", "x1_3")
) which is the number of
unique categories minus one.
logical: if TRUE
(default), coded variables are appended
to the data frame specified in the argument data
.
a numeric vector indicating user-defined missing values,
i.e. these values are converted to NA
before conducting
the analysis.
logical: if TRUE
(default), argument specification is checked.
Takuya Yanagida takuya.yanagida@univie.ac.at
Dummy or treatment coding compares the mean of
each level of the categorical variable to the mean of a baseline group. By
default, the first group or factor level is selected as baseline group. The
intercept in the regression model represents the mean of the baseline group.
For example, dummy coding based on a categorical variable with four groups
A
, B
, C
, D
makes following comparisons:
B vs A
, C vs A
, and D vs A
with A
being the
baseline group.
Simple coding compares each level of the
categorical variable to the mean of a baseline level. By default, the first
group or factor level is selected as baseline group. The intercept in the
regression model represents the unweighted grand mean, i.e., mean of group
means. For example, simple coding based on a categorical variable with four
groups A
, B
, C
, D
makes following comparisons:
B vs A
, C vs A
, and D vs A
with A
being the
baseline group.
Unweighted effect or sum coding
compares the mean of a given level to the unweighted grand mean, i.e., mean of
group means. By default, the first group or factor level is selected as
omitted group. For example, effect coding based on a categorical variable
with four groups A
, B
, C
, D
makes following
comparisons: B vs (A, B, C, D)
, C vs (A, B, C, D)
, and
D vs (A, B, C, D)
with A
being the omitted group.
Weighted effect or sum coding compares
the mean of a given level to the weighed grand mean, i.e., sample mean. By
default, the first group or factor level is selected as omitted group. For
example, effect coding based on a categorical variable with four groups
A
, B
, C
, D
makes following comparisons:
B vs (A, B, C, D)
, C vs (A, B, C, D)
, and D vs (A, B, C, D)
with A
being the omitted group.
Repeated or difference coding compares the
mean of each level of the categorical variable to the mean of the previous
adjacent level. For example, repeated coding based on a categorical variable
with four groups A
, B
, C
, D
makes following
comparisons: B vs A
, C vs B
, and D vs C
.
Forward Helmert coding compares the
mean of each level of the categorical variable to the unweighted mean of all
subsequent level(s) of the categorical variable. For example, forward Helmert
coding based on a categorical variable with four groups A
, B
,
C
, D
makes following comparisons: (B, C, D) vs A
,
(C, D) vs B
, and D vs C
.
Reverse Helmert coding compares the
mean of each level of the categorical variable to the unweighted mean of all
prior level(s) of the categorical variable. For example, reverse Helmert
coding based on a categorical variable with four groups A
, B
,
C
, D
makes following comparisons: B vs A
, C vs (A, B)
,
and D vs (A, B, C)
.
Orthogonal polynomial coding is
a form of trend analysis based on polynomials of order \(k - 1\), where
\(k\) is the number of levels of the categorical variable. This coding
scheme assumes an ordered-categorical variable with equally spaced levels.
For example, orthogonal polynomial coding based on a categorical variable with
four groups A
, B
, C
, D
investigates a linear,
quadratic, and cubic trends in the categorical variable.
rec
, item.reverse
# Example 1a: Dummy coding for 'gear', baseline group = 3
coding(gear, data = mtcars)
# Example 1b: Alterantive specification without using the 'data' argument
coding(mtcars$gear)
# Example 2: Dummy coding for 'gear', baseline group = 4
coding(gear, data = mtcars, base = 4)
# Example 3: Effect coding for 'gear', omitted group = 3
coding(gear, data = mtcars, type = "effect")
# Example 3: Effect coding for 'gear', omitted group = 4
coding(gear, data = mtcars, type = "effect", base = 4)
# Example 4a: Dummy-coded variable names with prefix "gear3."
coding(gear, data = mtcars, name = "gear3.")
# Example 4b: Dummy-coded variables named "gear_4vs3" and "gear_5vs3"
coding(gear, data = mtcars, name = c("gear_4vs3", "gear_5vs3"))
Run the code above in your browser using DataLab