Lecture 52

IIT Kanpur July 201
20 followers
Oct 24, 2023
Hello friends, welcome to the course of Foundations of Art Software.
You may remember that in the previous lectures, we have talked about graphics.
And every time when I talked about graphics, I took only one variable.
I made my bar plot, I made a histogram, I made a pie chart, etc.
But I took only one variable.
Now, in this lecture, we are going to talk about such graphics which are based on two values.
Now, what are the graphs of these two values?
For example, if we take a small example, if we see the height and weight of small children,
then you have seen that as the age changes, the height and weight of the children changes,
if our child is of normal health.
So now you have seen that as the height increases, the weight also increases,
and if the weight increases, the height also increases.
So don't these two seem to affect each other?
So now we want to see that if we have two variables,
do they have any relationship with each other?
Do they affect each other or not?
And if they affect each other, then what is their degree of effect?
Do they do too much or too little?
We have a lot of such questions that we try to understand through these graphics.
Now, when we talk about this relationship,
then in our statistics, we have two types of tools,
one is our analytical and one is our graphical.
So we try to find answers to these questions through analytical tools,
but when we also apply our graphical tools,
then whatever information we get from both the tools,
we combine it together, so it gives us the correct information.
Today we will talk about bivariate graphics,
that is, if we have two variables,
how do we prepare our graphics between them?
After that, we will give you a small description
where we will also talk about three-dimensional graphics.
Now see, we can see in three dimensions with our eyes,
but when we try to make a picture in paper or pen,
it can only be of two dimensions.
But still, what we have tried to do is
that we try to show our three-dimensional picture in two dimensions
so that we can see its effect in it.
So how do we prepare that three-dimensional picture here?
This is our next question.
Now, when we talk about graphics,
our two-dimensional and three-dimensional graphics in R software,
then there is a very long list that we can create here.
But it is very difficult for me to consider all those graphs.
And if I do, then our whole course will be on graphics only.
So I will give you some examples here.
I will tell you about some of our commands.
And after that, I will request you
to try to get more information about these two-dimensional and three-dimensional graphics.
And see how R can make very beautiful and informative graphics for you.
So let's start this lecture.
And first of all, we will give you two-dimensional graphics and its related information.
So now let me tell you a little bit about the association of two variables.
What is the relationship between our two variables?
And how do we try to study it?
So as you have seen, if we tell our students that
if you study for more hours, you will get more marks.
That is, how many hours a student studies and how many marks he gets in the exam,
these are related to each other.
And in the same way, we see how our weather changes,
our temperature changes,
the consumption of electricity in our homes and offices changes.
When the temperature is very high, then the consumption of electricity is high.
Why? Because we use air conditioners, coolers, etc.
In the same way, the weight of our small children,
as their height changes under normal conditions,
their weight also changes.
So you will see that the consumption of electricity and the temperature of the weather
or the height and weight of small children are all related to each other
and they affect each other.
Now the question is that if these variables are related to each other,
then their observations will also be related to each other.
So how to find out if there is a relationship between these two variables or not?
And if there is a relationship, then how to find out if the relationship between them is very strong or very weak?
So in statistics, we have two types of procedures.
One is graphical, in which we try to extract such information through graphs.
And the other is quantitative.
That is, we use our analytical tools and try to understand the relationship through their values.
Like correlation coefficient, contingency tables, chi-square statistics, linear regression, non-linear regression, etc.
Don't worry, I am not going to talk about these topics here.
I have just tried to tell you that these are our quantitative procedures, quantitative tools.
In graphicals, we have two-dimensional plots and three-dimensional plots.
Today we will talk about some of our popular graphics.
So now the question is how to decide graphically that there is a relationship between two variables.
So if you assume that we have two variables x and y, which we show,
then above these two variables, we have n-pairs observation.
That is, if we say that we have five small children and I have taken their height and weight observation.
I took the height of the first child and then I took the weight of the first child.
Then I took the height and weight of the second child.
Then I took the height and weight of my five children in the same way.
So this is called paired observation.
That is, I have taken both the height and weight of the same child.
Now the problem is how do we plot these paired observations in our plot
and how do we try to extract the information hidden in that data through it.
So what we do is we plot these paired observations in a single graph on the x-y axis.
And we call it a scatter plot.
You may remember that you tried to plot the scatter plot using the plot command in R software.
But at that time you took only one variable.
Today we are going to talk about the scatter plot of two variables.
So this scatter plot tells us about the relationship between the two variables and what kind of trend we have.
For example, the relationship between two variables can be like this.
For example, this is linear and this is non-linear.
That is, if you ever hypothetically think that there is a line going here and all these points are around this line.
This means that their relationship is linear.
And in this case, if you see that this is non-linear.
So this is how we try to extract our outcomes from our scatter diagram.
In addition to this, if we come across any relationship like a linear relationship,
then we talk about the strength in it.
And their relationship is increasing or decreasing.
So to explain this, let us take some examples here.
First of all, let us take these two graphs.
We call them 1 and 2.
If you hypothetically think that a line is coming out from here,
which is going through the middle of these data values.
Now if you look at the location of these points,
then in the first graph, the points are very close to this line.
And in the second graph, the points are far away from this line.
So in such cases, we say that the relationship between them is strong.
In the first case, the relationship is strong.
And in the second case, the relationship is moderate.
In addition, if you see how our x is increasing and the value of y is also increasing,
then we say that this is our positive relationship.
And look at the positive, it is in both.
So in the first case, we say that this is our strong positive linear relationship.
And in the second case, we say that this is our moderate positive linear relationship.
Okay, now let us work on these two pictures.
We call this plot number 3 and plot number 4.
So in plot number 3 and plot number 4,
if you hypothetically try to make a line,
then you will see that all the points in plot number 4 are far away from this line.
And in plot number 3, all these points are very close to this line.
So we also say that in the case of 3, the relationship is strong.
And in the case of number 4, the relationship is moderate.
Now in the case of number 2, we will see that the way our x value is increasing,
the value of y is decreasing.
Right, so we say that this is our negative relationship.
So we will say that this is our strong negative relationship.
And in the second and fourth case, this is our moderate negative relationship.
And in this, if you plot x and y,
and if you do not see any such relationship here,
then you say that there is no such relationship here.
So now we are going to talk about the direction and degree of our relationship here.
And we have two aspects, one is graphical and the other is quantitative.
So here we are going to talk about graphical.
So our bivariate plots,
bivariate means plots between two variables,
these give us the first-hand visual information
that what kind of relationship we have between two variables.
But first of all, if you look at the graph,
you will know by looking at it that
your relationship is positive, negative, strong, moderate, weak, what is it?
And this relationship can be either linear or non-linear.
So here we try to understand all these aspects and plots by taking some examples.
So first of all, let us talk about our bivariate scatter plots.
How do we plot it?
So we saw that you will have two data vectors here, x and y,
because this is bivariate, there are two variables here.
So you stored the data vectors between the two in x and y.
Now, you already knew the plot command,
that you can plot the scatter diagram through the plot command.
So our command is plot,
and then we will write both our variables as x, y.
Now, as you have seen in the graphics,
we have a lot of options that we can use in different ways
to make our graphics better.
So in the plot, we have one option, type.
So what is in type?
If we write p, we will get points here.
This is our default.
And if we type l, we will get lines.
And if we type b, we will get points and lines.
And if we type c,
then we will get the part by removing the line in b.
And I will show you this through a graph,
so you will understand it better.
And if we use o,
then it will plot the points and lines on top of each other.
And if we say s,
then it will plot the stairs on which we try to go.
And if we say type is equal to h,
then it will plot the histogram or high-density vertical lines.
So what have I done here?
I have taken an example,
and through that example,
we will try to explain all these graphs to you.
So now what is the command you have?
You have seen that plot x, y type.
After that, you know that if you want to add an overall title to it,
then you will use the main command.
If you want to use the title, then you will do the survey here.
Similarly, to give the title on the x- and y-axis,
and to control the aspect ratio,
you also have the aspect command.
You have done all this before,
so I don't see any problem in it,
that I will have to explain it to you again.
Let's look at the rest of the details.
If you look at the help type or help plot,
then you will get it very well.
To understand all these things,
let's take a small example.
And what we have done in this example
is that I have taken 20 students
and in their exam,
I have recorded the numbers that came out of 500.
And along with that,
they were also asked
how many hours they have studied every week,
how many hours they have studied.
And I know this from our experience
or you must have heard it in your homes
or you must have heard it from your teachers
that if you study more, you will get good numbers.
Right.
So that's why the data of these 20 students has been collected here.
Why?
On marks.
And how many hours they have studied every week.
So see, this is some data like this,
that there is one student
who studied 23 hours in a week
and got 337 marks.
Similarly, another student studied 25 hours
and got 316 marks.
And so on.
Similarly, we have numbers of 20 students here.
Now I have stored the numbers of these students
in two data vectors here.
Marks and hours.
So see, this is our paired observation.
That is, the number written in the first place in the marks,
337,
this is related to the first student in our hours
that he studied 30 hours,
so he got 337 marks.
Right.
Now after that, see,
I used my plot command.
If I simply use the command
plot xy,
then we will have to write here
plot hours, marks.
So you will see,
you will see such a plot.
Here comes hours,
here comes marks.
And here you will see,
such points have been created.
And if you want to see from these points,
if you can think of your hypothetical line,
then if you make some lines like this,
then you will not understand by looking at it
that all these points are around the line.
Right.
So in this way,
we try to draw our conclusion from here.
So now see from this,
we do one thing,
we use our different types of commands.
So I wrote here
type is equal to
in double quotes,
I wrote l.
So even if we do not write type,
still this default will take it as l.
So what does l mean?
Lines.
So see what he did,
he joined all these points together.
Like if you pay attention,
so see,
he joined them like this.
Right.
So you got this kind of curve.
Now in this,
if you do type equal to b,
so what did he do?
See,
he gave you both this point and line.
Right.
So that means we will get it from here.
After that,
I used the option o here.
So see what is in o,
he over plotted it.
That means he plotted the line above the point.
Whereas in the previous case,
these lines and points are different.
See,
this is a point and this is a break between the two lines.
Right.
After that,
if you have taken h here,
so in h,
see,
he made a vertical line on the x-axis from each point.
Right.
Similarly,
if you take s here,
so the way there are stairs in our houses,
he made a plot by joining each observation in that way.
Now how do you use this,
where to use it?
See,
this depends on you.
My job is to tell you the options of all these graphs,
that they can be made here.
Right.
Now in this option,
if you want to make any other modification,
if you want to take the title here,
then you write here,
mean is equal to marks obtained versus number of hours per week.
So whatever you have written here,
see,
it is printed here.
If you want to write something on the y-axis,
then marks obtained.
So see,
you wrote here,
y lab,
and see,
marks obtained is printed here.
After that,
if you want to write the number of weekly hours on the x-axis,
then see,
you wrote all this by doing x lab.
Right.
So in the same way,
see,
the way you want,
you can control this graph very well with this curve.
Right.
So let's go,
first of all,
before we move forward,
let's show these graphs on your other software,
so that you will get a little confidence,
that how it works.
So see,
first of all,
I have taken the data of my marks and hours.
See,
how much did it become?
Marks,
and how much did it become?
Hours.
Now from that,
see,
I want to show you one thing,
that if you have taken the plot here,
marks,
and hours,
then see,
your graph will look like this.
See,
here your marks are on the x-axis,
and hours are on the y-axis.
And if you reverse this here,
that you will write hours first,
and then you will write marks here.
See,
what will happen?
See,
these two will exchange,
will change.
See,
marks came on the y-axis,
and hours came on the x-axis.
Right.
Now let's go to this command,
we give our different options,
type is equal to here,
L.
Right.
Now see,
your type will be like this.
All the points are joined by a line.
You did B here.
And see,
as soon as you entered,
it gave you both the things.
Right.
And in the same way,
when you did O here,
if you see,
the lines and points are different.
But as soon as you enter O here,
see,
your points also came on the line.
Right.
In the same way,
if you do H here,
then you will see,
you will also get high-density vertical lines from here.
See,
and after that,
if you did S here,
then you will see,
you will get a graphic like this with stair steps.
After this,
if you try to write the different options you have written here,
then see,
you will get this kind of command here.
See like this.
And see here,
everything is shown here.
Marks obtained versus number of hours per week.
And the kind of graph you wanted,
you got it here very easily.
Right.
I am working on both these windows at the same place,
so I have to make it small.
But you can easily execute it on your computer.
Now let's tell you about another command here.
So you saw that
we are talking about scatter plot after two variables.
So if we have two options,
that we make the plot between X and Y,
or between Y and X.
In the same way,
if we have three variables,
then the scatter plot we have,
where will it be made?
Between X and Y,
between X and Z,
between Y and Z.
And at the same time,
we can also interchange X, Y and Z.
So for all these graphs,
the first option is to plot them in different ways.
The second option is to plot them at the same place.
And the way you write your matrix,
in the same way,
you make a matrix scatter diagram.
Right.
So to do this,
we have a command,
P-A-I-R-S,
and in parentheses,
we write the names of our variables.
So what does it do?
It will give us a matrix-like scatter plot.
Right.
C-Bind.
Right.
C-Bind.
Right.
In this case, if you assume that here you have taken marks and here you have taken hours,
then see what happened, their axis changed and you got the data there.
So, in the first case, what you did is that you did your data between hours and marks
and in the second case, you did it between marks and hours, so you got all the data at
the same place, by which you can very easily find out what kind of relationship we have
between these variables.
Now, if we say that if you have a star variable, then your matrix plot will be something like
this.
There will be a plot in each of these blocks, right, I have told you the scatter plot, but
now it will be your job to draw the correct conclusion from it, it is easy, it is difficult,
I am not talking about this, but I have definitely told you how to get it out.
So, this is called a matrix scatter plot and in the matrix scatter plot, you have all
the options that if you want to give labels here, like as steady hours and marks obtained,
as I have given here, if you want to change its color, then you can do it here by using
COL, etc.
So, see all these, you have all the options to make such a matrix plot.
Now, we will show them to you in ARC software, so that you should be able to see how it
will work.
It came in between your marks and hours, and in this, if you want to change your label
and want to give the rest of your commands, then see how it will come, right.
So, if you have more variables than this, and your curves will be made from it, you
will have to see everyone together and after that you will have to draw the right conclusion
from it.
So, now we try to explain one more thing to you, you have seen that whenever we have
prepared our scatter plot, I told you that you think that there will be a hypothetical
line here, how will that line come to us and if we plot that line on our scatter diagram,
then you will understand that the information that I am getting through our plot is correct.
Right, so that is why we have a function scatter.smooth, everything is written in lower case alphabets.
What does it do to us, it will make a scatter diagram and will add a smooth curve to it,
and this smooth curve, we will call it our data, we will call it our data.
So,
if you want to modify the smooth curve, then you have a lot of options, like lparse, in
it we can modify it by the list command.
Now you know what a list is, in that you can make the color red, you can make lw, lty,
etc.
How to give that line, like if our line is this, we want to give that line like this,
or we want to give it like this, the width of it will be like this, its points will be
more, its width will be more or its width will be less, we can control it with all these
parameters.
So, as you can see, I have given lwd equal to 3 and lty equal to 3, so you got some kind
of line, which is already, see the direction of the line, everything is in the same position,
but its structure has changed.
Right.
Now the question arises that how did I get the values of lwd and lty, its very simple
method, you take all the values and experiment, from there you will get an idea, which will
suit you, which will give you the correct information according to your experiment and
data, you choose it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
So, let us first show you this in our R software, so that you can see it.
Right.
