Matlab boxplot adjacent values












1














I found that calculating an index to specify outliers of a dataset according to how the boxplot works does not give the same results. Please find below an example where I create some data, extract the values from the boxplot (as seen in datatips in the figure window) and compare them to the values I calculated.



While the median and quartiles match up the upper and lower adjacent values do not. According to the Matlab help under 'Whisker', the adjacent values are calculated as q3 + w*(q3-q1) where q3 and q1 are the quantiles and w is the specified whisker length.



Am I calculating this wrong or is there any other mistake? I would like to be able to explain the error.



Screenshot of results table (please note the results vary due to random data)



%Boxplot test

% create random, normally distributed dataset
data = round(randn(1000,1)*10,2);

figure(10)
clf
boxplot(data,'Whisker',1.5)

clear stats tmp

% read data from boxplot, same values as can be seen in datatips in the figure window
h = findobj(gcf,'tag','Median');
tmp = get(h,'YData');
stats(1,1) = tmp(1);
h = findobj(gcf,'tag','Box');
tmp = get(h,'YData');
stats(1,2) = tmp(1);
stats(1,3) = tmp(2);
h = findobj(gcf,'tag','Upper Adjacent Value');
tmp = get(h,'YData');
stats(1,4) = tmp(1);
h = findobj(gcf,'tag','Lower Adjacent Value');
tmp = get(h,'YData');
stats(1,5) = tmp(1);

% calculated data
stats(2,1) = median(data);
stats(2,2) = quantile(data,0.25);
stats(2,3) = quantile(data,0.75);
range = stats(2,3) - stats(2,2);
stats(2,4) = stats(2,3) + 1.5*range;
stats(2,5) = stats(2,2) - 1.5*range;

% error calculation
for k=1:size(stats,2)
stats(3,k) = stats(2,k)-stats(1,k);
end %for k

% convert results to table with labels
T = array2table(stats,'VariableNames',{'Median','P25','P75','Upper','Lower'}, ...
'RowNames',{'Boxplot','Calculation','Error'});









