This function constructs a contrast code from a character variable. It is useful when one needs to recode a two-category character variable to a numeric one.

build_contrast(vector, cond_a, cond_b)

Arguments

vector

A character vector.

cond_a

A character string to be coded -0.5.

cond_b

A character string to be coded 0.5.

Value

A numeric vector.

Details

The lm method supports factor and character variables by dummy coding them. Dummy coding can make the interpretation of regression coefficient difficult or at least more difficult than contrast coding. Contrast-coded-variable coefficients interpretation is particularly useful when conducting a joint-significance test.

See also

scale for centering continuous numeric variable.

Examples

  data(ho_et_al)

  ho_et_al$condition_contrast <- build_contrast(ho_et_al$condition,
                                                "Low discrimination",
                                                "High discrimination")

 head(ho_et_al)
#>   id           condition    sdo linkedfate hypodescent condition_contrast
#> 1  2  Low discrimination 1.8125      6.000    2.333333               -0.5
#> 2  3 High discrimination 1.5625      5.875    6.000000                0.5
#> 3  4 High discrimination 1.7500      6.625    6.000000                0.5
#> 4  5  Low discrimination 4.2500      5.125    5.666667               -0.5
#> 5  6  Low discrimination 1.9375      4.375    4.000000               -0.5
#> 6  9 High discrimination 2.8750      3.750    4.000000                0.5