How to do 'effects coding' in Matlab

조회 수: 5 (최근 30일)
Tobias Averbeck
Tobias Averbeck 2020년 3월 2일
댓글: Matthew 2023년 7월 12일
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.

채택된 답변

Tobias Averbeck
Tobias Averbeck 2020년 3월 3일
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
Matthew
Matthew 2023년 7월 12일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by