How to do 'effects coding' in Matlab

5 Ansichten (letzte 30 Tage)
Tobias Averbeck
Tobias Averbeck am 2 Mär. 2020
Kommentiert: Matthew am 12 Jul. 2023
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.

Akzeptierte Antwort

Tobias Averbeck
Tobias Averbeck am 3 Mär. 2020
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');
  1 Kommentar
Matthew
Matthew am 12 Jul. 2023
I had a general question about using effects coding in LMEs. Say I had 3 categorical variables I wanted to run effects coding on - running fitlme only returns the intercept and 2 of those 3 variables. How do I view the last one?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by