I am trying to understand how to set up contrasts for models with interaction effects that go up to 4-way interactions. As an example, I am using a 3-way interaction that comprises: Gender (2 levels), Type (2 levels) and Usability (2 levels). Let us assume that the interaction and the corresponding "main" effects are significant.
The fitted model is with effect coding as follows:
formula = 'Counts ~ Usability*Gender*Type + (1|Participant)';
mdl = fitglme(t, formula, 'Distribution','Poisson', ...
'DummyVarCoding','effects','Link','log','FitMethod','Laplace');
In order to figure out how each level combination of all variables work, I am taking the design matrix of the model itself:
variableLevels = unique(mdl.Variables(:,3:end),'stable','rows');
designMat = splitvars(table(unique(designMatrix(mdl),'stable','rows')));
designMat = renamevars(designMat, designMat.Properties.VariableNames, mdl.CoefficientNames);
contrastTable = [variableLevels, designMat];
This is an exemplary table as output (table shows fine/aligned in the thread editor, but not when posted, apologies):
'Usability_A' 'Female' 'Rotated' 1 1 1 1 1 1 1 1
'Usability_A' 'Female' 'Straight' 1 1 1 -1 1 -1 -1 -1
'Usability_A' 'Male' 'Rotated' 1 1 -1 1 -1 1 -1 -1
'Usability_A' 'Male' 'Straight' 1 1 -1 -1 -1 -1 1 1
'Usability_B' 'Female' 'Rotated' 1 -1 1 1 -1 -1 1 -1
'Usability_B' 'Female' 'Straight' 1 -1 1 -1 -1 1 -1 1
'Usability_B' 'Male' 'Rotated' 1 -1 -1 1 1 -1 -1 1
'Usability_B' 'Male' 'Straight' 1 -1 -1 -1 1 1 1 -1
To perform a contrast among e.g., Rotated versus Straight for Males and Usability_B, I perform the following:
L = table2array(contrastTable(7,4:end))-table2array(contrastTable(8,4:end));
[pVal,F,DF1,DF2] = coefTest(mdl,L)
On the other hand, I would like to know whether Female versus Male (regardless of Usability and Type) have any differences on the response variable Counts. Would I have to aggregate all entries for male and female into two matrices and then subtract them from each other?
Can I ask whether this is a correct approach? Any help will be appreciated!