Decision Trees
I am working with the decision tree chart (v1.0.1.0) in the latest version of Power BI (2.119.986.0 64-bit / July 2023). For some reason, the text at the bottom indicating Rel error, CVal error, Root error, and cp does not appear (despite having Additional parameters > show info toggled on). Any suggestions on how to get this information to appear in the chart? Thank you!
- lyndess3 years agoRegular Visitor
After continued investigation into the problem, I discovered that the "issue" is actually a design choice.
The Cause of the Problem
The decision tree visual in Power BI was designed for classification trees, not regression trees—although the visual certainly works for both (because the underlying R code is based on rpart, which supports both). How do I know this? When you add the visual to a Power BI report, you are prompted to enable "script visuals." If instead you click the "Review" button, you can actually see the underlying R code.
Not too far down, you'll come across these comments:
# WARNINGS: # This visual intended to be used for classification tasks. It was not tested for regression trees.
... And if you scroll down much further, you'll can find the code that prints out the error text at the bottom:
#info for classifier if( showInfo && !is.null(dtree) && dtree$method == 'class') pbiInfo <- paste("Rel error = ", d2form(opt$relErr * rooNodeErr), "; CVal error = ", d2form(opt$xerror * rooNodeErr), "; Root error = ", d2form(rooNodeErr), ";cp = ", d2form(opt$CP, 3), sep = "")Notice the first line of the "if" statement: "... dtree$method == 'class'. As a result of this conditional, the visual will only show the error text at the bottom if the tree is a classification tree—and not a regression tree.
(By the way: you can also review the full text of the R code on GitHub: https://github.com/microsoft/PowerBI-visuals-decision-tree/blob/master/script.r)
Solutions
If you need to see the error values, there are at least a few solutions to this problem. The first would be to change your tree from a regression tree to a classification tree by converting your target variable from a continuous variable to a categorical variable. Once you do this, the error values will show up at the bottom.
A second solution, of course, would be to build your decision tree directly in R, or create your own custom R visualization in Power BI. In both cases, however, the text at the bottom still will not appear by default; you would need to manually generate that text by referencing the error values (as is done in the published decision tree chart visual). If you are interested in going down this path, here are a few links that will help you:
- Creating decision trees in R: https://www.rdocumentation.org/packages/rpart/versions/4.1.19/topics/rpart
- To get the tree to look exactly like the one in Power BI: https://www.rdocumentation.org/packages/rattle/versions/5.5.1/topics/fancyRpartPlot
- Getting your data out of Power BI and into R: https://datakuity.com/2018/02/19/export-data-from-power-bi-into-a-file-using-r/
- Creating R visuals in Power BI: https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-r-visuals