help treeplot

Title

treeplot-- Graphing a tree in Stata

Syntax

treeplot
outcome
features,
type(tree_type)
tree_depth(integer)
predict(name)
save_graph(graph_name)
fig_size(integer)
dpi(integer)
h(name)

Inputs

outcome: numerical variable (for a regression tree), or categorical variable (for a classification tree)

features: list of numerical variables representing the features. When a feature is categorical,
please generate the categorical dummies related to this feature. As the command does not do it by default,
it is user's responsibility to generate the appropriate dummies

Description

treeplot is a command for graphing a regression or classification tree in Stata 16.
It uses the Stata/Python integration (sfi)
capability of Stata 16 making use of the Python Scikit-learn API.

Options

type(tree_type) sets the type of tree, where tree_type is "reg" for regression and "class" for classification

tree_depth(integer) sets the depth of the tree, where integer is the maximum depth

predict(name) generates regression or classification predictions, with name indicating the name of the variable that will contain the predicted values

save_graph(graph_name) saves the plot on the hard-disk, with graph_name the chosen name of the plot

fig_size(integer) defines the size of the plot, with integer the chosen size

dpi(integer) sets the quality rendering of the figure, with dpi indicating the dots per inches (that is, how many pixels the figure comprises)

h(name) is a generated variable identifying with a "1" the observations used to build the tree

Remarks

To run this program you need to have both Stata 16 (or later versions) and Python
(from version 2.7 onwards) installed. You can find information about how to install
Python in your machine here: https://www.python.org/downloads.
Also, the Python Scikit-learn (and related dependencies) and the Stata Function Interface (sfi) APIs
must be uploaded before running the command. You can find information about how to install/use
them here (observe that the SFI API is already installed in your Stata 16 or later versions):
(1) Scikit-learn: https://scikit-learn.org/stable/install.html; (2) sfi: https://www.stata.com/python/api17.

Please, remember to have the most recent up-to-date version of this program installed.

Example

Example 1: Plot a regression tree

Load initial dataset

sysuse auto, clear

Run and plot a tree regression

treeplot price mpg rep78 headroom , type("reg") tree_depth(3) predict("pred") save_gr("graph_tree_r") fig_size(6) dpi(600) h("sample")

Example 2: Plot a classification tree

Load initial dataset

webuse iris, clear

Run and plot a tree classification

treeplot iris seplen sepwid petlen petwid , type("class") tree_depth(3) predict("pred") save_gr("graph_tree_c") fig_size(6) dpi(600) h("sample")

Reference

Cerulli, G. 2021. Improving econometric prediction by machine learning, Applied Economics Letters, 28, 16, 1419-1425, 2021.

Droste, M. 2022. Stata-pylearn. Available at: https://github.com/mdroste/statapylearn.

Gareth, J., Witten, D., Hastie, D.T., Tibshirani, R. 2013. An Introduction to Statistical Learning : with Applications in R. New York, Springer.

Raschka, S., Mirjalili, V. 2019. Python Machine Learning. 3rd Edition, Packt Publishing.

Author

Giovanni Cerulli

IRCrES-CNR

Research Institute for Sustainable Economic Growth, National Research Council of Italy

E-mail: giovanni.cerulli@ircres.cnr.it

Also see

Online: python, c_ml_stata_cv, r_ml_stata_cv srtree, sctree, subset