A part of my work at my day job needed calculating quartiles. I quickly coded a routine to find the required numbers. But when i checked the numbers through excel and then with Minitab. I was astonished, as for the same set of numbers, i was getting different quartiles results in these two software’s.
Which one to trust?
Excel gave a different Q1 and Q3 , while Minitab showed me another. So i googled and found this excellent paper discussing how different software’s calculate quartiles and why they can and are different..
With this clarification, i ended my day and came home. But knowing about this three methods of calculating quartiles, as soon as i reached home, i quickly code this following program to see how this work.
So here is the Fortran program that will calculate quartile in all the 3 methods describe in that document.
program quartile
! Fortran program to test the 3 methods of Quartile Calculation.
! Quartiles are calculated differently in EXCEL, Minitab and SAS method 5
! This program tests the 3 methods.implicit none
interface
subroutine sasm5(x,quart)
real*8 :: x(:),quart
end subroutinesubroutine sasm4(x,quart)
real*8 :: x(:),quart
end subroutinesubroutine excel(x,quart)
real*8 :: x(:),quart
end subroutine
end interfacereal*8 :: a,b,c,x(4),quart
data x/1.0d0,2.0d0,3.0d0,4.0d0/Print *, "Excel Method"
quart=0.25d0
call excel(x,quart)
print *,’ q1=’,quartquart=0.500d0
call excel(x,quart)
print *,’ Median=’,quartquart=0.750d0
call excel(x,quart)
print *,’ q3=’,quart
Print *, "Minitab Method"
quart=0.25d0
call sasm4(x,quart)
print *,’ q1=’,quartquart=0.500d0
call sasm4(x,quart)
print *,’ Median=’,quartquart=0.750d0
call sasm4(x,quart)
print *,’ q3=’,quartPrint *, "SAS Method 5"
quart=0.25d0
call sasm5(x,quart)
print *,’ q1=’,quartquart=0.500d0
call sasm5(x,quart)
print *,’ Median=’,quartquart=0.750d0
call sasm5(x,quart)
print *,’ q3=’,quartend
subroutine excel(x,quart)
implicit none
! Excel’s method to calculate quartiles.
! Based on discussion in this paper http://www.haiweb.org/medicineprices/manual/quartiles_iTSS.pdf
!real*8 :: x(:),quart,a,b,c
integer :: n,ibn=size(x)
a=(n-1)*quart
call getgp(a,b,c)ib=int(c)
!print *,n,a,b,c,ibquart= (1-b)*x(ib+1) +b*x(ib+2)
end
subroutine sasm4(x,quart)
implicit none
! Minitab’s method of calculating is the same as the SAS Method 4.
! Based on discussion in this paper http://www.haiweb.org/medicineprices/manual/quartiles_iTSS.pdf
!real*8 :: x(:),quart,a,b,c
integer :: n,ibn=size(x)
a=(n+1)*quart
call getgp(a,b,c)ib=int(c)
!print *,n,a,b,c,ibif((ib+1)>n) then
quart=(1-b)*x(ib) +b*x(n)
else
quart=(1-b)*x(ib) +b*x(ib+1)
end ifend
subroutine sasm5(x,quart)
! Calculate Quartiles using SAS Method 5
! This method is the default method of SAS and is based on the empirical distribution function.
! Based on discussion in this paper http://www.haiweb.org/medicineprices/manual/quartiles_iTSS.pdf
!
implicit none
real*8 :: x(:),quart,a,b,c,tol,diff
integer :: n,ibtol=1.e-8
n=size(x)a=n*quart
call getgp(a,b,c)ib=int(c)
!print *,n,a,b,c,ibdiff=b-0.0d0
if(diff <=tol) then
quart=(x(ib+1)+x(ib))/2.0d0
else
quart=x(ib+1)
end ifend
subroutine getgp(a,b,c)
! Subroutine to that returns the Right hand and Left hand side digits of a decimal number
real*8 :: a,b,cb=mod(a,1.0d0)
c=a-bend
wondering what is a quartile, visit http://en.wikipedia.org/wiki/Quartile