lab-09/analysis.R

22 lines
1.2 KiB
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)
}