1
$\begingroup$

I'm new to Mathematica and started using it because of interest in statistics. So I've started reading books about statistics and found the problem, that the inverted empirical CDF with averaging method (http://mathworld.wolfram.com/Quantile.html) - as used in my books - was not supported by Mathematica including BoxWhiskerChart.

This was a great training for me to test my understanding and here is the result open for comments and improvements:

(* Define first the inverted empirical CDF with averaging function \
QUANTILEIECDAVE *)
ClearAll[myIF]
SetAttributes[myIF, HoldAll]
myIF[cond_, iftrue_, iffalse_] := 
  With[{result = 
     Replace[cond, {True :> iftrue, False :> iffalse, _ :> None}]}, 
   result /; result =!= None];
QUANTILEIECDAVE[x_, q_] := 
  Module[{s = Sort[x], n = Length[x]}, 
   myIF[IntegerQ[n*#], 1/2 (s[[n*#]] + s[[n*# + 1]]), 
      s[[Ceiling[n*#]]]] & /@ q];

(* For the BoxWhiskerChart we need a new function BOXWHISKERIECDAVE \
for drawing the box *)
IQR[x_] := First[Quantile[x, {3/4}, {{0, 1}, {0, 0}}] - 
  Quantile[x, {1/4}, {{0, 1}, {0, 0}}]]
MinIQR[x_] := First@Select[Sort[x], # >= Median[x] - IQR[x]*3/2 &, 1];
MaxIQR[x_] := 
  First@Select[Sort[x, Greater], # <= Median[x] + IQR[x]*3/2 &, 1];
BOXWHISKERIECDAVE[x_] := 
  Flatten[{MinIQR[x], QUANTILEIECDAVE[x, {1/4, 2/4, 3/4}], MaxIQR[x]}];

(* For the BoxWhiskerChart we need to find out the outliers with 3/2 \
boundries around IQR *)
OUTLIERS[l_] := 
  Module[{n = MinIQR[l], m = MaxIQR[l]}, 
   l /. x_ /; (n <= x <= m) -> None];

(* We have a function BOXWHISKERLABEL to overwrite the standard \
tooltip with our new values *)
BOXWHISKERLABEL[data_, index_, label_] := 
  Grid[{{Style["max", Bold], #1[[5]]}, {Style["75%", 
        Bold], #1[[4]]}, {Style["median", Bold], #1[[3]]}, {Style[
        "25%", Bold], #1[[2]]}, {Style["min", Bold], #1[[1]]}}, 
     Dividers -> {{#, #}, {#, #}} &@{Directive[GrayLevel[0.3]], 
       Directive[GrayLevel[0.3]]}, 
     Alignment -> {{Center, ".", {Left}}}, Frame -> GrayLevel[0], 
     BaseStyle -> Directive[AbsoluteThickness[1], Dashing[{}]]] &@
   BOXWHISKERIECDAVE[data];

(* Start drawing our new BoxWhiskerChart with outliers *)
t = List[{4, 4, 1, 5, 3, 3, 1, 3, 4, 5, 6, 9, 1}, {3, 4, 6, 4, 8, 9, 
    2, 7, 10, 7, 5, 6, 5}, Range[1, 10]];
ol = OUTLIERS /@ t;
Show[BoxWhiskerChart[t, ChartLabels -> {"t1", "t2", "t3"},
  Method -> "BoxRange" -> (BOXWHISKERIECDAVE[#] &),
  GridLines -> {None, Range[0, 10, 1]},
  LabelingFunction -> (Placed[BOXWHISKERLABEL[##], Tooltip] &),
  ChartLabels -> Placed[Range[2005, 2009], None]],
 ListPlot[MapIndexed[{#2[[1]], #1} &, ol, {2}]]]

enter image description here

Findings:

  1. Changing the Method -> "BoxRange" does not work with Outliers style of BoxWhiskerChart anymore so I've to use the Show function to overlay the outliers myself
  2. The tooltip does not accept the new values returned by the function defined with Method -> "BoxRange" so I've had to change that too
$\endgroup$
  • $\begingroup$ This seems an interesting piece of work, but I don't see how it is a question for this site. Perhaps you could rephrase part of it as a question and then post the rest as a self-question? $\endgroup$ – m_goldberg Dec 28 '15 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.