{"id":93825,"date":"2021-07-07T09:50:00","date_gmt":"2021-07-07T13:50:00","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=93825"},"modified":"2022-11-21T09:47:43","modified_gmt":"2022-11-21T14:47:43","slug":"how-to-plot-xgboost-trees-in-r","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/","title":{"rendered":"How to Plot XGBoost Trees in R"},"content":{"rendered":"\n<p>In this post, we\u2019re going to cover how to plot XGBoost trees in R.&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/XGBoost\">XGBoost<\/a>&nbsp;is a very popular machine learning algorithm, which is frequently used in&nbsp;<a href=\"https:\/\/www.kaggle.com\/\">Kaggle<\/a>&nbsp;competitions and has many practical use cases.<\/p>\n\n\n\n<p>Let\u2019s start by loading the packages we\u2019ll need. Note that plotting XGBoost trees requires the&nbsp;<strong>DiagrammeR<\/strong>&nbsp;package to be installed, so even if you have&nbsp;<strong>xgboost<\/strong>&nbsp;installed already, you\u2019ll need to make sure you have&nbsp;<strong>DiagrammeR<\/strong>&nbsp;also.<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\n# load libraries<br>\nlibrary(xgboost)<br>\nlibrary(caret)<br>\nlibrary(dplyr)<br>\nlibrary(DiagrammeR)\n<\/p>\n\n\n\n<p>Next, let\u2019s read in our dataset. In this post, we\u2019ll be using&nbsp;<a href=\"https:\/\/www.kaggle.com\/shrutimechlearn\/churn-modelling\">this customer churn dataset<\/a>. The label we\u2019ll be trying to predict is called \u201cExited\u201d and is a binary variable with 1 meaning the customer churned (canceled account) vs. 0 meaning the customer did not churn (did not cancel account).<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\n# read in dataset<br>\nchurn_data <- read.csv(\"Churn_Modelling.csv\")\n<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"638\" height=\"209\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/07\/xgboost-data-in-r-the-automatic-net.png\" alt=\"How to Plot XGBoost Trees in R\" class=\"wp-image-93838 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/07\/xgboost-data-in-r-the-automatic-net.png 638w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/07\/xgboost-data-in-r-the-automatic-net-300x98.png 300w\" data-sizes=\"(max-width: 638px) 100vw, 638px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 638px; aspect-ratio: 638\/209;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-preparing-the-dataset-for-modeling\"><strong>Preparing the dataset for modeling<\/strong><\/h2>\n\n\n\n<p>Now, let\u2019s prep our dataset for modeling. First, we\u2019ll remove a few variables we don\u2019t need. Second, we\u2019ll one hot encode each of the categorical variables. Then, we\u2019ll split our dataset into train and validation.<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\n# remove variables we won&#8217;t be using for modeling<br>\nneed_data <- churn_data %>% select(-RowNumber, -CustomerId, -Surname)<br><br>\n \n# one hot encode all categorical variables<br>\ndummy <- dummyVars(\" ~ .\", data = need_data)<br>\nneed_data <- data.frame(predict(dummy, newdata = need_data))<br><br>\n \ny_label <- need_data$Exited<br>\nneed_data <- need_data %>% select(-Exited)<br>\nneed_data <- data.frame(Exited = y_label, need_data)<br><br>\n \n# convert Exited column to a factor<br>\nneed_data$Exited <- as.factor(need_data$Exited)<br><br>\n \n# split data into train \/ validation<br>\nset.seed(0)<br>\nindexes <- sample(1:nrow(need_data), 0.7 * nrow(need_data))<br><br>\n \ntrain_data <- need_data[indexes,]<br>\nval_data <- need_data[-indexes,]\n<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Train the XGBoost model<\/strong><\/h2>\n\n\n\n<p>For the purpose of this article, we\u2019ll just vary a few of the hyperparameters using a grid search.<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\n# create tuning grid<br>\ngrid_default <- expand.grid(nrounds = c(50, 75, 100, 150, 200, 250),<br>\n                            max_depth = c(2, 3, 4, 5),<br>\n                            eta = c(0.05, 0.1, 0.15),<br>\n                            gamma = c(0),<br>\n                            colsample_bytree = c(0.7),<br>\n                            min_child_weight = c(5),<br>\n                            subsample = c(0.6))<br><br>\n \n# set random seed<br>\nset.seed(1234)<br><br>\n \n# train XGBoost model<br>\nxgb_model <- train(formula(need_data), <br>\n                   data = need_data,<br>\n                   tuneGrid = grid_default,<br>\n                   method = &#8220;xgbTree&#8221;,<br>\n                   metric = &#8220;Kappa&#8221;)\n<\/p>\n\n\n\n<p><strong>Plotting XGBoost trees<\/strong><\/p>\n\n\n\n<p>Now, we\u2019re ready to plot some trees from the XGBoost model. We\u2019ll be able to do that using the&nbsp;<em>xgb.plot.tree<\/em>&nbsp;function. Let\u2019s plot the first tree in the XGBoost ensemble. Note that in the code below, we specify the model object along with the index of the tree we want to plot.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\n# plot the first tree<br>\nxgb.plot.tree(model = xgb_model$finalModel, trees = 1)\n<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/07\/xgboost-plot-tree-in-r-the-automatic-net.png\" alt=\"How to Plot XGBoost Trees in R\" class=\"wp-image-93844 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p>From the plot, we can see that Age is used to make the first split in the tree.<\/p>\n\n\n\n<p>If we want to plot another tree, we can just change the tree index. For example, the line of code below plots the second tree in the XGBoost ensemble.<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\nxgb.plot.tree(model = xgb_model$finalModel, trees = 2)\n<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/07\/xgboost-plot-tree-2-the-automatic-net.png\" alt=\"\" class=\"wp-image-93847 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p>If you want to adjust the height and width of the plot, you can do that by changing the&nbsp;<em>plot_height<\/em>&nbsp;and&nbsp;<em>plot_width<\/em>&nbsp;parameters:<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\nxgb.plot.tree(model = xgb_model$finalModel, trees = 1, plot_width = 1000, plot_height = 1000)\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Exporting tree plots to a file<\/strong><\/h4>\n\n\n\n<p>Plots can also be exported programmatically using the&nbsp;<em>export_graph<\/em>&nbsp;function. Note that to do this, you\u2019ll need to set&nbsp;<em>render = FALSE<\/em>&nbsp;in the&nbsp;<em>xgb.plot.tree<\/em>&nbsp;function.<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\n# create plot object of XGBoost tree<br>\ntree_plot <- xgb.plot.tree(model = xgb_model$finalModel, trees = 1, plot_width = 1000, <br>\n                           plot_height = 1000, render = FALSE)<br><br>\n \n# export plot object to file<br>\nexport_graph(tree_plot, &#8220;xgboost_tree_plot.pdf&#8221;, width = 1000, height = 1000)\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Plotting multiple trees at once<\/strong><\/h4>\n\n\n\n<p>Rather than plotting a single tree, you can also plot multiple trees at once. You just need to pass a vector of which trees you\u2019d like to plot. For example, the code below will plot the first three trees.<\/p>\n\n\n\n<p style=\"background-color:#fcfcdb;font-size:12px\" class=\"has-background\">\nxgb.plot.tree(model = xgb_model$finalModel, trees = 1:3)\n<\/p>\n\n\n\n<p>Visit TheAutomatic.net Blog for additional insight on this topic: <a href=\"https:\/\/theautomatic.net\/2021\/04\/28\/how-to-plot-xgboost-trees-in-r\/\">https:\/\/theautomatic.net\/2021\/04\/28\/how-to-plot-xgboost-trees-in-r\/<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to plot XGBoost trees in R, and get sample code for using caret, dplyr and DiagrammeR packages.<\/p>\n","protected":false},"author":388,"featured_media":44292,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,338,341,352,344,342],"tags":[2532,806,9967,487,6591,9966],"contributors-categories":[13695],"class_list":{"0":"post-93825","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-ibkr-quant-news","10":"category-quant-development","11":"category-quant-north-america","12":"category-quant-regions","13":"category-r-development","14":"tag-caret","15":"tag-data-science","16":"tag-dplyr-and-diagrammer","17":"tag-r","18":"tag-rstats","19":"tag-xgboost","20":"contributors-categories-theautomatic-net"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Plot XGBoost Trees in R | IBKR Quant<\/title>\n<meta name=\"description\" content=\"Learn how to plot XGBoost trees in R, and get sample code for using caret, dplyr and DiagrammeR packages.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/93825\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Plot XGBoost Trees in R | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"Learn how to plot XGBoost trees in R, and get sample code for using caret, dplyr and DiagrammeR packages.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-07T13:50:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:47:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/target-download.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrew Treadway\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Treadway\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Andrew Treadway\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\"\n\t            },\n\t            \"headline\": \"How to Plot XGBoost Trees in R\",\n\t            \"datePublished\": \"2021-07-07T13:50:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:47:43+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/\"\n\t            },\n\t            \"wordCount\": 161,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/target-download.jpg\",\n\t            \"keywords\": [\n\t                \"caret\",\n\t                \"Data Science\",\n\t                \"dplyr and DiagrammeR\",\n\t                \"R\",\n\t                \"rstats\",\n\t                \"xgboost\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"Quant North America\",\n\t                \"Quant Regions\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/\",\n\t            \"name\": \"How to Plot XGBoost Trees in R | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/target-download.jpg\",\n\t            \"datePublished\": \"2021-07-07T13:50:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:47:43+00:00\",\n\t            \"description\": \"Learn how to plot XGBoost trees in R, and get sample code for using caret, dplyr and DiagrammeR packages.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-plot-xgboost-trees-in-r\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/target-download.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/05\\\/target-download.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"Quant Download\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\",\n\t            \"name\": \"Andrew Treadway\",\n\t            \"description\": \"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \\\/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\\\/\\\/www.linkedin.com\\\/in\\\/andrew-treadway-a3b19b103\\\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/theautomatic.net\\\/about-me\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/andrewtreadway\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Plot XGBoost Trees in R | IBKR Quant","description":"Learn how to plot XGBoost trees in R, and get sample code for using caret, dplyr and DiagrammeR packages.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/93825\/","og_locale":"en_US","og_type":"article","og_title":"How to Plot XGBoost Trees in R | IBKR Quant Blog","og_description":"Learn how to plot XGBoost trees in R, and get sample code for using caret, dplyr and DiagrammeR packages.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/","og_site_name":"IBKR Campus US","article_published_time":"2021-07-07T13:50:00+00:00","article_modified_time":"2022-11-21T14:47:43+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/target-download.jpg","type":"image\/jpeg"}],"author":"Andrew Treadway","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Andrew Treadway","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/"},"author":{"name":"Andrew Treadway","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc"},"headline":"How to Plot XGBoost Trees in R","datePublished":"2021-07-07T13:50:00+00:00","dateModified":"2022-11-21T14:47:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/"},"wordCount":161,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/target-download.jpg","keywords":["caret","Data Science","dplyr and DiagrammeR","R","rstats","xgboost"],"articleSection":["Data Science","Programming Languages","Quant","Quant Development","Quant North America","Quant Regions","R Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/","name":"How to Plot XGBoost Trees in R | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/target-download.jpg","datePublished":"2021-07-07T13:50:00+00:00","dateModified":"2022-11-21T14:47:43+00:00","description":"Learn how to plot XGBoost trees in R, and get sample code for using caret, dplyr and DiagrammeR packages.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-plot-xgboost-trees-in-r\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/target-download.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/target-download.jpg","width":900,"height":550,"caption":"Quant Download"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc","name":"Andrew Treadway","description":"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\/\/www.linkedin.com\/in\/andrew-treadway-a3b19b103\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.","sameAs":["https:\/\/theautomatic.net\/about-me\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/andrewtreadway\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/05\/target-download.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/93825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/388"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=93825"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/93825\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/44292"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=93825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=93825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=93825"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=93825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}