Multcompare for fitlm output or something similar?
8 次查看(过去 30 天)
显示 更早的评论
Dear Matlab community,
I am running an ANCOVA model in matlab. I have a Y variable, and an X variable, a Group variable (3 groups), and 2 covariates I would like to account for.
My ultimate goal would be to 1. see if there is a general relationship between Y and X and 2. if the slopes between Y and X differ between the 3 groups, while controlling for the 2 covariates.
I thus ended up using: mymodel = fitlm(mytable, 'Y~CV1 + CV2 + X*G').
I end up with a significant model based on the F and p value, and when running anova(mymodel), the X*G term has a significant p-value, which from my understanding of the AOCTOOL tutorial, means that the slopes are different between the three 3 groups.
However, since AOCTOOL only allows for one covariate, I ended up using the fitlm function, which doesn't return a stats object which I can use multcompare with, to answer the question as to how the slope of groups differ (e.g. which one is statistically bigger compared to the other etc.).
Can anyone provide information as to how I can find out how the slopes differ in the context of my model?
Thank you,
Paul
0 个评论
回答(1 个)
Aditya
2025-8-20
Hi Paul,
You are correct that a significant X*G interaction in your model (Y ~ CV1 + CV2 + X*G) indicates that the slope of Y versus X is not the same across your three groups, after accounting for the covariates. However, to determine which groups differ in slope (e.g., is the slope in Group 1 different from Group 2, etc.), you need to perform post-hoc tests on the interaction terms.
MATLAB’s fitlm encodes categorical variables using dummy variables, so the model coefficients represent the slope for the reference group and the differences in slope for the other groups relative to that reference. To compare slopes between any two groups, you can use the coefTest function to test linear combinations of the coefficients corresponding to the interaction terms. For example, to test whether the slope in Group 2 differs from Group 3, you would test whether the difference between their interaction coefficients is significantly different from zero. This approach allows you to perform all pairwise comparisons of slopes between groups, even with multiple covariates in the model.
% Fit the model (assuming mytable has variables: Y, X, G, CV1, CV2)
mymodel = fitlm(mytable, 'Y ~ CV1 + CV2 + X*G');
% Get coefficient names to identify indices
coefNames = mymodel.CoefficientNames;
% Find indices for interaction terms (adjust names if your groups are named differently)
idx_XG2 = find(strcmp(coefNames, 'X:G_2')); % e.g., Group 2
idx_XG3 = find(strcmp(coefNames, 'X:G_3')); % e.g., Group 3
% Test if slope in Group 2 differs from Group 3
c = zeros(1, numel(coefNames));
c(idx_XG2) = 1;
c(idx_XG3) = -1;
[p_BvsC, F, df1, df2] = coefTest(mymodel, c);
% Test if slope in Group 1 (reference) differs from Group 2
c2 = zeros(1, numel(coefNames));
c2(idx_XG2) = 1;
[p_AvsB, F, df1, df2] = coefTest(mymodel, c2);
% Test if slope in Group 1 (reference) differs from Group 3
c3 = zeros(1, numel(coefNames));
c3(idx_XG3) = 1;
[p_AvsC, F, df1, df2] = coefTest(mymodel, c3);
% Display results
fprintf('Group 1 vs Group 2 slope difference p-value: %.4f\n', p_AvsB);
fprintf('Group 1 vs Group 3 slope difference p-value: %.4f\n', p_AvsC);
fprintf('Group 2 vs Group 3 slope difference p-value: %.4f\n', p_BvsC);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Analysis of Variance and Covariance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!