In this video, we are going to look at the code to build a regression tree.
And we will build a regression tree on our movie data set.
And using that tree, we will predict the values of boxoffice collection for our test set.
The first step that we need to take to create this tree is we need to install some packages.
To create this entry, we will be using our part package.
And once a tree is created, we will use the R part or plot package to plot that decision tree.
So we need two packages, our part and our part not.
Again, if these are already in start, you do not need to install them.
But if they are not, you need to run these two lines to install the other part and are part of the
package.
So I will run these two commands.
And Condoling did.
Now both the packages are installed.
But if you go to the packages section, the take will not be dead.
You can see we have these two packages.
These are installed, but these are not active to make them active.
We either in library are part and library are blocked or we just take it here.
So we'll run these two commands to make them active.
Now we can use these two libraries, training a regression tree is very simple, not just one line of
code.
This is that line we will use.
The odd part funkin Gregory is the name of Abel in which we will get all the information or designation
tree.
And lately, I'm getting information from the, ah, part function.
That formula is.
Collection delayed dart, the meaning of collection delayed orders.
Anything on the left of L.A., this very symbol?
Anything to the left of this baby symbol is the dependent variable.
So my dependent variable is collection.
And I want to identify the relationship of collection with all the other variables in my data to represent
all other variables.
I have to write a dot.
If I want to use particular variable, I can write the name of that particular variable also here,
if I want to use two variables, I can write the name of both the variables and use a plus symbol in
between them.
So if I want to find the relationship of collection with marketing expense and production expense,
I'll just write collection delay marketing expense plus production expense.
So formula will tell the output function.
Which is the dependent variable and which are the independent variables here?
Collection is the dependent variable and DART signifies that all other variables except collection are
the independent or predictable variables.
The next parameter is data.
Name of data is green.
Because we want to use only retrains it to bring that model.
The third parameter we are going to use is control.
This barometer will help us control the length of the decision three.
We do not want a very long decision tree, a small tree is more interpretable.
And it has less chance of overfitting.
So here, I'm just going to keep our depth of three.
That is, it will have at Max.
Three more layers from the initial load, which has the total population.
If you want to know more about the are part function, that is what all other arguments can you use
with our part?
You just go to our bar and press EF1.
It will open the help of our part function in the dating scene.
You can see all the different argument that can be given.
The arguments that we are giving are mostly mandatory.
This control argument is not mandatory.
It has some default values but to control the growth of this country.
We have put this control argument to.
So I will run this, come on, no.
And you can see a new very well, Greg Tree is created and it has the information of the decision tree
model on this trading site.
Not if you want to look at this decision tree created by this single line of code.
You can run this second line.
The second line uses the passport law function of our partner plourde package.
The first argument, and this is the variable which contains the disagreeably information for us, it
was a.
The second argument is not mandatory.
It is just the color palette that we are going to use to lord this decision tree.
You can use a different color palette.
You can miss this argument also.
If you do not write this, it will use some default colors of its own.
The third argument is digit digit tells how many significant digits.
Should that decision tree help?
Once we applaud this decision tree, I will change the value of this digit to show you how the value
in the decision tree changes.
So let us run this command and Blätter diffidently.
You can see on the right we have this decision tree plot.
We can click on the zoom button.
And it is the zoomed version of this decision tree.
Let us look at this decision tree to see what this is telling.
So the top node that is the initial node had all the observations.
That is hundred percent of the observations.
And the average collection of all the movies was nearly 45000.
The first split is using the variable budget.
At nearly thirty eight thousand, well, you saw the left side.
As for movies which have budget less than 38000 and the right side is for movies which have budget more
than to you told them.
In the right side, you can see this node contains nearly 16 percent of the observations.
And whereas this left node contains nearly 84 percent of the obligations.
In these 16 percent of the movies, which are budget, more than 38000.
The average collection is nearly seven retooled holen, whereas.
In movies with less than 38000 budget, the average collection is nearly 39000.
So there is a huge difference between movies which have budget more than 38000.
And budget less than 38000.
You can continue further to see the effect of different variables.
At each node.
So in this node, the split is on the Berek everything.
And this node displayed is on trailered views and so on, since we mentioned that we need a maximum
depth of three layers.
This is layer zero.
This is layer one.
This is layer, too.
And this is layer three.
So this tree has a maximum depth of three.
If we increase that control barometer of maximum debt to five, we will have a bigger three.
Although that tree will be difficult to interpret.
So here you can clearly see that the most important variable is budget after budget.
It is nearly three levels.
And then we have that iterating our marketing expense.
Now, let us go back and see the effect of mentioning those digits.
But we did.
So if I put digits as Zito and done, come on.
You can see that now I have.
Now, I have this plot in scientific format that is, it is giving me values in 45 in two tenders to
develop three format.
So since this becomes a little bit difficult to read and understand, that is why I had Bridget is equal
to minus three.
That is.
I want those three significant figures also, which were going into the.
So if you wanted to be in the power, you increase the value of digital.
And if you want more significant deserves to be displayed like this, you reduce the value of digit.
The love we have created, we have trained our regression tree on the training set and we have used
the Arpad outlawed function to block the integration de.
You can use these regression trees in your presentations and in your document.
And these can help you make important business decisions.
No other part of using the C entry is to be able to predict the values for other movies or for future
observations that we are going to get.
Do predictive value, we use predict function.
I'm going to add a new column in the test dataset.
This column will contain the predicted values.
Using the decision tree that we have created.
So to find out the predicted values, we use this predict function in this predict function.
The first argument is that variable, which contains the information of DDC entry, which was a three
for us.
The second argument is the data on which you want to predict.
So that data is contained in the test, does it?
And the third parameter is telling the type of response that you want to predict.
Since we want continuous menus, we'll use type is equal to vector.
If this was a classification decision tree, that is, we wanted to predict the classes, then we would
have used a different type.
That is Tabor's equal duck class.
Here is a disintegration tree.
We want a political director.
So I will run this and we can go and look at the test dataset.
Notice click on it.
And in the end, you can see this column, the collection column has the actual values.
And this last column, the red column, has the predicted values for are.
We'll read it as it.
So using the information in a tree, it has predicted these values for the desert.
Now, one measure of performance of our model is calculating the mean squared error.
And asked Orlandi, two related means squared error is the meaning of squared of differences between
actual and predicted.
So basically we find out the difference between predicted and actual V squared is different.
And then we find, I mean, of all those values.
If I run this, a new variable, MASC squared is created.
It has this value.
Which is nearly one hundred thirteen million.
And this is the mean square difference between actual and deeply rooted values.
Now, when we study the advanced techniques in the future, we calculate masc values for all those techniques
and we will see that whether using those techniques reduces this MASC.
So basically, the accuracy will be dulled using MASC if masc value is low.
The model is more accurate.
So this is the code to create our D.C. entry.
Then to plot a D.C. entry and then to use that this entry to predictive values for another dataset.