Hello and welcome to this art tutorial.
In the previous sections, we already introduced two nonlinear regression models
the polynomial regression model and the SVR regression model.
And today we're going to introduce a new nonlinear regression model
which is the decision tree regression model.
So far the best model we had for our data set
and our specific problem was the polynomial regression model.
Let's see how decision tree regression will do and compare it
to the previous ones.
So let's start with the basics.
Let's set the right folder as working directory.
So right now I'm going to the regression folder.
And we are now in decision tree regression.
I'm just getting to the end.
So this is the right folder containing the position salary CSV file.
Make sure of that and click on this more button here and set as working directory.
All good.
And now let's take our regression template to build this model efficiently.
So we take everything from here to here
and pasting it here right.
And now we just need to change a few things.
So let's change the basics.
Let's replace the regression model by decision tree.
Regression.
All right.
We're going to copy that and put that same title here.
All right.
Visualizing the decision tree regression results.
And same here.
All right.
And now let's change
the most important thing which is the part where we create our regressor.
So I'm going to remove that comment line here.
And now let's build our decision tree regression model.
So it's going to be the same thing as anytime we're going to import a package.
And then use a function from this package to build our regressor.
So this package for decision tree regression
in R will be the R part package.
For those of you who don't have the R part package in your packages here,
I will type this command line so that you can install it.
So as you can see, mine is already here our part.
Just check if you have it
and if you don't have it just type here install dot packages.
And then in the parenthesis in quotes you type the name of the package, which is
our part.
All right.
And then you just select this line execute.
And this one cell package.
All right.
But I'm going to put this line as comment.
Just press command to shift plus C.
And now let's also add this line library.
And in parenthesis the name of the package we want to select.
Well import our port right.
And as you can see our part is not selected here.
And when this line of code will be executed
then this will be selected here.
All right.
And now we are ready to start building the model.
So as I just said we're going to take a function from this part library.
And this function is actually our part as well.
So let's use this function.
And as usual we're going to call our regressor regressor.
So here it is.
So that's our decision tree regression regressor.
And let's now use our part function.
All right.
So now let's see what arguments we need to input.
The best way to do that is to press F1 here
so that we get the info about the R part library.
So usually we have it here.
But then we just need to click on this link here.
And that will give us info about this artboard library okay.
So let's see what we have.
The first argument is formula.
So you know what to do here.
We just need to write formula equals the dependent variable.
Then a tilde.
And then a dot that represents all your independent variables.
All right. But that we know perfectly then data.
So data is the data set on which we want to build
our decision tree regression model.
So since we did not build any training set data is going to be the data
set exactly like polynomial regression and SVR regression.
All right then weights is an optional argument.
You can add some weights to make your model more advanced.
But you know this is more advanced.
So we will not cover that right now.
And then you have some other arguments that are optional that that can help you
make your model even more robust or, you know, include some regularization
techniques, urbanization to prevent overfitting, that sort of things.
But right now we just want to build a simple decision
tree regression model because we have a simple data set.
And so we only need formula and data.
So let's go back to R and let's input these arguments.
So first argument was formula.
And then as I mentioned
it's formula equals salary because it's our dependent variable.
Then tilde just pressing alt.
And here
and then a dot or actually level.
But you know I want to make this script that can be also
applied to your data sets. So I'll use a dot.
All right.
So that's the first argument.
And now let's give the second one.
The second one was data.
The data equals data set.
And now our regressor is ready to be built.