1
$\begingroup$

The statistics book I am using defines quartiles the same as Method 1 explained in wikipedia.

Use the median to divide the ordered data set into two-halves. If there is an odd number of data points in the original ordered data set, do not include the median (the central value in the ordered list) in either half. If there is an even number of data points in the original ordered data set, split this data set exactly in half. The lower quartile value is the median of the lower half of the data. The upper quartile value is the median of the upper half of the data. This rule is employed by the TI-83 calculator boxplot and "1-Var Stats" functions.

For the following illustration, the numbers with yellow background represent the data while the curly braced three numbers represent their quartiles.

enter image description here

As Quartiles defined in Mathematica uses different algorithm, I create my own implementing the Method 1 above as follows.

CorrectedQuartile[raw_List] := Module[{data, length, Q1, Q2, Q3},
   data = Sort@raw;
   length = Length@data;
   Q2 = Median@data;
   If[Mod[length, 2] == 0,
    Q1 = Median@Take[data, length/2];
    Q3 = Median@Take[data, -length/2],
    Q1 = Median@Take[data, (length - 1)/2];
    Q3 = Median@Take[data, (1 - length)/2]
    ];
   {Q1, Q2, Q3}
   ];

Question

My rule of thumb is "Writing our own functions must be the last resort". Is it possible to use the existing or built-in Quartiles to behaves the same as the Method 1 does?

$\endgroup$
  • $\begingroup$ The online help for Quantile gives 8 different definitions one of which almost certainly matches the Texas Instruments approach. It also gives a general formula to be able to customize however you want. I would expect this question to be closed. $\endgroup$ – JimB Jun 29 at 15:16
  • $\begingroup$ It is very hard to choose the values for {{a,b},{c,d}}. $\endgroup$ – Too Fat Man No Neck Jun 29 at 15:51
  • 1
    $\begingroup$ Here's another "last resort": tiQuartiles[list_] := With[{med = Median[list]}, Riffle[Median /@ Lookup[GroupBy[list, Sign[# - med] &], {-1, 1}], med]]; $\endgroup$ – Michael E2 Jun 29 at 16:07
5
$\begingroup$

Update

I realized that different sets of parameters can be used:

corQuartiles[d_?ListQ] := Quantile[d, {1/4, 1/2, 3/4},
  {If[EvenQ[Length[d]], {1/2, 0}, {0, 1}], {0, 1}}]

Test:

# -> corQuartiles[#] & /@ Range /@ Range[2, 7]
{{1, 2} -> {1, 3/2, 2}, {1, 2, 3} -> {1, 2, 3}, {1, 2, 3, 4} -> {3/2, 
   5/2, 7/2}, {1, 2, 3, 4, 5} -> {3/2, 3, 9/2}, {1, 2, 3, 4, 5, 
   6} -> {2, 7/2, 5}, {1, 2, 3, 4, 5, 6, 7} -> {2, 4, 6}}

Original answer

I've looked into the third parameter {{a,b},{c,d}} of Quantile but it seems that they cannot satisfy OP's demand. Maybe you still need to write it by yourself, like:

corQuartiles[d_?ListQ] := With[{n = Length[d]},
  If[EvenQ[n],
   Quartiles[d],
   Median /@ TakeList[Sort[d], {(n - 1)/2, 1, All}]
  ]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ How did you determine the "confusing" parameters? $\endgroup$ – Too Fat Man No Neck Jun 29 at 16:44
  • $\begingroup$ @TooFatManNoNeck (1) {a,b} is obtained by solving $x=a+(n+b)q$ (documentation), since your illustration actually gives a series of $x_{n,q}$; (2)For linear interpolation between elements (instead of a certain element), we have {c,d}={0,1}. $\endgroup$ – SneezeFor16Min Jun 29 at 16:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.