share|improve this question





























    1














    I found that calculating an index to specify outliers of a dataset according to how the boxplot works does not give the same results. Please find below an example where I create some data, extract the values from the boxplot (as seen in datatips in the figure window) and compare them to the values I calculated.



    While the median and quartiles match up the upper and lower adjacent values do not. According to the Matlab help under 'Whisker', the adjacent values are calculated as q3 + w*(q3-q1) where q3 and q1 are the quantiles and w is the specified whisker length.



    Am I calculating this wrong or is there any other mistake? I would like to be able to explain the error.



    Screenshot of results table (please note the results vary due to random data)



    %Boxplot test

    % create random, normally distributed dataset
    data = round(randn(1000,1)*10,2);

    figure(10)
    clf
    boxplot(data,'Whisker',1.5)

    clear stats tmp

    % read data from boxplot, same values as can be seen in datatips in the figure window
    h = findobj(gcf,'tag','Median');
    tmp = get(h,'YData');
    stats(1,1) = tmp(1);
    h = findobj(gcf,'tag','Box');
    tmp = get(h,'YData');
    stats(1,2) = tmp(1);
    stats(1,3) = tmp(2);
    h = findobj(gcf,'tag','Upper Adjacent Value');
    tmp = get(h,'YData');
    stats(1,4) = tmp(1);
    h = findobj(gcf,'tag','Lower Adjacent Value');
    tmp = get(h,'YData');
    stats(1,5) = tmp(1);

    % calculated data
    stats(2,1) = median(data);
    stats(2,2) = quantile(data,0.25);
    stats(2,3) = quantile(data,0.75);
    range = stats(2,3) - stats(2,2);
    stats(2,4) = stats(2,3) + 1.5*range;
    stats(2,5) = stats(2,2) - 1.5*range;

    % error calculation
    for k=1:size(stats,2)
    stats(3,k) = stats(2,k)-stats(1,k);
    end %for k

    % convert results to table with labels
    T = array2table(stats,'VariableNames',{'Median','P25','P75','Upper','Lower'}, ...
    'RowNames',{'Boxplot','Calculation','Error'});









    share|improve this question



























      1












      1








      1







      I found that calculating an index to specify outliers of a dataset according to how the boxplot works does not give the same results. Please find below an example where I create some data, extract the values from the boxplot (as seen in datatips in the figure window) and compare them to the values I calculated.



      While the median and quartiles match up the upper and lower adjacent values do not. According to the Matlab help under 'Whisker', the adjacent values are calculated as q3 + w*(q3-q1) where q3 and q1 are the quantiles and w is the specified whisker length.



      Am I calculating this wrong or is there any other mistake? I would like to be able to explain the error.



      Screenshot of results table (please note the results vary due to random data)



      %Boxplot test

      % create random, normally distributed dataset
      data = round(randn(1000,1)*10,2);

      figure(10)
      clf
      boxplot(data,'Whisker',1.5)

      clear stats tmp

      % read data from boxplot, same values as can be seen in datatips in the figure window
      h = findobj(gcf,'tag','Median');
      tmp = get(h,'YData');
      stats(1,1) = tmp(1);
      h = findobj(gcf,'tag','Box');
      tmp = get(h,'YData');
      stats(1,2) = tmp(1);
      stats(1,3) = tmp(2);
      h = findobj(gcf,'tag','Upper Adjacent Value');
      tmp = get(h,'YData');
      stats(1,4) = tmp(1);
      h = findobj(gcf,'tag','Lower Adjacent Value');
      tmp = get(h,'YData');
      stats(1,5) = tmp(1);

      % calculated data
      stats(2,1) = median(data);
      stats(2,2) = quantile(data,0.25);
      stats(2,3) = quantile(data,0.75);
      range = stats(2,3) - stats(2,2);
      stats(2,4) = stats(2,3) + 1.5*range;
      stats(2,5) = stats(2,2) - 1.5*range;

      % error calculation
      for k=1:size(stats,2)
      stats(3,k) = stats(2,k)-stats(1,k);
      end %for k

      % convert results to table with labels
      T = array2table(stats,'VariableNames',{'Median','P25','P75','Upper','Lower'}, ...
      'RowNames',{'Boxplot','Calculation','Error'});









      share|improve this question















      I found that calculating an index to specify outliers of a dataset according to how the boxplot works does not give the same results. Please find below an example where I create some data, extract the values from the boxplot (as seen in datatips in the figure window) and compare them to the values I calculated.



      While the median and quartiles match up the upper and lower adjacent values do not. According to the Matlab help under 'Whisker', the adjacent values are calculated as q3 + w*(q3-q1) where q3 and q1 are the quantiles and w is the specified whisker length.



      Am I calculating this wrong or is there any other mistake? I would like to be able to explain the error.



      Screenshot of results table (please note the results vary due to random data)



      %Boxplot test

      % create random, normally distributed dataset
      data = round(randn(1000,1)*10,2);

      figure(10)
      clf
      boxplot(data,'Whisker',1.5)

      clear stats tmp

      % read data from boxplot, same values as can be seen in datatips in the figure window
      h = findobj(gcf,'tag','Median');
      tmp = get(h,'YData');
      stats(1,1) = tmp(1);
      h = findobj(gcf,'tag','Box');
      tmp = get(h,'YData');
      stats(1,2) = tmp(1);
      stats(1,3) = tmp(2);
      h = findobj(gcf,'tag','Upper Adjacent Value');
      tmp = get(h,'YData');
      stats(1,4) = tmp(1);
      h = findobj(gcf,'tag','Lower Adjacent Value');
      tmp = get(h,'YData');
      stats(1,5) = tmp(1);

      % calculated data
      stats(2,1) = median(data);
      stats(2,2) = quantile(data,0.25);
      stats(2,3) = quantile(data,0.75);
      range = stats(2,3) - stats(2,2);
      stats(2,4) = stats(2,3) + 1.5*range;
      stats(2,5) = stats(2,2) - 1.5*range;

      % error calculation
      for k=1:size(stats,2)
      stats(3,k) = stats(2,k)-stats(1,k);
      end %for k

      % convert results to table with labels
      T = array2table(stats,'VariableNames',{'Median','P25','P75','Upper','Lower'}, ...
      'RowNames',{'Boxplot','Calculation','Error'});






      matlab statistics boxplot






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 9:42

























      asked Nov 20 '18 at 12:51









      Lukas W.

      166




      166
























          1 Answer
          1






          active

          oldest

          votes


















          1














          While the calculation of the boundaries, e.g. q3 = q3 + w*(q3-q1), is correct, it is not displayed in the boxplot. What is actually displayed and marked as upper/lower adjacent value is the minimum and maximum of the values within the aforementioned boundaries.



          Regarding the initial task leading to the question: For applying the same filtering of outliers as in the boxplot the calculated boundaries can be used.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393404%2fmatlab-boxplot-adjacent-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            While the calculation of the boundaries, e.g. q3 = q3 + w*(q3-q1), is correct, it is not displayed in the boxplot. What is actually displayed and marked as upper/lower adjacent value is the minimum and maximum of the values within the aforementioned boundaries.



            Regarding the initial task leading to the question: For applying the same filtering of outliers as in the boxplot the calculated boundaries can be used.






            share|improve this answer


























              1














              While the calculation of the boundaries, e.g. q3 = q3 + w*(q3-q1), is correct, it is not displayed in the boxplot. What is actually displayed and marked as upper/lower adjacent value is the minimum and maximum of the values within the aforementioned boundaries.



              Regarding the initial task leading to the question: For applying the same filtering of outliers as in the boxplot the calculated boundaries can be used.






              share|improve this answer
























                1












                1








                1






                While the calculation of the boundaries, e.g. q3 = q3 + w*(q3-q1), is correct, it is not displayed in the boxplot. What is actually displayed and marked as upper/lower adjacent value is the minimum and maximum of the values within the aforementioned boundaries.



                Regarding the initial task leading to the question: For applying the same filtering of outliers as in the boxplot the calculated boundaries can be used.






                share|improve this answer












                While the calculation of the boundaries, e.g. q3 = q3 + w*(q3-q1), is correct, it is not displayed in the boxplot. What is actually displayed and marked as upper/lower adjacent value is the minimum and maximum of the values within the aforementioned boundaries.



                Regarding the initial task leading to the question: For applying the same filtering of outliers as in the boxplot the calculated boundaries can be used.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 12:50









                Lukas W.

                166




                166






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393404%2fmatlab-boxplot-adjacent-values%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]