multcompare and anovan with continuous group variable

4 views (last 30 days)
Hi everyone,
I am a bit confused about how to use multcompare and anovan, when one of my variables is continuous: y = [3.2184 4.2694 4.2562 4.7992 5.3111 6.0386];
g1 = {'file 1', 'file 1', 'file 1', 'file 2', 'file 2', 'file 2'};
g2 = [1.0000 1.1000 1.3400 1.3100 1.4100 1.6000];
[p1,table1,stats1] = anovan(totmean(1,(1:10))' , {g1 g2}, 'continuous', (2),'model','interaction');
I am supposed to use parameter 'dimension' :
[c,m,h,nms] = multcompare(stats1, 'dimension', 2)
The problem is that when I run it, I get:
Error using multcompare (line 245)
DIM must specify only categorical factors with 2 or more degrees of freedom.
Could anyone help me with that?
Thank you!!

Answers (1)

Aditya
Aditya on 22 Jul 2025
Hi Christina,
The error you encountered arises because the multcompare function in MATLAB is intended for making pairwise comparisons between the levels of a categorical factor, not a continuous one. In your example, g1 is a categorical variable (with levels like 'file 1' and 'file 2'), while g2 is a continuous variable. When you run multcompare(stats1, 'dimension', 2), MATLAB throws an error because the second factor is continuous and does not have discrete levels for pairwise comparison.
If you want to use multcompare to compare group means, you should only specify dimensions corresponding to categorical variables. In your case, you can compare across the levels of g1 like this:
[c, m, h, nms] = multcompare(stats1, 'dimension', 1);
This will give you pairwise comparisons between the groups defined by g1 (e.g., 'file 1' vs 'file 2'), taking into account the continuous covariate g2 as specified in your ANOVA model.
If you are interested in the effect of the continuous variable g2, you cannot use multcompare for this purpose. Instead, you should interpret the main effect or interaction terms related to g2 from the ANOVA table output, or use regression analysis to further explore the relationship between y and g2. For example, you can use fitlm to fit a linear model including both your categorical and continuous predictors:
tbl = table(y', categorical(g1)', g2', 'VariableNames', {'y','g1','g2'});
lm = fitlm(tbl, 'y ~ g1 + g2 + g1:g2');
disp(lm)
This approach will allow you to assess the significance and effect size of your continuous variable, as well as any interaction with the categorical variable. To visualize the relationship between y and g2, you might also plot them:
scatter(g2, y)
xlabel('g2 (continuous variable)')
ylabel('y')
title('Scatter plot of y vs. g2')

Community Treasure Hunt

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

Start Hunting!