## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(rankimp)

set.seed(1)
n <- 200
z <- rnorm(n)
df <- data.frame(
  x1 = z + rnorm(n, sd = 1 / 3),   # correlation of about 0.9 with x2
  x2 = z + rnorm(n, sd = 1 / 3),
  x3 = rnorm(n),
  x4 = rnorm(n),
  x5 = rnorm(n)
)
df$y <- 1.5 * z + 0.8 * df$x3 + rnorm(n)

round(cor(df$x1, df$x2), 2)

## ----panel--------------------------------------------------------------------
library(randomForest)

set.seed(2)
fit <- randomForest(y ~ ., data = df, ntree = 200)

J <- importance_judges(
  fit,
  methods = c("permutation", "loco"),
  data = df, target = "y",
  seeds = 1:3, n_perm = 5
)
J

## ----scores-------------------------------------------------------------------
round(attr(J, "scores"), 3)

## ----consensus----------------------------------------------------------------
cr <- consensus_rank(J)
cr$tau

## ----clusters-----------------------------------------------------------------
het <- judge_clusters(J)
het

## ----test---------------------------------------------------------------------
het$test[c("statistic", "p_value", "null_mean")]

## ----plot, fig.width = 6, fig.height = 4--------------------------------------
autoplot(het)

