Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Measures.Quartiles: value for Q1 (lower quartile) wrong in QuantileMethod.R #865

Closed
hkoestin opened this issue Sep 13, 2017 · 6 comments
Closed

Comments

@hkoestin
Copy link

hkoestin commented Sep 13, 2017

Hello @cesarsouza

I just found out, that the calculation for the lower quartile (25% quantile) is not working correctly, when passing in QuantileMethod.R as method.

I have the following vector:

var v1 = new double[] { 18, 31, 25, 2, 22, 13, 37, 1, 4, 7, 6, 45, 10, 24, 23, 49, 27, 9, 35, 
                             14, 34, 33, 41, 42, 20, 43, 3, 48, 15, 39, 11, 38, 46, 17, 40, 16, 
                             50, 29, 19, 47, 12, 28, 32, 8, 30, 26, 5, 44, 36, 21};

For this vector, I get the following results for the Q1 value:

  • v1:
    • using Accord: 12.5
    • using R: 13.25

Don't know though, where the difference is coming from.
For some other samples I get correct results.

Cheers, Harald

@cesarsouza
Copy link
Member

Hi @hkoestin,

Thanks for opening the issue! As a workaround, you can sort the values before calling the Quartiles method while also passing alreadySorted as false, as shown below:

double m = Measures.Quartiles(v1.Sorted(), out q1, out q3, alreadySorted: true, type: QuantileMethod.R);

I am still investigating where the problem actually is, but I hope the workaround can solve the issue for you in the meantime!

Thanks again,
Cesar

@cesarsouza
Copy link
Member

By the way, regarding the second vector, I am getting a value of 9.25 in R instead of the indicated 11.25. This matches with the framework once the values have been sorted manually with the workaround above. So I hope the workaround will really work for you, but if you find another case that it fails, please let me know.

Regards,
Cesar

cesarsouza added a commit that referenced this issue Sep 13, 2017
…ment partial sorting;

Updating the Quantile methods to use this method instead of NthElement;

 - Updates GH-865: Measures.Quartiles: value for Q1 (lower quartile) wrong in QuantileMethod.R
@hkoestin
Copy link
Author

@cesarsouza 👍
Thanks for the quick response. I double-checked v2 again. And in fact, it is really 9.25 I get from R and Accord. So no worries about that. I updated the initial issue description.

I will include the fix for sorting the values first. Thanks again!

@hkoestin
Copy link
Author

Did the fix with the code suggestion from above. Works fine now.

If you want to or need them, here is my set of test-vectors for the regression tests against R-compatibility, used to execute xUnit Theories.

First double[] is the input vector.
Second value is Q1.
Third value is Q3.

new object[]
{
    new double?[] {0.0, 1.0, 2.0, 4.0, 5.4, 3.5, 7.8, 8.9, 17.0, 23.78, 98.9, 2.3, 4.5, 6.7, 9.34, 42.42},
    3.2, 11.255
},
new object[]
{
    new double?[] {0.0, 5.4, 2.0, 4.0, 1.0, 3.5, 7.8, 17.0, 8.9, 98.9, 23.78, 2.3, 4.5, 6.7, 42.42, 9.34},
    3.2, 11.255
},
new object[]
{
    new double?[]
    {
        0.0, 1.0, 2.0, 4.0, 5.4, 3.5, 7.8, 8.9, 17.0, 23.78, 98.9, 2.3, 4.5, 6.7, 9.34, 42.42, 23, 17.87, 18.54, 16.23, 15.34,
        19.8723, 23, 24.32
    },
    4.375, 20.65423
},
new object[]
{
    new double?[]
    {
        18, 31, 25, 2, 22, 13, 37, 1, 4, 7, 6, 45, 10, 24, 23, 49, 27, 9, 35, 14, 34, 33, 41, 42, 20, 43, 3, 48, 15, 39, 11, 38,
        46, 17, 40, 16, 50, 29, 19, 47, 12, 28, 32, 8, 30, 26, 5, 44, 36, 21
    },
    13.25, 37.75
},
new object[]
{
    new double?[]
    {
        18, 14, 1, 15, 4, 32, 10, 26, 38, 9, 24, 16, 31, 20, 25, 30, 22, 6, 28, 21, 33, 17, 5, 35, 2, 13, 36, 8, 29, 7
    },
    9.25, 28.75
},
new object[]
{
    new double?[]
    {
        112, 718, 320, 576, 547, 658, 253, 560, 408, 314, 681, 303, 236, 753, 122, 239, 222, 797, 593, 274, 338, 604, 52, 245,
        389
    },
    245, 593
},
new object[]
{
    new double?[]
    {
        209, 556, 317, 571, 219, 599, 568, 516, 582, 279, 298, 319, 614, 290, 458, 262, 281, 606, 513, 519, 356, 338, 525, 576,
        180
    },
    290, 568
}

cesarsouza added a commit that referenced this issue Sep 14, 2017
 - Updates GH-865: Measures.Quartiles: value for Q1 (lower quartile) wrong in QuantileMethod.R
@cesarsouza
Copy link
Member

Thanks @hkoestin!

I've committed a fix to the issue yesterday and it has passed all test vectors above. The fix should be included in the next (pre-)release.

Regards,
Cesar

@cesarsouza
Copy link
Member

Added in 3.8.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants