noc19-ma14 Lecture 15-central Tendency of data - Median

IIT Kanpur July 201
20 followers
Dec 3, 2023
Concept of median. Computation of median for ungrouped data. 3. Computation of median for grouped data.
Welcome to the next lecture on the course descriptive statistics with R software.
You may recall that in the earlier lecture we had discussed the idea of central tendency
and we had planned that we will discuss several measures of central tendency of the data.
In the last lecture we had explained the concept of arithmetic mean.
Now in this lecture I am going to consider the aspect of partitioning values and under
that topic I will try to consider the median okay.
So let us try to first understand what are these partition values.
If you try to see means if I try to create here the frequency distribution means on the
x axis we have values say class width or the x values and on the y axis we have frequency
values and suppose we have got a frequency distribution like this one.
Now you can see here that the entire frequency is being covered under this curve.
You can see here these are the frequency values and these are the different values
here of the frequency on the curve.
Now we would like to know that how these values are going to be partitioned.
For example if I say suppose I want to divide this in 4 equal parts.
So I can make here first, say here second, say here third and here fourth.
So you can see here from here to here this is indicating the area which is containing
nearly the 25% of the total frequency and similarly from here to here this is an area
which is trying to cover another 25% of the frequency and similarly this is also 25% of
the total frequency and this is also 25% of the total frequency.
So these values here they are called as suppose here partitioning values which are trying
to divide the total frequency into 4 equal parts.
So I can call it here as a quartiles.
So this is quartile, this is quartile value and yeah means the first value can be called
as first quartile, second value can be called as second quartile and so on.
So essentially what is happening that we are trying to divide the entire frequency into
10 parts and similarly in case if you want to divide it into more parts for example if
this is the frequency curve then possibly I can divide it into 10 parts 1, 2, 3, 4,
5, 6, 7, 8, 9, 10 and so on.
And similarly means I can partition it in say some other way also for example in case
if I want to partition it into say 100 equal parts 1, 2, 3, 4, 5, 6, 7, 8 and dot dot dot
up to here there will be 100 such partitions and it is also not necessary that this partition
have to be of the same length they can be of different lengths.
So by looking at the partitioning value we can have an idea that how the frequency is
distributed over the entire range of the frequency distribution and this will also give us an
idea that how the frequency is concentrated in different regions of the frequency curve.
So we will try to take up all these partitioning values one by one we will try to understand
them and we will try to see how to compute them on the R software.
Now I can say very simply that the frequency distribution is partitioned to have an idea
about the concentration of the values over the entire frequency distribution and as I
said there are several measures median, quartile, decile, percentile.
So let us try to start our discussion with median.
Now going through with that definition suppose if I try to plot here the frequency curve
like this and suppose I say I would like to divide the entire frequency into 2 parts 2
equal parts such that 50% of frequencies on the left side of this red vertical line and
50% of the frequency is on the right hand side of this red vertical line.
So now corresponding to which here is this value this is called as median.
So median is the value which divides the observations into 2 equal parts such that at least 50%
of the values are greater than or equal to the median and at least 50% of the values
are less than or equal to the median.
So median is a measure which is trying to divide the total frequency into 2 parts.
So if I say that the median of my frequency distribution is suppose say is 20.
So I can say here that 50% values are less than 20 and there are 50% values which are
more than 20 okay.
Now if you try to compare median with arithmetic mean then in all those situation where we
have got extreme observation that means some observation which is taking very, very high
value then in those cases this median is preferred.
Why because if you try to see suppose if I try to take here 2 values 2 and 4 and then
I try to find out its arithmetic mean, arithmetic mean is going to be 2 plus 4 divided by 2
is equal to 3.
But if I try to add here 2, 4 and here 100 then this value becomes here arithmetic mean
is equal to 2 plus 4 plus 100 divided by here 3 and this is 106 by 3 and this is closely
equal to 35.3.
So you can see here there is a huge difference between 3 and 35.3 and this difference is
coming because there is a new value which is added here 100 and this 100 is very much
different from 2 and 4 there is a huge difference between the 2 values.
So this median is a better average than arithmetic mean in case if we have extreme observations.
Now I would try to give the definition and how to compute the median in 2 cases.
One is ungrouped data and other is grouped data.
So first we try to understand the median and its computations when we have a data that
is ungrouped.
So we can just try to say we have observation x1, x2, xn so there are n values and they
are ungrouped or you can call as they are the values of some discrete variable.
Now what I do I try to order the observation and I present the ordered values as x and
in the subscript inside the bracket I am writing here 1 and the second ordered value will be
the value of x inside the bracket I am writing here 2 and so on.
What does this mean?
This means that this value x1 is the smallest value this is the minimum value among x1,
x2, xn and this x inside the bracket n this is the highest value or the maximum value
among x1, x2, xn.
What does this mean?
Suppose if I try to take here 4 observation say here x1 is equal to say here 20, x2 is
equal to here say here 10, x3 is equal to here 60 and x4 is equal to 5.
Now these are the 4 values.
So I try to find out here the minimum value among 20, 10, 60 and here 5.
So this is here 5.
So the first ordered value which I will denote as say x and 1 inside the bracket in the subscript
this becomes here 5 and after that once again I try to find out the minimum value among
the remaining value which is 20, 10 and 60.
So this gives me the second ordered value which is equal to here now you can see here
this is here 10 and similarly x3 is the minimum value among the remaining values between 20
and 60 and this is equal to 20 and obviously then the largest value is here 60.
So you can see here the difference between the simple observations and the ordered observations.
So you can see here what is the relationship?
The relationship is this first ordered value x1 this is same as your here fourth unordered
value.
Similarly here second ordered value is the same as the second unordered value and third
ordered value is here 20 which is the same as x1 this is the first unordered value and
fourth ordered value is 60 which is the same as third unordered value.
So you can see here that how the ordered and unordered values are interrelated okay.
So now the first step in finding the median of an ungrouped data is to order it first
and once you order it then there are two situations that the number of observations they can be
odd or the number of observation can be even.
So now in case if the number of observations are odd then the median is going to be the
n plus 1 by 2th ordered value which is here like this and in case if even that mean the
number of observations is even then the median is going to be the average of n by 2th ordered
observation and n by 2 plus 1th ordered observation like this.
So this gives you here the definition of the median.
So you simply have to see that whatever is the appropriate ordered value according to
this rule that will give you the median.
Now we consider the median for the group data.
So we know that whenever we have a group data or data on any continuous variable then the
first step is that we try to create the frequency table.
Then in frequency table we will have classes.
So now here we start our discussion by assuming that we had the data and from the data we
have created the frequency table and this frequency table has classes and there are
k classes denoted as a1, a2 say here ak.
So now the entire frequency is distributed equally among k classes and we assume here
that the absolute frequency of the ith class is ni.
So there are ni number of observation in the ith class ai.
From this absolute frequency I can compute the relative frequency and we are denoting
the relative frequency fi say here ni upon say here total frequency.
One thing I would like to mention here and I would like to draw your attention that please
notice the definition and symbol of fi.
In the case of median I am trying to denote fi the relative frequency but later on when
we are trying to consider other type of measure there is a possibility that I may define this
fi to be the absolute frequency.
So be watchful.
Now after this what we have to do?
Now I have got here classes a1, a2, ak and we know by definition the median is the value
where the total frequency is going to be divided into two equal parts.
So there will be a class where that half of the frequency will be lying.
So I will try to find out here the class where half of the frequency is lying and let this
class be denoted by here as say here am.
So am is the interval or the class which includes the median.
So I can define this median class as the mth class where in case if I try to sum all the
frequencies from 1 to m-1 and sum of all the frequencies 1 to m then this sum is going
to be smaller than half and this summation from i goes from 1 to m this is going to be
greater than or equal to half.
So this is going to be my median class.
Now the expression for finding out the median in case of group data is given by this.
Here you can see there is a quantity here em-1 this is denoting the lower limit of the
median class and similarly here there is a quantity dm this is going to denote the width
of the median class width means upper limit minus lower limit.
So this is the class width.
Then there is a relative frequency here fm which is going to be the relative frequency
of the median class and based on this we try to compute the median of the given grouped
data and we denote it here say x bar med the short form of median.
Now let us try to take an example and try to see how to get it done.
Here I would like to inform you that when we try to compute the median on the R software
then at least to the best of my knowledge there is only one command which is available
inside the base package to compute the median.
So this R software has no separate commands for computing the median for grouped and ungrouped
data.
So now through this example I will try to show you that how these different values like
as relative frequency of the median class and so on how they are chosen.
And then I will try to show you that how the median is computed on the R software but then
I will not be able to show you that how to specifically compute the median for the grouped
data.
Well one can write a small program or a small function to compute such thing but at least
I will not be handling it here.
So let us try to consider this example in which the data is collected on the time taken
by a customer to arrive in a shop inside a shopping mall and this time is recorded on
different days of the month.
So assuming that there are 31 days in the month this data has been collected for example
on the day 1 he takes 30 minutes on the day 2 the customer takes 31 minutes and so on.
So now I will try to find out the median from this data first considering it as ungrouped
data and then I will try to group it and then once again I will try to find out the median.
Okay so now here in this case when I try to consider this data as an ungrouped data.
So the number of observations here is 31 so n is equal to 31 so the value of n plus 1
is here 2 this is equal to 16.
So now what we have done that we have ordered the data this data has been ordered well I
am not showing you here that you can do and then I am trying to find out the n plus 1
by 2th ordered value and this is the 16th value in the ordered data and this value comes
out to be here 26.
So 26 minutes is the median time and now in case if I try to convert the same data into
an even number of observations.
So I can drop the last observation and I try to consider only here the 30 observations.
So in that case the number of observation becomes here 30 and then n by 2 here is 15
and n by 2 plus 1 is here 16.
Now according to the definition of the median the median is going to be the mean of n by
2th ordered value and n by 2 plus 1th ordered value.
So essentially this is going to be the arithmetic mean of the 15th ordered value and 16th ordered
value and from the data we find that the 15th ordered value is to 27 and 16th ordered value
is 26.
So this median comes out to be 26.5.
So this is how we compute the median in case of ungrouped data considering the data to
be odd and even numbers.
And similarly in case if I try to consider this data as a group data then we try to create
here the frequency table.
So you can see here I have already created the frequency table here.
These are my class intervals of the width 5 units like as 20 to 25, 25 to 30 and so
on and then in the second column I have found the absolute frequency and in the third column
I have computed the relative frequencies of all the classes.
Now you can see the advantage of working with the total relative frequency.
Total relative frequency is always going to be there are 5 classes.
So I go from here 1 to 5 this is going to be here 1.
So I simply need to find out here what is the class say here m minus 1 fi which is smaller
than 0.5 and for what value of here m the sum of frequency is greater than half.
So once I try to do it here I observe that there is a third class this is e3 for which
the sum of the relative frequency of class 1, class 2 and class 3 this comes out to be
12 upon 31.
So this is going to be 3 minus 1.
So essentially I am trying to find out f1 plus f2.
So you can see here this f1 is 0 and f2 here is 12 upon 31.
So this comes out to be smaller than half and if I try to find out the sum of f1, f2
and f3 this comes out to be 30 upon 31.
How this 30 is coming into picture?
This is coming out to be the absolute frequency of class 1 this is 0 plus absolute frequency
of class 2 this is 12 and absolute frequency of class 3 this is 18.
So this is essentially 12 plus 18 which is equal to 30 and this comes out to be greater
than half.
So now I can say here my median class is third class e3 and so here m is equal to 3.
Now I try to find out the lower limit of the median class which is here 25 the relative
frequency of the median class which is here 18 upon 31 and then the width of the interval
of the median class this is here 30 minus 25 which is equal to here 5.
Now once I try to substitute all these values over here in this expression and I try to
simplify it I get here 25.97.
So you can see here there is not much difference in the value of the median when we are trying
to compute it as a group data or say ungrouped data right.
For the ungrouped data you may recall that this value was coming out to be 26 you can
see here and for the group data this is coming out to be 25.97.
So here you can see means if your data is proper then practically there is no difference
either you try to compute the median say by this formula or by that formula and possibly
this is the reason that R has not implemented it and then if you try to see this 25.97 is
also close to 26.
So for all practical purposes there is not much difference.
Now I try to come on the aspect of R software inside the R software to compute the median
the command is median and this x is my here data vector and then in median also there
are several option.
So but I would once again request you that you try to look into the help menu and try
to see what are the different possible parameters that can be given inside the arguments.
But here I would certainly like to address that how would you compute the median in case
if some data is missing and that is represented as say here na.
So in that case use the same command median and give the data vector and use the option
here na.rm is equal to true.
So this will give you the value of the median.
So I try to now collect all the data on the minutes inside this data vector here minutes
and then I simply try to find out the median of minutes here this comes out to here 26.
So you can see here this is matching with the value that you had obtained earlier and
this is the screenshot.
So I would like to also show you here inside the same data that in case if the data is
missing then how you are going to handle it.
So inside the same data set I try to make the first two values to be not available and
I replace them by na.
So now in this case I try to create or I try to store the data inside a new data vector
minutes.na.
Well I would like to address here one thing in R there is an option to name the variables
using the dot sign or say full stop sign.
So that is why minutes.na I am writing it is not a say built-in function or there is
a rule it is simply trying to denote just for the sake of convenience that this is the
same data of minutes but now I am using the missing values.
So now using this data set I try to find out here the median using the same command median
minutes.na and I am using here na.rm equal to true and you can see here that this is
the screenshot and this value comes out to be again here 26.
So before I try to do something more let me try to show you how to compute these things
on the R software.
So first I try to store this data on the R console say here minutes so you can see here
these are the values of minutes and then I try to find out the median of your minutes.
So you can see here this comes out to be like this and similarly and I try to consider here
the missing values I try to once again store the data inside a new vector median.na and
you can see here this is the data minutes.na and then I try to find out the median of this
data right and if you try to see you have not used the option na.rm is equal to true.
So that is a very common mistake.
So now let me use it here na.rm is equal to so you can see here this value is once again
coming out to be 26.
Now I would like to stop here I have given you a detailed overview of median how to compute
it what is the concept and how to compute it in R and you please try to practice it
take some data and try to calculate the median manually and then try to do the same thing
with the software and try to see what is the difference usually I expect that unless
and until the data is extremely heterogeneous this difference will be very, very small and
for all practical purposes the value of the median that you compute from the R command
either for the group data or the ungrouped data they will not differ much.
So for all practical purposes you can accept them.
So you practice and I will see you in the next lecture till then goodbye.
