lab-07/analysis.R

21 lines
1 KiB
R

# Синтетические данные и расчёт метрик для обоих дашбордов.
set.seed(2026)
price <- runif(180, 1, 10)
discount <- rbinom(180, 1, 0.4)
bought <- rbinom(180, 1, plogis(1.5 - 0.4 * price + discount))
sample_data <- data.frame(bought, price, discount)
model <- glm(bought ~ price + discount, data = sample_data[1:120, ], family = binomial())
p <- predict(model, newdata = sample_data[121:180, ], type = "response")
actual <- sample_data$bought[121:180]
threshold <- seq(0.2, 0.8, by = 0.1)
result <- data.frame(threshold,
sensitivity = vapply(threshold, function(t) mean(p[actual == 1] >= t), numeric(1)),
specificity = vapply(threshold, function(t) mean(p[actual == 0] < t), numeric(1)))
# Проверяет конечность основной метрики перед экспортом.
stopifnot(nrow(result) > 1, all(is.finite(result$sensitivity)))
if (sys.nframe() == 0L) {
arguments <- commandArgs(trailingOnly = TRUE)
stopifnot(length(arguments) == 1L)
write.csv(result, arguments[1], row.names = FALSE)
}