Hi all,
I've been a little perplexed by the different ways to code dummy variables when fitting a linear mixed model (using fitlme). Specifically I have a model with two categorical fixed factors. I'd like to do contrasts between the different levels of each factor. Now several online sources tell me I should use 'effects' coding for this, but it isn't clear to me why this is, nor is it clear to me how I should code my contrast matrix when using 'effects' coding rather than reference coding.
For example, if I have a model with an intercept and one categorical fixed factor with three levels, such that:
T = table(y,var1); % y is my response variable, var1 a categorical variable with three levels
formula = 'y ~ var1';m1 = fitlme(T,formula,'DummyVarCoding', 'reference');me1 = fitlme(T,formula,'DummyVarCoding', 'effects');
Now I'd like to test whether there's a difference between the first and second categories. In the case of the m1 (reference coding) and me1 (effects coding) I would do this as follows:
[p,F,df1,df2] = coefTest(m1,[0 1 0]); % coefTest(lme,H)
[pe,Fe,df1e,df2e] = coefTest(me1,[1 1 0]);
This gives me very different results even though in both cases multiplying each contrast matrix with the associated fixed effects matrix gives me the same value (according to the documentation for coefTest: "It tests the null hypothesis that H0: Hβ = 0, where β is the fixed-effects vector."
When I try both options with some simulated data, where the first two levels of the fixed factor differ significantly, I only find this significant difference when using reference coding. Any insight would be greatly appreciated.
Here's the code for some simulated data:
d1 = zeros(100,1)+randn(100,1);d2 = ones(100,1)+randn(100,1);d3 = ones(100,1).*2+randn(100,1);d = [d1;d2;d3];cov1 = [zeros(100,1);ones(100,1);ones(100,1).*2];T = table(d,categorical(cov1),'VariableNames',{'d','cov1'});f = 'd~cov1';m1 = fitlme(T,f);me1 = fitlme(T,f,'DummyVarCoding','effects');[p,F,df1,df2] = coefTest(m1,[0 1 0]); % coefTest(lme,H)[pe,Fe,df1e,df2e] = coefTest(me1,[1 1 0]);
Best Answer