fitglme/coefTest contrasts

17 views (last 30 days)
LM_BU
LM_BU on 17 Feb 2024
Answered: Garmit Pant on 29 Feb 2024
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'); % Skips Participant and Counts columns
designMat = splitvars(table(unique(designMatrix(mdl),'stable','rows'))); % Finds unique coding of each factor-level combination
designMat = renamevars(designMat, designMat.Properties.VariableNames, mdl.CoefficientNames); % Adds coefficient names on top for clarity
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)); % 7th row: Usability_B&Male&Rotated; 8th: ...&Straight
[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!

Answers (1)

Garmit Pant
Garmit Pant on 29 Feb 2024
Hello LM_BU
From what I gather, you are working with generalised linear mixed-effects model and you are performing hypothesis testing for the model that has a 3 way interaction between variables that have 2 levels each. You want to understand how to set up contrast vectors for your hypothesis.
Given that you are using effects coding, the main effects of the different variables will be coded as [1 -1]. To set up contrasts for a particular hypothesis, we need to consider only the interactions that focus on the coefficients with the variables that we need. You can find the method to set up contrasts for the two hypotheses mentioned by you below.
Hypothesis 1: Rotated versus Straight for Males and Usability_B:
  • We’ll only consider the effects of the the variable “Type” with the “Male” and “Usability_B” levels. Thus, the contrast set up by you for the case is accurate.
Hypothesis 2: Female versus Male (regardless of Usability and Type)
  • Initialize the contrast vector with zeros. The length of the vector should match the number of fixed effects coefficients in the model, which is 7.
  • Set the value for the "Gender" coefficient in the contrast vector.
  • The "Gender" coefficient is in the 3rd position of the fixed effects.
  • The value for the "Gender" coefficient in the contrast vector is "8", which is the sum of the "Gender" coefficients for "Female" (1+1+1+1) minus the sum for "Male" (-1-1-1-1).
  • We do not need to divide by the number of combinations (4) because "coefTest" will handle the scaling appropriately.
  • The interaction terms involving "Gender" ("Usability:Gender", "Type:Gender", "Usability:Type:Gender") will sum to zero and are not included in the contrast vector for this main effect test.
  • The resulting contrast vector will have the value "8" in the 3rd position and "0" in all other positions.
L_gender = zeros(1, 7);
L_gender(3) = 1+1+1+1 - (-1-1-1-1);
For further understanding on the methods mentioned above, you can refer to the following MATLAB Documentation:
  1. GeneralizedLinearMixedModelclass: https://www.mathworks.com/help/stats/generalizedlinearmixedmodel-class.html
  2. coefTest” function. Navigate to the “Input Arguments” section to read about fixed-effects contrasts: https://www.mathworks.com/help/stats/generalizedlinearmixedmodel.coeftest.html
I hope you find the above explanation and suggestions useful!

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!