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
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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.