
Support is P(A∩B), confidence is P(B|A), and lift is confidence divided by P(B). Lift above one signals a positive sample association.
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
| rule | support | confidence | lift |
|---|---|---|---|
| milk_to_bread | 0.440 | 0.80734 | 1.21400 |
| cheese_to_bread | 0.215 | 0.57333 | 0.86216 |
| bread_to_milk | 0.440 | 0.66165 | 1.21400 |
| cheese_to_milk | 0.200 | 0.53333 | 0.97859 |
| bread_to_cheese | 0.215 | 0.32331 | 0.86216 |
| milk_to_cheese | 0.200 | 0.36697 | 0.97859 |
analysis.R
# Синтетические данные и расчёт метрик для обоих дашбордов.
set.seed(2026)
bread <- rbinom(200, 1, 0.6) == 1
items <- list(bread = bread, milk = runif(200) < ifelse(bread, 0.72, 0.3),
cheese = rbinom(200, 1, 0.4) == 1)
pairs <- expand.grid(a = names(items), b = names(items), stringsAsFactors = FALSE)
pairs <- pairs[pairs$a != pairs$b, ]
support <- mapply(function(a, b) mean(items[[a]] & items[[b]]), pairs$a, pairs$b)
confidence <- support / vapply(items[pairs$a], mean, numeric(1))
result <- data.frame(rule = paste(pairs$a, pairs$b, sep = "_to_"), support, confidence,
lift = confidence / vapply(items[pairs$b], mean, numeric(1)))
# Проверяет конечность основной метрики перед экспортом.
stopifnot(nrow(result) > 1, all(is.finite(result$lift)))
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$rule
y <- result$lift
if (is.numeric(x)) {
plot(x, y, xlab = "rule", ylab = "lift", pch = 19, col = "#245d76")
} else {
par(mar = c(7, 4, 2, 1))
barplot(y, names.arg = x, ylab = "lift", las = 2, col = "#245d76")
}
```
### Таблица
```{r}
knitr::kable(result, digits = 3)
```
## Row
### Как читать результат
Поддержка равна P(A∩B), доверие — P(B|A), lift — P(A∩B)/(P(A)P(B)). Lift выше единицы означает положительную ассоциацию в выборке.
superset.sql
-- Замените portfolio_results именем таблицы, куда импортирован results.csv.
SELECT rule, lift
FROM portfolio_results
ORDER BY rule;