Hello and welcome to this art tutorial.
So right now let's write the code to visualize those results.
So we're going to do this with ggplot2.
In case you didn't follow the linear regression tutorials on R
I'm just going to write a line to install the ggplot2 package.
I'm not going to install it, but it's just in case you don't have it.
And to check that you need to go to the packages here
and check if you have the ggplot2 package here.
And so if you don't find it in this list here,
you need to install it and to install it.
It's really simple.
You just need to type here install dot packages.
You can press enter here.
And then in quotes you enter the name of the package which is ggplot2.
And then you just need to select this line and execute this line
by pressing Command and Control plus enter.
And this will install the ggplot two package without any issue.
So I'm going to put that as command by pressing command shift plus c.
All right.
And now we can start to visualize the linear regression results.
So as you can see in the packages here the ggplot2 package is not selected here.
So I'm going to need to select here.
So either I can click here.
Or the better way is to automate the selection of this package
thanks to this script.
And to do this we just need to add the line library
and in parenthesis the name of the package with no quotes.
So just glad to hear this way and when this line will be executed.
Actually we can check it right now.
As you can see, ggplot2 is not selected.
I'm executing this line and now it's selected.
Okay, so now that our ggplot2 library is selected, let's start building the plot.
So we're going to do exactly like in simple linear regression.
We going to use the ggplot function.
And then we're going to add the different components in the graph.
So first we will add the real observation points thanks to the jump point function.
And then we're going to add
the predictions component thanks to the Geum line function.
Then we're going to add a title to the plot things to get title,
and then the label to the x axis with x slab and a label to the y axis with y lab.
So very simple.
Exactly like before, we are going to make this graph step by step.
Okay. So let's start with the first step.
The first step is just to type here ggplot
with parentheses.
And that will initiate the plot.
And now remember we need to add a plus here.
And that's where we start to add the different components.
So the first component is the real observation points.
And we are plotting them with the gem point function.
So let's add here geom point geom underscore point.
Actually here it is.
And now we input the arguments in the parentheses okay.
So the first argument remember is the esthetic function.
So that's in this function that we will input the x coordinates
of our observation points as well as the y coordinates.
So let's do this s for a function.
So in this esthetic function we need to input the x coordinate
and the y coordinate of all our ten observation points.
So let's see what these coordinates are.
Let's have a look at our data set.
So our ten observation points are characterized
by their levels and their salaries.
The x coordinates corresponds to the independent variable
that is the level column here.
And our y coordinates will be our ten salaries
here associated to these ten levels here.
So in short the x coordinates are the independent variable values.
That is the ten levels. Here.
And the y coordinates are the dependent variable values.
That is the ten salaries here okay.
So let's input that.
So x here is level and y is salary.
However since we did not specify that the data is data set here our data set
we need to specify where we are taking the levels and the salaries.
And to specify that we need to add here data set
dollar.
So that's our understands that we are taking
the levels of our data set data set here.
So it's going to be the same for Y.
We're going to specify that we're taking the salaries of the columns
salary in our data set this way.
Perfect.
And now we can add another argument because this
a static function was the first argument of this geom point function.
And remember we can add a second argument which we will add
because we want to add a color to our points to then make the distinction
between the real observation points and the predictions.
And so we're going to pick the red color.
So here I'm going to add the color argument
and set it equal to red in quotes.
All right.
Now let's not forget to close the parentheses of the jump point function
because this parenthesis was for the esthetic function.
And now we are all fine.