# AFNI and NIfTI Server for NIMH/NIH/PHS/DHHS/USA/Earth

##### Personal tools
You are here: Home » SSCC » How to contruct matrices for main effects and interactions

# How to contruct matrices for main effects and interactions

##### Document Actions

Mathematically speaking there are infinite matrices for one specific effect testing. Here we provide one systematic approach to constructing matrices (or vectors under special circumstances) for main effects and interactions which can be used to set up general linear testing with -glt or -gltsym in 3dDeconvolve.

Using a 3x3 design as an example, we first construct common effect vectors for all the factors (overall mean), A and B in this case. The number of elements in the vectors corresponds to the number of levels for this factor:

A1 = [1 1 1]
B1 = [1 1 1]

Then define differential effects (pairwise comparisons) for all the factors. The number of columns in the matrices corresponds to the number of levels for this factor, and the number of rows equals to the number of levels for this factor minus 1. It's intuitive to interpret those 1's, -1's, and 0s in the matrices.

A2 =
[1 -1 0
0 1 -1]

B2 =
[1 -1 0
0 1 -1]

The main effects and interaction for A and B are the Kronecker products. ⊗ is the Kronecker product in mathematical notation.

A main effect
A2 ⊗ B1 =
[1 1 1 -1 -1 -1 0 0 0
0 0 0 1 1 1 -1 -1 -1]

B main effect
A1 ⊗ B2=
[1 -1 0 1 -1 0 1 -1 0
0 1 -1 0 1 -1 0 1 -1]

Interaction
A2 ⊗ B2 =
[1 -1 0 -1 1 0 0 0 0
0 1 -1 0 -1 1 0 0 0
0 0 0 1 -1 0 -1 1 0
0 0 0 0 1 -1 0 -1 1]

How about three-way interaction? Suppose that we have a third factor, C, with four levels. If we define

C3 =
[1 -1 0 0
0 1 -1 0
0 0 1 -1]

the matrix for the three-way interaction can be obtained through

A2 ⊗ B2 ⊗ C3

The kronecker function in R (or kron() in Matlab) can be used to calculate the above matrices.

(not specified) 