<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/intermediate-tree-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/12/29/intermediate-tree-1/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/12/bonsai-316573__340.jpg?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-3558" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/12/bonsai-316573__340-150x150.jpg?resize=150%2C150" data-recalc-dims="1"></a>If you followed through the Basic Decision Tree exercise, this should be useful for you. This is like a continuation but we add so much more. We are working with a bigger and badder datasets. We will be also using techniques we learned from model evaluation and work with ROC, accuracy and other metrics.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/29/intermediate-tree-1-2/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong><br>
read in the adult.csv file with header=FALSE. Store this in df. Use <code>str()</code> command to see the dataframe. Download the Data from <a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/11/adult.csv">here</a></p>
<p><strong>Exercise 2</strong><br>
You are given the meta_data that goes with the CSV. You can download this <a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/11/adult_names.txt">here</a> Use that to add the column names for your dataframe. Notice the df is ordered going from V1,V2,V3 _ _ and so on. As a side note, it is always best practice to use that to match and see if all the columns are read in correctly.</p>
<p><strong>Exercise 3</strong><br>
Use the table command and print out the distribution of the class feature.</p>
<p><strong>Exercise 4</strong><br>
Change the class column to binary.</p>
<div>
<aside><img src="http://r-exercises.com/2016/12/29/intermediate-tree-1/"></aside>
<div><strong>Learn more</strong> about decision trees in the online courses
<ul>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/product/regression-machine-learning-with-r/">Regression Machine Learning with R</a> (it includes two lectures on definitions, characteristics, mathematical formulae, graphical representations, fitting, forecasting, and accuracy of decision trees)</li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/product/machine-learning-a-z-hands-on-python-r-in-data-science/">Machine Learning A-Z™: Hands-On Python &amp; R In Data Science</a> (including 3 lectures, ~45 mins, on decision tree regression in both R and Python)</li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/product/data-science-and-machine-learning-bootcamp-with-r/">Data Science and Machine Learning Bootcamp with R</a> (126 lectures and 17.5 hrs of video, including several lectures on decisions trees and random forests)</li>
</ul>
</div>
</div>
<p><strong>Exercise 5</strong><br>
Use the <code>cor()</code> command to see the corelation of all the numeric and integer columns including the class column. Remember that numbers close to 1 means high corelation and number close to 0 means low. This will give you a rough idea for feature selection</p>
<p><strong>Exercise 6</strong><br>
Split the dataset into Train and Test sample. You may use sample.split() and use the ratio as 0.7 and set the seed to be 1000. Make sure to install and load caTools package.</p>
<p><strong>Exercise 7</strong><br>
Check the number of rows of Train<br>
Check the number of rows of Test</p>
<p><strong>Exercise 8</strong><br>
We are ready to use decision tree in our dataset. Load the package “rpart” and “rpart.plot” If it is not installed, then use the install.packages() commmand.</p>
<p><strong>Exercise 9</strong><br>
Use rpart to build the decision tree on the Train set. Include all features.Store this model in dec</p>
<p><strong>Exercise 10</strong><br>
Use the prp() function to plot the decision tree. If you get any error use this code before the<code> prp() </code>command<br>
<code><br>
par(mar = rep(2, 4))<br>
</code></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/12/29/intermediate-tree-1/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/QdpvNW5jLn0"></a></div>