scaled_contr_poly: Create scaled orthogonal polynomial contrasts for an ordered...

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/contrast_functions.R

Description

The function contr.poly creates orthogonal polynomial contrasts for an ordered factor, with the standard deviations of the columns in the contrast matrix determined by the number of columns. The scaled_contr_poly function takes this contrast matrix and alters the scale so that the standard deviations of the columns all equal scale.

Usage

1
scaled_contr_poly(x, scale = 1, return_contr = TRUE)

Arguments

x

A factor, a numeric or character vector of levels ordered least to greatest, or a single integer greater than or equal to 3. See 'Details'.

scale

A single positive number indicating the standard deviation for the columns of the contrast matrix. Default is 1.

return_contr

A logical indicating whether the contrast matrix should be returned, or x as an ordered factor with the contrasts applied. See 'Details'.

Details

If x is a factor, then the non-NA levels of x are used as the levels for the contrast matrix. If x is a vector, then the unique non-NA values in x in the order in which they appear in x are used as the levels for the contrast matrix. If x is a single integer greater than or equal to 3, then the numbers 1:x are used as the levels for the contrast matrix. Any other value for x results in an error (if x = 2, then polynomial contrasts are technically possible, but all binary predictors should be treated as unordered factors and coded with sum contrasts). contr.poly is then called to obtain an orthogonal polynomial contrast matrix of the appropriate degree. The contrast matrix is is put on unit scale and then multiplied by the scale argument, resulting in an orthogonal polynomial contrast matrix where each column has standard deviation scale. If return_contr = TRUE, the contrast matrix is returned. If return_contr = FALSE, then x is coerced to an ordered factor with the contrast matrix applied, and x is returned. NA is never assigned as a level in the contrast matrix or in the factor returned by the function, but NA values in x are not removed in the factor returned when return_contr = FALSE.

Value

If return_contr = TRUE a scaled orthogonal polynomial contrast matrix is returned. If return_contr = FALSE, then a factor with the scaled orthogonal polynomial contrasts is returned.

Author(s)

Christopher D. Eager <eager.stats@gmail.com>

See Also

named_contr_sum for unordered factors.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
f <- factor(rep(c("a", "b", "c"), 5), ordered = TRUE)
contrasts(f) <- contr.poly(3)

# difference in contrasts
contrasts(f)
scaled_contr_poly(f)
scaled_contr_poly(f, scale = 0.5)

# different options for 'x'
scaled_contr_poly(levels(f))
scaled_contr_poly(3)
scaled_contr_poly(c(2, 5, 6))

# return factor
f2 <- scaled_contr_poly(f, return_contr = FALSE)
f2

CDEager/standardize documentation built on March 13, 2021, 3:48 p.m.