contr.orthonorm {bayestestR}R Documentation

Orthonormal Contrast Matrices for Bayesian Estimation


Returns a design or model matrix of orthonormal contrasts such that the marginal prior on all effects is identical (see 'Details'). Implementation from Singmann & Gronau's bfrms, following the description in Rouder, Morey, Speckman, & Province (2012, p. 363).

Though using this factor coding scheme might obscure the interpretation of parameters, it is essential for correct estimation of Bayes factors for contrasts and order restrictions of multi-level factors (where k>2). See info on specifying correct priors for factors with more than 2 levels in the Bayes factors vignette.


contr.orthonorm(n, contrasts = TRUE, sparse = FALSE)



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


a logical indicating whether contrasts should be computed.


logical indicating if the result should be sparse (of class dgCMatrix), using package Matrix.


When contrasts = FALSE, the returned contrasts are equivalent to contr.treatment(, contrasts = FALSE), as suggested by McElreath (also known as one-hot encoding).

Setting Priors

It is recommended to set 0-centered identically scaled priors of the dummy coded variables produced by this method. These priors then represent the distance the mean of one of the levels might have from the overall mean.


This method guarantees that any set of contrasts between the k groups will have the same multivariate prior regardless of level order; However, different contrasts within a set contrasts can have different univariate prior shapes/scales.

For example the contrasts A - B will have the same prior as B - C, as will (A + C) - B and (B + A) - C, but A - B and (A + C) - B will differ.


A matrix with n rows and k columns, with k=n-1 if contrasts is TRUE and k=n if contrasts is FALSE.



contr.orthonorm(2) # Q_2 in Rouder et al. (2012, p. 363)

contr.orthonorm(5) # equivalent to Q_5 in Rouder et al. (2012, p. 363)

## check decomposition
Q3 <- contr.orthonorm(3)
Q3 %*% t(Q3) ## 2/3 on diagonal and -1/3 on off-diagonal elements

[Package bayestestR version 0.11.5 Index]