Sync portfolio laboratory source

This commit is contained in:
portfolio 2026-11-26 13:13:27 +03:00
commit 9a6204df95
5 changed files with 93 additions and 0 deletions

22
analysis.R Normal file
View file

@ -0,0 +1,22 @@
# Синтетические данные и расчёт метрик для обоих дашбордов.
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)
}