How can I include Tukey (HSD) result in my boxlpot as "a" and/or "b" indicating whether there is a significant difference or not between the groups?

15 views (last 30 days)
Dear all,
in the following you can find my script. When I run my script I get two graphics, one graphic shows the result of Tukeys HSD analysis and the other graphic shos the boxplot .
My aim is to include the Tukey result in the boxplot, using letters to indicate significant differences. in this case there should appear the letter "a" above sample 1, and a "b" above the samples 2 and sample 3.
Thanks for any ideas or help!
Test1 = [
36.4000 40.6000 40.3000
31.5000 39.7000 41.0000
25.4000 40.8000 45.7000
27.5000 35.8000 43.4000
29.4000 37.0000 40.2000
30.3000 42.6000 39.5000
28.8000 41.8000 42.7000
32.3000 45.4000 39.1000
22.8000 39.5000 38.8000
27.6000 46.2000 40.6000
24.4000 52.4000 41.4000
19.5000 38.5000 41.9000
27.7000 42.8000 37.4000
19.6000 45.5000 32.3000
28.2000 44.3000 36.1000];
V=Test1;
wt=V(:, 1);
T3=V(:, 2);
T2=V(:, 3);
neuwt=V(:,1)';
neuT3=V(:,2)';
neuT2=V(:,3)';
data(:,1) = neuwt;
data(:,2) = neuT3;
data(:,3) = neuT2;
[p, table, stats] = anova1(data);
[c, a, h, nmn] = multcompare(stats, 'alpha', 0.05, 'ctype','hsd')
boxplot(Test1)
  2 Comments
Abhishek Chakraborty
Abhishek Chakraborty on 1 Jun 2020
Sorry for a very naive question but how did you get to interpret the letters to be "a" and "b" for your samples through the MATLAB results?
dpb
dpb on 1 Jun 2020
That was just an arbitrary naming convention OP wanted to place on the significant versus nonsignificant results. Those that are significantly different are those whose bars are disjoint; by inspection one observes the LH is not overlapping either of the other two while the two RH are. Hence labelling them together and the other differently.
The p-statistic is returned in the c array from multcompare to see the actual probability estimate.

Sign in to comment.

Accepted Answer

dpb
dpb on 4 Aug 2019
Edited: dpb on 1 Jun 2020
Kinda' a fun one... :)
First, make sure your boxplot is gca, then the following seems to be pretty robust...the one probably unexpected thing is the handles are in reverse order from R to L on the plot in the handles array of lines, hence the flip on the labels index.
hBOX=gca;
hold on
hGbox=hBOX.Children
hUpW=findobj(hGbox.Children,'Tag','Upper Whisker');
labels={'a';'b';'b'};
hTxt=arrayfun(@(h,i) text(h.XData(1),h.YData(2),labels(i), ...
'verticalalignment','bottom','HorizontalAlignment','center'), ...
hUpW,flip([1:numel(hUpW)].');
I just created a labels array by hand, obviously by parsing the returned means and probabilities one could generate the groups and labels programmatically.
It is strange why you built so many intermediary results of the same values, though???
I can see why created a test array for posting question, but if you once have the array V, then
wt=V(:, 1);
T3=V(:, 2);
T2=V(:, 3);
neuwt=V(:,1)';
neuT3=V(:,2)';
neuT2=V(:,3)';
data(:,1) = neuwt;
data(:,2) = neuT3;
data(:,3) = neuT2;
is all just superfluous -- all(V(:)==data(:)) --> 1 (true) so just use V
[p, table, stats] = anova1(V);
[c, a, h, nmn] = multcompare(stats, 'alpha', 0.05, 'ctype','hsd')
boxplot(V)
and generate the same results.
  5 Comments
dpb
dpb on 9 Aug 2019
Huh. Don't know how that [ got misplaced--I'd'a swore I cut 'n pasted from the command line that did work. Sorry about that. What I used and intended to post was
hTxt=arrayfun(@(h,i) text(h.XData(1),h.YData(2),labels(i), ...
'verticalalignment','bottom','HorizontalAlignment','center'), ...
hUpW,flip([1:numel(hUpW)].'));
where the [] were just cosmetic to surround the 1:N vector to transpose it to column. Not really needed.
Like much of ML, once you see something once, it's not nearly as mysterious as seems on first blush! :)
Valerie
Valerie on 11 Aug 2019
Yes, indeed! But it can take so much time until that point where it all makes sense (and until it works the way it should :D). Thanks so much for your help!

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!