MATLAB: How to do ‘effects coding’ in Matlab

doelinear mixed-effects model

Hi, right now I am writing a program for 'design of experiments' and I need to calculate the effects matrix for my factors and levels. The matrix should look like this with white = 1, grey = 0, black = − 1.
The only functions I found regarding effects coding in Matlab are 'fitlme' and 'fitglme', but I really don't understand how they work and how I get an effects matrix from this (like in the picture). For example how would that work for a 2^9 design plan?
Thanks in advance.

Best Answer

So, I found a solution, you have to use categorical input to get the -1,0,1 effects matrix, this is the code I used:
% Define the levels of the factors and size of design
level = [4 4 6 6];
reduced_design_size = 300;
% Construct the reduced design
[D,~] = rowexch(size(level,2),reduced_design_size,...
'linear','categorical',[1:size(level,2)],'levels',level);
% This is just to have a numerical response column for the
% formula of the lme
D(:,5) = randperm(size(D,1))';
D_tbl = mat2dataset(D);
% Convert the factors to 'categorical' to get a -1,0,1 effects matrix
D_tbl.D1 = nominal(D_tbl.D1);
D_tbl.D2 = nominal(D_tbl.D2);
D_tbl.D3 = nominal(D_tbl.D3);
D_tbl.D4 = nominal(D_tbl.D4);
% Calculate LME with just addition of factors as formula
% and response as the added column
lme = fitlme(D_tbl,'D5 ~ D1 + D2 + D3 + D4',...
'FitMethod','REML','DummyVarCoding','effects');
% Extract effects matrix
DM = designMatrix(lme,'Fixed');
% sort for better overview
DM = sortrows(DM,[1:size(DM,2)],'descend');