# How to calculate (standardized) orthogonal contrast coding in R?

I want to determine efficient fractional factorial designs and blocked designs for factorial surveys and am using the R library AlgDesign. The criteria I'm using to determine if a design is efficient enough is the D-efficiency. In order to get a better understanding how efficient my designs are, I want the D-efficiency to be in the range of 0 to 1.

I've read in literature that this can be achieved by using (standardized) orthogonal contrast coding for my factors. This works fine and I'm getting D-efficiencies in the desired range, that I can easily interpret. What I'm doing is replacing the contrasts for my factors in R by the contrast matrix given in the literature [for example Kuhfeld (1994)]:

lang<-as.factor(c("English","Chinese","Korean","German"))
contrasts(lang)
contrastmatrixlang<-cbind(cbind(c(1.414215,0,0,-1.414214), c(-0.816497,1.632993,0,-0.816497)),c(-0.57735,-0.57735,1.73205,-0.57735)) # orthogonal coding for 4-level factors, see Kuhfeld 1997
contrasts(lang)<-contrastmatrixlang


However providing the standardized orthogonal contrast coding like this is somewhat problematic since I can only find these codings for a maximum of five-level factors. So if I want to use a six-level factor (or higher), I don't know what contrast coding to use. Does anyone know a way to calculate these standardized orthogonal contrast codings? Or is there another way to choose contrasts in a way that help me to get d-efficiencies in the desired range?

A full example of what I'm doing atm below:

lang<-as.factor(c("English","Chinese","Korean","German"))
contrasts(lang)
contrastmatrixlang<-cbind(cbind(c(1.414215,0,0,-1.414214), c(-0.816497,1.632993,0,-0.816497)),c(-0.57735,-0.57735,1.73205,-0.57735)) # orthogonal coding for 4-level factors, see Kuhfeld 1997
contrasts(lang)<-contrastmatrixlang
marital<-as.factor(c("Single","Married"))
contrastmatrixmarital<-c(1,-1) # orthogonal coding for 2-level factors
contrasts(marital)<-contrastmatrixmarital
children<-as.factor(c("none","one","two"))
contrastmatrixchildren<-cbind(c(1.224745,0,-1.224745),c(-0.707107,1.414214,-0.707107)) # orthogonal coding for 3-level factors
contrasts(children)<-contrastmatrixchildren
cand.list<-expand.grid("Language" = lang, "Marital Status" = marital, "Children" = children)
formula=~Language+Marital Status+Children+Marital Status:Children
library(AlgDesign)
set.seed(1337)
optB<-optBlock(frml=formula, withinData=cand.list, blocksizes=c(12,12))
set.seed(1337)
optF<-optFederov(frml=formula, data=cand.list, nTrials=12)
optB$D # [1] 1 optF$D
# [1] 0.8735807


I solved my problem. In case anyone struggles with this in the future, here's how the standardized orthogonal contrast coding can be calculated:

To calculate the orthogonal coding, you'll need a column with 1s for the intercept and with the effects coding that is provided by contr.sum. Those two need to be combined to a matrix. After this, all you need to do is orthonomarlize the matrix for example by using orthonormalize of the far library (available from CRAN). In orthonormalized matrix if you calculate the sum of the square of a column, the result will be 1. In case of the standardized orthogonal contrast coding you actually want the columns sum of squares to be n where n is the number of factor-levels. So all you need to do is multiply the orthonormalized matrix by the square root of n. The last step is to remove the first column.

Example for a factor with n=6:

library(far)
ex <- as.factor(c("A","B","C","D","E","F"))
EC <- cbind(c(1,1,1,1,1,1), contr.sum(ex))
SOCC <- orthonormalization(EC)*6^(1/2)
SOCC <- SOCC[,(2:6)]
contrasts(ex) <- SOCC


Which results in:

contrasts(ex)
[,1] [,2]       [,3]       [,4]       [,5]
A  1.732051   -1 -0.7071068 -0.5477226 -0.4472136
B  0.000000    2 -0.7071068 -0.5477226 -0.4472136
C  0.000000    0  2.1213203 -0.5477226 -0.4472136
D  0.000000    0  0.0000000  2.1908902 -0.4472136
E  0.000000    0  0.0000000  0.0000000  2.2360680
F -1.732051   -1 -0.7071068 -0.5477226 -0.4472136