SHARE
TWEET

Untitled

a guest Jun 19th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(randomForest)
  2. library(ggplot2)
  3. library(randomForestExplainer)
  4.  
  5. n <- 100
  6. p <- 4
  7.  
  8. # Create data:
  9. xrandom <- matrix(rnorm(n*p)+5, nrow=n)
  10. colnames(xrandom)<- paste0("x",2:5)
  11. d <- data.frame(xrandom)
  12. d$x1 <- factor(sample(1:2, n, replace=T))
  13.  
  14. # Equation:
  15. y <- d$x2 + rnorm(n)/5
  16.  
  17. y[d$x1==1] <- y[d$x1==1]+5
  18. d$y <- y
  19.  
  20. # Random Forest:
  21. fr <- randomForest(y ~ ., data=d,localImp=T)
  22.  
  23. # Random Forest Explainer:
  24. interactions_frame <- min_depth_interactions(fr, names(d)[-6])
  25. head(interactions_frame, 2)
  26.      
  27. variable    root_variable mean_min_depth occurrences interaction
  28. 1       x1            x1       4.670732           0       x1:x1
  29. 2       x1            x2       2.606190         221       x2:x1
  30.   uncond_mean_min_depth
  31. 1              1.703252
  32. 2              1.703252
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top