And now let's add
the following component which is our regression line.
So here we add a little plus and then enter.
So to plot the observation points we used geom point.
And now very intuitively to plot the regression line
we're going to use geom line.
All right. So pretty easy to remember.
And then in this line same as for the points
we're going to take the x coordinate and the y coordinate of our regression line.
So let's see first we need to take the function esthetic as parentheses.
And that's where we put the x and the y.
So in your opinion what is going to be the x.
Well the x is going to be the years experience of the training set
because we want to have the predictions
of the salaries for the observations in the training set.
So in the x axis we need to have the number of years
of experience of the training set observations.
So here I'm just going to copy this.
There and
paste it here.
But now what is going to be the Y.
It's not going to be training set salary
obviously because we don't want to take the real salaries now.
We want to take the predicted salaries.
So in order to take the predicted salaries
we will very simply use again this predict function.
We cannot use again y pred here because why print
is the predicted salaries of the test set observations.
But here we want to get the training set predicted salaries.
So we will copy this predict
to take the predictions
of the training, set salaries and therefore in new data.
Here we need to replace test set by training set.
And now it's good.
Now this geom line function here
will plot all the predicted salaries of the training set.
Observations.
And so for each employee with
the same number of years of experience, because here we have the same x,
we're going to have both the real salaries, the salaries
they actually have in the company and the salaries predicted
by our simple linear regression model for the same employees.
Okay, so it's ready. The line is ready.
But as for the observation points, we will just add a color.
And then that means that we need to put a comma here and press enter.
And that's where we put the second argument which is going to be the color.
And this time we will pick
a blue color.
So again that's just my taste.
You're welcome to use some other colors if you prefer.
Okay. So now our plot is almost ready.
We have all the observation points of the training set.
We have the regression line.
And so now we could plot the graph now.
But if we want to have a nice graph that we want to present to people
like managers or clients or even your friends, it's always
better to add a little title and some labels on the axis.
So let's do this. Plus here and Gigi.
So this time it's not GM, it's Gigi because it's not a geometrical thing.
Gigi. Title parenthesis quotes.
Because we have to put the title of our plot in quotes.
And here we're going to give the following title salary
vs experience.
And we are going to specify
that it's the training set.
Because then we will do
the same for the test set and close the parenthesis.
And that's it. So perfect. That's our title.
And now let's just add a name of the x axis.
So to do this we use the x lab as in parentheses.
We will say that the name of our x axis is going to be years
of experience
okay.
And then plus and then the same for our y axis.
And obviously it's going to be y lab.
And he will choose salary very simply okay.
And our graph is ready to be plotted.
So I'm going to select all this.
And now press Command and Control.
Press enter to execute.
And here is our beautiful graph.
So that's the training set results with the real training set observations
with the real salaries and red and the regression line in blue.
So let's zoom this graph and let's give an interpretation.
All right.
So this is our training set results.
So we can clearly make the distinction
between the real observations which are the red points.
So these are the real salaries of the employees in the company.
And this blue straight line here
is our linear regression model.
So that's the prediction that means that for example
if we take this employee here well we can see that
his number of years of experience is a little more than five years of experience.
His real salary is about $65,000.
But our model predicted for this particular employee,
this result here, that means he predicted that this employee has a salary
of a little less than $75,000.
Okay.
So that's a real important distinction to understand.
This is the real value, and this is the predicted value of the model.
And to get the predicted salary you need to project this
point of the line here onto the salary axis,
which give us a predicted salary of a little less than $75,000.