
Fit a binomial model with price and discount. Compute held-out sensitivity and specificity at each decision threshold.
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
| threshold | sensitivity | specificity |
|---|---|---|
| 0.2 | 0.96552 | 0.32258 |
| 0.3 | 0.89655 | 0.35484 |
| 0.4 | 0.79310 | 0.54839 |
| 0.5 | 0.68966 | 0.67742 |
| 0.6 | 0.55172 | 0.74194 |
| 0.7 | 0.34483 | 0.93548 |
| 0.8 | 0.31034 | 1.00000 |
analysis.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)
}
dashboard.qmd
---
title: "Логистическая регрессия"
lang: ru
format: dashboard
execute:
echo: false
---
```{r}
source("analysis.R", local = TRUE)
```
# Результаты
## Row
### График
```{r}
x <- result$threshold
y <- result$sensitivity
if (is.numeric(x)) {
plot(x, y, xlab = "threshold", ylab = "sensitivity", pch = 19, col = "#245d76")
} else {
par(mar = c(7, 4, 2, 1))
barplot(y, names.arg = x, ylab = "sensitivity", las = 2, col = "#245d76")
}
```
### Таблица
```{r}
knitr::kable(result, digits = 3)
```
## Row
### Как читать результат
Логит моделирует log(p/(1−p)). Чувствительность и специфичность меняются с порогом. Их оценивают на данных вне обучающей выборки.
superset.sql
-- Замените portfolio_results именем таблицы, куда импортирован results.csv.
SELECT threshold, sensitivity
FROM portfolio_results
ORDER BY threshold;