datastorm-open / visNetwork

R package, using vis.js library for network visualization
Other
544 stars 126 forks source link

visTree: Make .generateYColor robust towards NaN and degenerate trees #443

Open vspinu opened 2 years ago

vspinu commented 2 years ago

Make color ramp generation robust against two degenerate cases:

  1. Presence of NaN values in object$frame$yval.
  2. Duplicated values of object$frame$yval which might result in yval range in a tree being 0 which results in division by 0 in this line.

Both cases can occur in trees which were refit to new data. That is, yval values and node counts are recomputed on new data but preserving the tree structure. Unfortunately there is no built in functionality for such a "refit" in rpart, so I have my own custom code for that. Thus cannot provide a simple repro. But hopefully the PR is simple enough not to require that.

An example of such a tree:

n= 2944 

node), split, n, deviance, yval
      * denotes terminal node

1) root 2944 1940000000 257  
  2) var< 2.17e+03 2944  278000000 257 *
  3) var>=2.17e+03 0 1640000000 NaN *