Hello and welcome to this art tutorial.
So today in this tutorial,
we are ready to fit multiple linear regression to the training set.
So as for simple linear regression, the first thing that we're going to do
is to introduce the multiple linear regressor.
And we're going to call it regressor.
And then as for simple linear regression we're going to use the lm function
here lm.
And in parentheses we're going to input the parameters okay.
So let's have a look at the LM function here by pressing F1.
Here we go. Fitting linear models.
Let's look at our arguments.
So that's the same as for simple linear regression.
Actually the first argument is formula.
But this time you're going to see how we will need to change the formula syntax.
And then the second compulsory argument to import is the data.
And of course it's going to be the training set.
So let's first input the formula.
So formula here.
So remember in simple linear regression
we wrote the formula salary proportional to years experience.
And here it's almost going to be the same.
But the difference is that we have several independent variables.
We have four independent variables.
In simple linear regression the dependent variable
was proportional to the only independent variable.
Well here it's going to be the same.
The profit is going to be proportional to these independent variables.
Only the correct way to say it is not proportional, but is the profit
is going to be a linear combination of the independent variables,
and formula equals profit
and n.
And here the idea is to add all the independent variables
separated with a plus sign.
So let's look at them.
It's r d spend
I'm using the dot here because in the data set
we have some spaces and I'll replace them by dots.
So here it's dot then.
Plus and then the other variable which is administration
plus marketing spend
spend plus state.
And that's all.
So that's how you express the profit
as a linear combination of all these independent variables.
However there is a trick to write this formula in a much more efficient way.
It's instead of writing all the independent variables here,
we can simply write a dot.
That's what we can simply write.
And that's the exact same equation.
R understands that you want to express the profit
as a linear combination of all the independent variables.
So the dot here just means all the independent variables okay.
So that's for the first argument. Formula equals
profit as a linear combination of all the independent variables.
And the second argument is data.
And as we said it is of course the training set
because we want to train our multiple linear regression model
on the training set and then test the performance on the test set later.
All right.
So that's it for building our multiple linear regression regressor.
So we're going to select this and execute.
Here we go.
And now now there is something very important to do.
And this is something very practical very useful in R it's that we are going to
look at the informations of our regressor that we just built here.
And to do this it's really, really simple.
We just need to it's actually like simple linear regression only here.
This time it's going to be more interesting
because we will have several independent variables,
and some of them will have a stronger effect on the dependent variable.
So we'll see. It's going to be more interesting.
And so like in simple linear regression we're going to write a summary
of our regressor.
All right.
And just press enter.
And that's it I'm just going to move that up.
And here we have all the info of our regressor okay.
So here the first thing is just a reminder of what the formula is.
So the profit is expressed as a linear combination
of all the independent variables.
And our regressor is trained on the training set.
Okay. Perfect residuals.
We will be talking about that at the end of this part.
So let's not focus on that right now.
However that's the important part.
The coefficients here.
That's what we need to focus on right now.
Because as you can see it gives some info
for each of your independent variable.
Or by the way state two and state three.
Here are the dummy variables.
And that's because, you know, I told you that the library
takes care of everything for you.
Not only it created the dummy variables are
knew that it had to create dummy variables for the state variable, and we helped R
to understand that because we encoded the state variable as factors.
So or did all the job for you because not only he created the dummy
variables for the state variable, but also R didn't fall into the dummy
variable try because as you can see, it automatically removed
one of the dummy variable to avoid some redundant dependency.
So that's perfect.
That's the beauty of, our libraries as well as Python libraries and Python.
I know we specified a new section to remove the dummy variable,
but that was just to remind you about the trap.
We don't need to do it.
R and Python take care of that for you.