Great.
And now let's add the second component which are the predictions.
So I'm going to add a plus here and add my second component here.
So the second component is as I just said the predictions.
And we are going to add these predictions component with the germline function.
So exactly like we did in simple linear regression.
So geom line here serum underscore line and then parenthesis of course.
And again we are going to take the a static function combined to a color.
So I'm just going to copy all this right.
And paste it in my germline function.
All right.
And now of course we will change a few things.
So first let's start with the simplest the let's change the color.
We're going to pick a blue color for predictions.
Here we go.
And now what we need to change are the coordinates of our prediction points.
So the x coordinates of our prediction points are still going
to be the levels themselves.
Because we're just predicting the salaries of the ten levels
that we have in our data set.
So here we obviously need to take the only levels that we have in our data set.
So we keep here data set the level.
But then for the y coordinates of course something is going to change here
because by taking data set
our salary here, we are taking the real salaries of our ten levels.
That is the salaries that are in the salary column of our data set.
Here.
So of course we need to remove this and replace it by something else.
And what is this something else going to be.
Well of course it's going to be the predictions
of the salaries of the ten levels according to the linear regression model.
And to get these predictions, we are going to use of course the predict
function.
And so actually that's exactly the same as in simple linear regression.
In this predict function we first need to specify the regressor.
So the regressor is called Len rag because you know
we are plotting the linear regression results.
And our linear regression model was called linear.
That's our regressor. So linear right here.
And now the second argument remember is new data.
So new data here.
And that's actually the data points we want to make predictions on.
So very simply that's our data set because we want to get
the predictions of the salaries of the ten levels of our data set.
So here we input indeed data set
there.
Perfect.
So we're good because we already inputted the blue color for our predictions.
Great.
And now let's complete this graph very quickly by adding a title A plus here.
And then you know we use the G title function to add a title.
Can you see how it's simple.
It's starting to get really intuitive now.
You know we are plotting the real observation points with the jump
point function, then the predictions with the GM line function,
and now a title with the target title function.
Well, everything will seem easier and easier for you.
So for the title we're going to add as in Python truth
or Bluff.
You know, just to give a funny name to our problem.
And we're going to specify here that we are plotting the linear regression result.
So we will just specify here linear regression.
All right.
So that's it for the title.
And now let's add an x label and a y label.
So for the x label we're going to add simply here
x lab parenthesis then quotes.
And then level.
Because our x axis will contain the levels.
And then plus y lab parenthesis
quotes and salary and done.
The linear regression results are ready to be plotted.
So let's not wait anymore and let's immediately have a look.
So I'm going to select all this.
I don't need to select this because ggplot2 is already imported.
And now let's press Command and Control plus enter to execute.
And here are the linear regression results.
So let's zoom on it and let's interpret.
Great. Okay.
So what is the first thing to understand here.
Well the most important thing to understand is definitely that
we need to make the distinction between the real observation points
that are the red points here.
Each point correspond to one specific level
and the real salary associated to this level on the y axis here.
And then we have our predictions on this line that is that
for each level let's say for example the level five
when we project this level five here on this straight line
here, this point is actually our prediction point.
And we can get the predicted salary by projecting
this point here on the y axis this way.
And so we get a little less than $250,000.
Okay. So that's the first thing to understand.
The red points are the real observation points.
And the points on this blue straight line are the prediction points.
Okay.
Now the second important thing to understand
is that our prediction result is actually a straight line.
And the fact that it's a straight line is due to a particular reason.
It's that the linear regression model is obviously a linear model.
Each time you build a linear model in two dimensions, you'll get a straight line.
And I'm highlighting this because the next model we are going to visualize
the result is going to be the polynomial regression model.
And this model turns out to be a non-linear regression model.
And you will see that it will not be a straight line anymore.