Jump to content

Finding the Interquartile Range (IQR)


MasterACE14

Recommended Posts

Good Evening,

 

I've created a function to get the Range of an array (Highest Value - Lowest Value). Now I'm trying to use this to find the Range of the Upper Quartile(Q3) and the Lower Quartile(Q1) but am not sure as to what is the best/most efficient way of doing this? Should I find the middle value of the array, and then create 2 new arrays, the top half and bottom half, then use the Range function on each of them to get Q3 and Q1 or is there a better way to go about doing this?

       
$x = array();
sort($x);

// calculate median (middle number/value)
        $count = count($x);
        $middleval = floor(($count-1)/2);
            if($count % 2) {
                $median = $x[$middleval];
            } else {
            $low = $x[$middleval];
            $high = $x[$middleval+1];
            $median = (($low+$high)/2);
            }

// calculate range
$min = min($x);
        $max = max($x);
        return $range = $max - $min;

 

any help/suggestions are appreciated! Thank you.

 

Kind Regards,

Ace

Link to comment
https://forums.phpfreaks.com/topic/238068-finding-the-interquartile-range-iqr/
Share on other sites

You were able to figure the median out fairly easily, right? Why not apply that same logic to find the first and third quartiles?

 

Got it! I was over thinking it. Thanks!

    $x = array(7,3,4,6,4,2,7);    
$median = 4;
        $q1 = $median - min($x);
        $q3 = max($x) - $median;
        $iqr = $q3 - $q1;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.