BayesTools provides several contrast matrix functions for Bayesian factor analysis. These functions create different types of contrast matrices that can be used with factor variables in Bayesian models.

contr.orthonormal(n, contrasts = TRUE)

contr.meandif(n, contrasts = TRUE)

contr.independent(n, contrasts = TRUE)

contr.ordered_cumulative(n, contrasts = TRUE)

contr.ordered_cumulative_levels(n, contrasts = TRUE)

Arguments

n

a vector of levels for a factor, or the number of levels

contrasts

logical indicating whether contrasts should be computed

Details

The package includes the following contrast functions:

contr.orthonormal

Return a matrix of orthonormal contrasts: a normalised Helmert basis, whose columns have unit length and sum to zero and which is identical on every platform. Any orthonormal basis of the centred space implies the same exchangeable prior on the factor level effects under a spherical prior on the coefficients, which corresponds to the description by Rouder et al. (2012) . Returns a matrix with n rows and k columns, with k = n - 1 if contrasts = TRUE and k = n if contrasts = FALSE.

contr.meandif

Return a matrix of mean difference contrasts. This is an adjustment to the contr.orthonormal that ascertains that the prior distributions on difference between the gran mean and factor level are identical independent of the number of factor levels (which does not hold for the orthonormal contrast). Furthermore, the contrast is re-scaled so the specified prior distribution exactly corresponds to the prior distribution on difference between each factor level and the grand mean – this is approximately twice the scale of contr.orthonormal. Returns a matrix with n rows and k columns, with k = n - 1 if contrasts = TRUE and k = n if contrasts = FALSE.

contr.independent

Return a matrix of independent contrasts – a level for each term. Returns a matrix with n rows and k columns, with k = n if contrasts = TRUE and k = n if contrasts = FALSE.

contr.ordered_cumulative

Return a cumulative increment basis for ordered factors, with the first level fixed to zero and the last level equal to the full total effect.

contr.ordered_cumulative_levels

Return a cumulative level basis for ordered factors, where the first level receives the first allocation share and the last level equals the full total effect.

References

Rouder JN, Morey RD, Speckman PL, Province JM (2012). “Default Bayes factors for ANOVA designs.” Journal of Mathematical Psychology, 56(5), 356–374. doi:10.1016/j.jmp.2012.08.001 .

Examples

# Orthonormal contrasts
contr.orthonormal(c(1, 2))
#>            [,1]
#> [1,] -0.7071068
#> [2,]  0.7071068
contr.orthonormal(c(1, 2, 3))
#>            [,1]       [,2]
#> [1,] -0.7071068 -0.4082483
#> [2,]  0.7071068 -0.4082483
#> [3,]  0.0000000  0.8164966

# Mean difference contrasts
contr.meandif(c(1, 2))
#>      [,1]
#> [1,]   -1
#> [2,]    1
contr.meandif(c(1, 2, 3))
#>            [,1] [,2]
#> [1,] -0.8660254 -0.5
#> [2,]  0.8660254 -0.5
#> [3,]  0.0000000  1.0

# Independent contrasts
contr.independent(c(1, 2))
#>      [,1] [,2]
#> [1,]    1    0
#> [2,]    0    1
contr.independent(c(1, 2, 3))
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    0    1    0
#> [3,]    0    0    1

# Ordered cumulative contrasts
contr.ordered_cumulative(c("low", "mid", "high"))
#>      [,1] [,2]
#> [1,]    0    0
#> [2,]    1    0
#> [3,]    1    1
contr.ordered_cumulative_levels(c("low", "mid", "high"))
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    1    1    0
#> [3,]    1    1    1