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

Example output

             .L         .Q
a -7.071068e-01  0.4082483
b -7.850462e-17 -0.8164966
c  7.071068e-01  0.4082483
             .L         .Q
a -1.000000e+00  0.5773503
b -2.168241e-17 -1.1547005
c  1.000000e+00  0.5773503
            .L         .Q
a -5.00000e-01  0.2886751
b -1.08412e-17 -0.5773503
c  5.00000e-01  0.2886751
             .L         .Q
a -1.000000e+00  0.5773503
b -2.168241e-17 -1.1547005
c  1.000000e+00  0.5773503
                .L         .Q
[1,] -1.000000e+00  0.5773503
[2,] -2.168241e-17 -1.1547005
[3,]  1.000000e+00  0.5773503
             .L         .Q
2 -1.000000e+00  0.5773503
5 -2.168241e-17 -1.1547005
6  1.000000e+00  0.5773503
 [1] a b c a b c a b c a b c a b c
attr(,"contrasts")
             .L         .Q
a -1.000000e+00  0.5773503
b -2.168241e-17 -1.1547005
c  1.000000e+00  0.5773503
Levels: a < b < c

standardize documentation built on March 5, 2021, 9:07 a.m.