Hello and welcome to this art tutorial.
So now it's time for the fun part.
I can't wait to show you the training set results and the test set results.
So let's start to build the graphs.
So the first thing we're going to do is to import the ggplot2
library, which is a very good way of plotting something in R.
So I'm going to import it.
But before importing this library there might be some of you
who are starting R for the first time.
So if we go to our packages here, you can see that
the ggplot2 library is here on my RStudio.
But it might not be the case for you because
and that would mean that the ggplot2 library is not installed on your R.
So just for those of you for which it's the case, I'm going to install
ggplot2 library.
So we have to use this command install packages here.
And then in parenthesis we add ggplot2
g plot two.
Then we select this press command and control plus enter to execute.
And here and as you can see it's installing the library.
And that's done.
So that's the message you should be obtaining when you install the package.
Now it should be appearing in your library.
So mine is here and we are ready to start plotting the graph.
I'm just going to put that in comment.
So command shift plus C.
Now it's incoming
because once it's installed we don't need to install it again.
Okay.
So now let's start to plot our training set results graph.
So okay so first thing we need to do as you can see the ggplot
two library is not selected here.
So we will need to select it.
But we want to automate all this if we want to use simple linear
regression on another data set.
So we will just type here library.
Then in parentheses the name of the library.
Not in quotes actually did ggplot2.
And as you can see if I select this line
and execute here, you see that it's not selected.
And if I execute it selected here we just have a suggestion
to type suppress package started messages to eliminate package started messages.
Well that's not very important.
You can execute that if you don't want the package started messages.
But that's okay.
So I'm just going to enlarge this
okay.
And now like for this course the approach we will be taking to build
this graph with ggplot2 is going to be a step by step approach, because we are
first going to plot all the observation points in the training set.
Then we're going to plot the regression line.
And then we're going to add a title and then a label to the x axis
and a label to the y axis.
So you know it's going to be step by step.
First the observation points,
then the regression line, then the title, then the labels.
Okay.
So let's start with the observation points.
So okay.
So generally to introduce a plot in ggplot we start by typing
ggplot parenthesis.
All right.
And then that's
when we start to build step by step the little components of our graph.
And since we're adding each component one by one
we are going to separate each of these components by plus.
So let's go for the first component plus here I will press enter.
And here we go for the first component.
So the first component are the observation points.
So very intuitively we are going to write geom
underscore point.
All right.
So we are going to use this function of ggplot
to scatter plot all our observation points of the training set.
So the first thing that we need to do is to precise
which are going to be the x axis and the y axis.
And that's in the esthetic function which we call x.
So that's the function that will take as input the x variables and the y variable.
So the x variables is our years experience variable.
That means the number of years of experience.
That's what we want to have in the x axis.
And the y variable will be our salaries but the real ones
not the predicted ones. Yet.
So therefore we are going to add in parentheses x
equals.
And then we're going to put all our years of experience.
But be careful.
We want to take the years of experience of the observations in the training set.
So here we'll just have to specify training set dollar
and then years experience.
So that means that we are taking the years experience values
of the observations in the training set okay.
And now same for Y.
And here we do the same only with the salaries
which is here okay.
And that plots all your observation points.
The real ones in the plot okay.
So we could stop here, but we would like to add a call
because we will want to make the distinction between the observation
points and the regression line to, you know, make a nice plot.
So come here and then I'm going to press
enter to add a new argument of geom point.
Because this can be a little confusing here we have
we have a coming here and coming here.
This coming here is just to separate the x and the y in the esthetic function.
And this coming here is to separate
the first argument and the second argument of the June point function.
So the second argument is color.
And let's pick red.
I usually like to have my observation points in red.
But you know that's just my taste.
If you have some other tastes, you're welcome to, choose
your favorite color for the observation points.
Okay, so that's it for the geom point.
With this zoom point part, here we are plotting all the observation
points of our training set.