
Choose the pruning level by cross-validation, then estimate the ROC on held-out observations.
Run Rscript --vanilla analysis.R results.csv and quarto render dashboard.qmd. The same synthetic result table can be connected to Superset using the SQL below.
Calculated results
| false_positive | true_positive |
|---|---|
| 0.00000 | 0.00000 |
| 0.17241 | 0.22581 |
| 0.31034 | 0.25806 |
| 0.48276 | 0.48387 |
| 0.51724 | 0.54839 |
| 0.51724 | 0.64516 |
| 0.75862 | 0.80645 |
| 0.82759 | 0.96774 |
| 1.00000 | 1.00000 |
| 1.00000 | 1.00000 |
analysis.R
# Синтетические данные и расчёт метрик для обоих дашбордов.
set.seed(2026)
x <- rnorm(180)
y <- rnorm(180)
sample_data <- data.frame(outcome = factor(as.integer(x * y + rnorm(180, 0, 0.4) > 0)), x, y)
tree <- rpart::rpart(outcome ~ x + y, sample_data[1:120, ], method = "class",
control = rpart::rpart.control(cp = 0.001, minsplit = 12))
cp <- tree$cptable[which.min(tree$cptable[, "xerror"]), "CP"]
p <- predict(rpart::prune(tree, cp), sample_data[121:180, ], type = "prob")[, "1"]
actual <- sample_data$outcome[121:180]
thresholds <- c(Inf, sort(unique(p), decreasing = TRUE), -Inf)
result <- data.frame(
false_positive = vapply(thresholds, function(t) mean(p[actual == "0"] >= t), numeric(1)),
true_positive = vapply(thresholds, function(t) mean(p[actual == "1"] >= t), numeric(1)))
# Проверяет конечность основной метрики перед экспортом.
stopifnot(nrow(result) > 1, all(is.finite(result$true_positive)))
if (sys.nframe() == 0L) {
arguments <- commandArgs(trailingOnly = TRUE)
stopifnot(length(arguments) == 1L)
write.csv(result, arguments[1], row.names = FALSE)
}
dashboard.qmd
---
title: "Дерево решений и ROC"
lang: ru
format: dashboard
execute:
echo: false
---
```{r}
source("analysis.R", local = TRUE)
```
# Результаты
## Row
### График
```{r}
x <- result$false_positive
y <- result$true_positive
if (is.numeric(x)) {
plot(x, y, xlab = "false_positive", ylab = "true_positive", pch = 19, col = "#245d76")
} else {
par(mar = c(7, 4, 2, 1))
barplot(y, names.arg = x, ylab = "true_positive", las = 2, col = "#245d76")
}
```
### Таблица
```{r}
knitr::kable(result, digits = 3)
```
## Row
### Как читать результат
Обрезка исключает разбиения с малым вкладом. ROC сопоставляет доли истинно и ложноположительных ответов при разных порогах.
superset.sql
-- Замените portfolio_results именем таблицы, куда импортирован results.csv.
SELECT false_positive, true_positive
FROM portfolio_results
ORDER BY false_positive;