noc19-ma14 Lecture 25-Association of variables-Univariate and Bivariate scatter Plots

IIT Kanpur July 201
20 followers
Dec 3, 2023
This lecture includes strength and the trend of relationship between two variables followed by different kinds of scatter plots.
Welcome to the course on descriptive statistics with R software.
You may recall that in the earlier lectures up to now, we have considered the descriptive
tools which are used for a univariate setup.
Univariate setup means there was only one variable and we tried to measure the measures
of central tendency measures of variations, etc. only on one variable.
Now what will happen when we have more than one variable?
For example, there can be two variables which are interrelated.
So the question comes that how to know whether the two variables are interrelated or not
and in case if they are interrelated, how to measure their degree of association.
So now from this lecture, we are going to attempt to study the descriptive tools which
are used for more than one variable.
Then there will be two types of tools.
One graphical tools and say another are analytical tools like as quantitative tools.
So in this lecture, we are going to study on say univariate and bivariate scatter plots
which is a graphical procedure.
So now let us start our discussion here and let me first take few examples.
Now for example, we know from our experience that if a student studies for more number
of hours, usually he will get more marks in the examination.
So now if I try to take these two as variables, the numbers of hours of study and the marks
obtained in the examination, then from experience we know that both these variables are associated
and they are related.
But you can think that if you have got a data set on two variables, how would you know on
the basis of given values that whether the two variables are related or not and in case
if they are related, how to show it graphically and how to quantify the degree of association.
So the first example which I have just taken is that the number of hours of study, they
affect the marks obtained in an examination.
Similarly, we also know that when the weather temperature increases for example during summer,
when we use more electrical appliances like a cooler, air conditioner and so on.
So the electricity or say power consumption increases, so I can say that the two variables
power consumption and weather temperature they are related and their tendency is that
as the temperature increases, the power consumption also increases.
Similarly, in say another example we know that weights of infants and small children,
they increase as the heights of those children increases under normal circumstances, right.
So now my question is this from this data set, from this type of information, we have
used our experience and based on that we are trying to conclude these things.
But my question is this how to do it statistically, how to do it mathematically and moreover how
to show it graphically and how to quantify it.
So now I will be considering the association between two variables.
So I will consider two variables and I will first try to show you that what are the different
types of plots available.
So now my question is this that I have got the observations on two variables and both
the variables are assumed to be related to each other.
So first question comes how to know that the variables are really related and if they are
related how to know what is the degree of relationship between the two variables.
So there are various graphical procedures like as two-dimensional plot, three-dimensional
plots and so on and there are some quantitative procedures also like as correlation coefficients,
contingency tables, chi-square statistics, linear regression, non-linear regression and
so on.
So we will try to study these tools one by one.
So now first let me try to describe the setup that what are the variables and how the observations
have been obtained and now we are interested in creating the graphs.
So I simply assume here suppose there are two variables and these two variables are
denoted by capital X and capital Y and a small n number of pairs of observations are available
on these two variables and these observations are denoted at X1, Y1 which are occurring
together X2, Y2 which are occurring together and lastly Xn, Yn which are occurring together.
What does this mean?
Suppose I take here X variable as say here height of children and I try to take here
Y to be the say here weight of children and now I try to collect the observation on these
two variables like this.
Suppose I take a child and I try to measure his height and suppose this height comes out
to be 100 centimetres.
So for this child number 1 the height is coming out to be 100 centimetre so I try to denote
it say here X1 is equal to 100 centimetre and then I try to find out the weight of the
same child, child number 1 and suppose this weight comes out to be say here 20 kilograms.
So I try to denote this first value as Y1 equal to 20.
So now this X1, Y1 which is here say here 100 and 20 this is my first pair observation
and similarly if I try to take here second child say child number 2 and if I try to measure
the height of this child suppose this comes out to be 90 centimetres.
So this is going to denote the height of the second child, height is denoted by capital
X so I can denote the height of the second child by X2 and similarly I try to find out
the weight of this child.
So weight is given by Y and its value is given by small y so I try to write down the Y2 which
is indicating the weight of second child and suppose we observe that this weight is suppose
17 kgs.
So now this X2, Y2 which is equal to here 90, 17 this is my second pair of observations
and so on we can say collect here more number of observation and this observation will be
given as 100, 20, 90, 17 and so on.
So this will indicate that this is the value of X1, Y1 and this is the value of X2, Y2.
So as soon as I write that there are n pairs of observations that mean these are some numerical
values which we have obtained by experimenting the data or by observing the data in any phenomena.
Now after doing this my first question will be I would like to know whether there is any
relationship between the two variables or not and that I would like to judge on the
basis of given set of data.
In order to do this thing we have a plot which is called as a scatter plot and in a scatter
plot what we do that we try to plot the paired observation in a single graph, how?
For example I have here two variables X and here Y, so I will try to take the value of
here X1 and here Y1 and I will try this point over here.
Similarly I try to take the value of here X2 and the value of here Y2 and I try to plot
it here.
So this point is the point X1, Y1 and this point is here X2, Y2.
So this is called a scatter plot.
Now this scatter plot can reveal different types of nature and trends of possible relationship.
So for example we can broadly divide the nature of relationship to be linear or non-linear.
So here you can see that I have just plotted a scatter plot here and here.
So you can see here these circles here they are trying to indicate the value of some observation
right.
So all these circles they are essentially trying to plot the values of Xi's and Yi's.
But if you try to look at the pattern in this graph what you see that you can see here that
there is a sort of trend mean the trend is that as the value of X is increasing the value
of Y is also increasing.
And this is happening more so over in a linear fashion there is a sort of linear relationship.
So I can say here that by looking at the scatter diagram I can conclude that the relationship
in this case is linear that means there exist a linear relationship between X and Y.
Similarly if you try to look into this figure second figure you can see here that once again
these points are plotted here.
But you can see here that these points are something like they are not actually linear
they are not showing that there is a really a linear trend but the trend is something
like this.
So this is indicating that the relationship between X and Y is non-linear right.
After this once we have decided that the relationship is linear or say non-linear then how to see
whether the strength of the relationship is say more or less.
So we are going to now consider here only the linear relationship.
And similar type of conclusion will also be there for the non-linear relationship but
I will not consider it here in this lecture.
So if you try to see here in these 2 graphics figure number 1 and figure number 3 on the
left hand side.
In case if you try to see here in the figure number 1 these points are concentrated inside
this band and in figure number 3 the points are concentrated in this band.
And now if you try to observe the width of this band and compare it with the band width
of figure number 1.
And you can see here that in case of figure number 3 the observations are scattered more
than in the case of figure number 1.
But in both the cases you can see that the trend is almost the linear which I am denoting
with the red color.
In both the cases you can see here that the trend is nearly linear.
But what is happening in this trend?
In this figure number 1 you can see here that those points are very close to the line.
All the scattered points they are lying very close to the line in red color.
Similarly in the case of figure 3 here if you try to see the points are lying quite
far away from the line.
You can see here in the orange line I am trying to denote the deviations.
And when I try to compare these deviations of observation from the trend line or the
red color line in figure number 3 and figure number 1.
I can say here that the strength of the linear relationship in figure number 1 is more why?
Because in figure number 1 here the points are lying more close to the line and in figure
number 3 the points are quite away from the line in comparison to figure number 1.
Now there is another thing which you have to observe.
In figure number 1 and say figure number 3 now try to observe my line in purple color.
You can see here as the values of x are increasing the values of y's are also increasing.
And the same thing is happening in figure number 3 also as the values of x are increasing
the values of y's are also increasing.
You can see here in figure number 1 this is my here x this is my here y and now if I try
to take another x here this is my here another y right.
So this is indicating that the relationship is increasing or we call it positive.
So this is what I mean which I have written in the title strong positive linear relationship.
That means the relationship is linear and this is strongly positive in comparison to
the relationship in figure number 3 where I am saying that the relationship is positive
but it is moderate positive and it is linear.
Similarly now in case if you try to observe in figure number 2 and figure number 4 here.
You can see here in this case as the values of x are increasing the values of y's are
decreasing.
This is happening in figure number 2 and the same thing is happening in figure number 4.
So in this case I can say that the relationship is decreasing.
Decreasing relation between say here x and y in both the cases figure 2 and figure 4.
Now in case if you try to create here a line that is called trend line you can see here
this is like this and in the figure number 4 this is like this.
Now if you try to analyze what are the deviations of individual observation from this line.
So if you first observe in figure number 4 these points are lying quite away from the
trend line you can see here in comparison to the figure number 2.
Because in figure number 2 now if you observe these points are very close to the line in
comparison to figure number 4.
So I can say now here that in figure number 2 the relationship between x and y is quite
strong and since it is decreasing relationship so we call it as a negative linear relationship.
Because the relationship is linear and similarly in the case of figure 4 I will call it as
a moderate negative linear relationship.
And similarly in case if you try to plot x and y and if you get no clear relationship
for example it is happening here in figure number 5.
For example you cannot say here whether there is an increasing trend or decreasing trend
or say where is the trend something like this.
So in this case by looking at the scatter plot I can say here there is no clear relationship
and even we do not know whether it is linear or nonlinear or even this is positive or negative.
So now in this lecture we are going to study the aspect of linear relationship.
So we will assume that whatever things we are going to do in those cases the relationship
between x and y is linear.
And there will be 2 aspects say graphical and say quantitative.
So now what I will do that I will try to take some examples and I will show you that what
are the commands on the R software and I will also show you that how to execute them and
how to understand the outcome.
First I am going to discuss here a command which is plot command.
One thing which I would like to make clear here that this plot command can be used to
create the scatter diagram in univariate as well as bivariate setup.
I had not covered it when I covered the univariate graphics because I knew that I am going to
cover the topic on plot.
So why not to cover it together right.
So in case if you have only one variable univariate case then the data on that variable
is stored in a data vector x and then the R command is plot and inside the argument
you have to write the data vector x okay.
Now I try to take a simple example and I try to show you here that how the graph will look
like.
So I have once again taken the same example which I considered in the earlier slides that
we have collected the data on height of 50% and which has been recorded here and this
data has been stored inside a variable whose name has been given as height like this.
Now I would like to plot this data.
So I give the command here plot and inside the argument height and you will see here
you will get here this type of outcome on the arc console and my objective is not to
show you here the figure but I want to show you that how to interpret it.
You can see here on the x axis this is given me the index.
What is the index?
For example, if you try to see in this data set what is my first observation?
This is my first observation.
So this is my here second observation.
So this index is trying to give the order in which the data has been given.
So this first observation will have index 1, second observation will have index 2, third
observation 130 will have index equal to 3 and in this plot what they have done?
They have taken index here, they try to take the value of a index number 1 and whatever
is the value here of the x data, for example, here this is height, they will plot it here,
they will try to take the index number 2, they will plot the data here somewhere wherever
it lies.
So here you can see here on the y axis we have here the height.
So this is only a scatter diagram of one variable.
So from here actually you can have the information on say central tendency or say dispersion
if the data is more concentrated or it is more concentrated around a particular value.
So all these types of measures of central tendency and dispersions can be viewed from
this type of graph.
Okay now I come to bivariate plots.
So bivariate plot mean there are 2 variables and these are the plot which gives us the
first hand visual information about the nature and degree of relationship between the 2 variables.
Whether the 2 variables are related or not and if they are related whether their relationship
is linear or say non-linear.
So all this type of information can be used by this bivariate plot.
So in bivariate plots what we try to do?
We will take 2 variables here x which will be plotted on x axis and say values of y variable
which will be plotted on y axis and then whatever are the values of say here x1, x2 and so on
here xn and similarly on the y axis all the values y1, y2 say here yn they will be plotted
here like this and they will try to show you the trend and degree of relationship.
So we will try to take some examples and we will try to see that what are the commands
and how they have to be executed.
So now in order to create a bivariate plot suppose we have 2 variables which I am denoting
by small x and small y in which the data of these 2 variables have been stored.
Now in case if I want to make a plot, plot like on x axis I will have the value of x
variable and on y axis I will have the value of y variable this can be executed by the
command plot.
But now inside the arguments I have to give the variables x and y separated by comma.
So this is how we have to do.
Now there is another option available in this command which is here type just by giving
various values to type we can create different types of graphics.
For example if I say type equal to p then it will give me only the points which is the
default value and if I say here p is equal to l this is going to give me the lines.
So you can see here I have made this p and here l to be bold which is indicating that
what is the meaning.
So you can easily remember also and if you want this point and line to both to be present
then I have to use the type equal to b which is coming from both and similarly if you try
to use the type equal to c then I will get lines which are only part of the b because
in b we have the points and lines both and similarly if I try to use here the command
o then I will get a over plotted means the points and line are both are over plotted
inside the graph.
Similarly if I try to take the type equal to s then I will get a graph which will look
like a stair steps and finally if I choose type equal to h then this will give me a lines
which will look like as if I have created the histograms of this data or there will
be some high density vertical lines.
So now let me try to take an example and I try to show you that how this different types
of graph will look like but before that there are some more options which are available
in the plot command.
For example in case if you want to give a title then you have to use the main inside
the argument and you have to just give whatever you want to give as a title of the plot.
Similarly if you want to give a subtitle of the plot then you have to use the command
sub a and if you want to give the title on the x axis then the command here is x lab
a and similarly for y axis title the command is y lab a and if you want to maintain the
aspect ratio that means how the graph will look like should it be more stage on in the
x direction or in the y direction then this is given by the option ASPTHE and if you give
a numerical value to this thing this will maintain the aspect ratio.
And definitely if you want to have more information I will suggest you that please try to look
into the help of this type command or say help on plot command.
Now I will take a simple example to demonstrate how to use this command and how the graphs
will look like.
As we have discussed that we know the number of hours of study of a student this affects
the marks obtained in the examination.
So the number of hours of study and the marks obtained in the examination they are related.
But this is from my experience so we have collected the data on 20 students that how
many hours every week they have studied and finally how many marks they have obtained
in the examination out of 500.
And those marks and the number of hours of these 20 students have been recorded as follows
that for example if a student has studied 23 hours per week then he has got 337 marks.
If a student has studied 25 hours every week then he has got 316 marks and so on.
So the first row is denoting the marks and second row is denoting the number of hours
per week which a student has studied and this data for 20 students has been obtained here.
So now what I do here first let me try to compile all this data into data vectors.
So I try to create a data vector here marks in which I try to store all the observation
which are given in the first row here this and here this and which is here.
And now in the second case I have taken a variable here hours in which I have stored
the data which is given in the second row here and I have created here 2 data vectors
marks and hours and I will be using this example again and again.
So now I have explained you the genesis of this data set.
Now I would try to simply make a plot of these 2 data vectors hours and marks.
So I will be using here the plot command for the bivariate plots which was plot xy.
So now I will say here plot inside the arguments I will try to give the 2 data vectors hours
and marks separated by comma and this will give me here the graphic like this one.
So you can see here that these are the data points and by looking at these data points
you can see here that there is going to be a sort of linear trend.
Linear trend means that most of the points are going to lie nearly on a straight line.
Now in case if these points are lying close to the line or away from line that will give
us an idea about the strength of the relationship between marks obtained in the examination
and number of hours studied right.
Now in case if I want to change this type then now I am going to use here a type which
is here L. If you remember this L was used as a type to create the graph with lines.
So now you can see here once I try to do it here I get here the graph like this one in
which all those data points like this one here and they are interconnected by such lines
and so on.
And if you wish you can compare it from this curve.
If you try to make it here this, this, this, this and so on connect them by line that will
give you a line plot.
Similarly if you try to choose here the option for type equal to b, b means both line and
point.
So you can see here this is the combination of the first two graph that we have obtained
here.
Those points are here all those points are here and they are connected by these lines
like this, this, this, this and so on.
So this is how you can obtain this type of plot.
Similarly if I try to use the option of type equal to o, o means over plotted.
What do you mean by over plotted?
In the earlier graph if there are two points here like this they were simply joined like
this.
But now I am saying that there are two points here like this and the line will cross through
like this one.
So you can see here these are the points and so on which are just joined from point to
point.
So the line and those dots both are over plotted.
So this type of graph will again give us a different type of information.
Now in case if I use the type H, so we have discussed this H means this is a sort of histogram
or say high density vertical lines.
So you can see here we have this data here like this and this data has been joined on
the x axis like this.
So this is another type of plot which can be obtained by using the type H and similarly
if you want to create a steps type of plot then use the type equal to S.
And if you see the plot which we have obtained here these are my data points and so on you
can see here first that what were my data points in the first curve.
You can see here this point is here like this and this and the same thing is here like this
the points are here and here.
And now they have been joined by say stairs type of plot and similarly you can see here
these points are going like steps means you have seen that when we climb on the roof then
there are stairs like this one.
So that is why this plot is called as stair steps type plot.
And now in case if you want to make it more informative suppose you want to add here the
title of the plot titles on the x and y axis.
So you have to use different options and if you remember we have discussed these things
in more detail when we have discussed the graphics in the case of univariate data.
So for example now exactly on the same way suppose I want to give here a title like marks
obtained versus number of hours per week.
So we have discussed that this can be given by the command main.
So I try to write here the main is equal to the title inside the double quotes and similarly
if you want to give here title on the x axis suppose I want to give number of weekly hours.
So then in order to do this thing we have the command x lab.
So I try to give whatever the title I want to give on x axis by writing x lab is equal
to the title inside the double quotes and this gives me here an outcome like this.
Similarly in case if you want to have a title on the y axis suppose I have want to give
here marks obtained then for that we have the command y lab.
So I write y lab is equal to the title which I want to give inside the double quotes and
this outcome is given over here.
And similarly there are some other option which I would say that please try to look
the help menu and try to see here.
So now I will try to show you that how these graphics using the plot command are constructed
inside the R software.
So let us come to our software part.
So you can see here that I already have stored the data hours and on the marks you can see
here like this.
So this is the same data that we have obtained in the example you can verify it.
Now I am trying to say here plot between say here marks and say here hours.
And as soon as you enter it you can you get here this type of graphic right.
So you can see here this is the same graphic that we have obtained.
Now in case if you simply try to change the order of the variable here you can see here
at this moment marks are coming on x axis and hours are coming on y axis.
Now suppose I want to interchange it in place of marks I will give hours and in place of
hours I will give the marks.
So I will type here plot hours and marks.
And now as soon as I enter you can see here that this graphic is changed.
You can see here like this.
So now I will continue the same command and I will clear the screen but I will try to
show you that what are the different options which you can use using the type option.
So if you see here this was our first option was type equal to L by which I will get the
lines.
So now you can see that as soon as I press enter this graphic will change.
You can see here this is now L and suppose if you want to have line and points both then
I have to give the type equal to B.
So you can you see here you get a different graphic and similarly if you want to use the
over plotted option by choosing the type equal to o a small o then as soon as I press enter
I get this curve.
This is the same graphic which we have seen inside the slides and similarly if you want
to have a histogram or say high density lines then using the type equal to H we get here
this type of command.
You can see here and finally if you want to have a stair type graphic then I have to use
that type equal to S and as soon as I do so I will get here an graphic like this one.
So you have now seen that creating graphic is not difficult at all.
Now suppose if you want to add some more features like as there are some default features that
whatever is the name of the variable that will be coming on the x and y axis.
But suppose if you want to add titles on x axis, titles on y axis and main title then
you have to use the same command.
So you see here on my slides that I had shown you here.
Now I will try to copy this command and I will try to paste it on the R console.
You can see here this is coming like this and now you can see here as soon as I have
pressed the enter this graphic is changed.
Now you have the titles on x, y axis as well as the other titles and similarly in case
if you want to change the colors this is also possible using the options.
But definitely I have shown you the basic operation.
You can make it as beautiful as possible, as informative as possible whatever you want.
So now I would stop in this lecture and in this lecture I have given you an idea of the
plot command.
I am not saying at all that which of the command or which of the graphic or which of the type
is the best.
It depends only on you people or the experimenter.
The experimenter has to decide or you people have to decide what type of information you
want from the graphic and which is the graphic which is most suitable to provide that information
in the correct way.
And this also comes by practice and experience.
So at this moment the objective is to learn how to create graphics and how to make them
more informative, more interactive using the R commands.
So you please take some data set, try to experiment on it, try to give different types of option,
try to create the plots and try to see what type of information they are going to provide.
Try to take different types of types and you practice and we will see you in the next lecture.
Till then good bye.
