
Estimate Naive Bayes posteriors for the four binary feature combinations. Laplace-smoothed rates (k+1)/(n+2) avoid zero estimates.
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
| pattern | probability |
|---|---|
| 0 | 0.060016 |
| 10 | 0.446500 |
| 1 | 0.248200 |
| 11 | 0.806620 |
analysis.R
# Синтетические данные и расчёт метрик для обоих дашбордов.
set.seed(2026)
class <- rbinom(160, 1, 0.45)
inputs <- cbind(rbinom(160, 1, 0.2 + 0.55 * class), rbinom(160, 1, 0.4 + 0.35 * class))
patterns <- expand.grid(x = 0:1, y = 0:1)
scores <- sapply(0:1, function(label) {
selected <- inputs[class == label, , drop = FALSE]
rate <- (colSums(selected) + 1) / (nrow(selected) + 2)
mean(class == label) * apply(patterns, 1, function(row) prod(dbinom(row, 1, rate)))
})
result <- data.frame(pattern = paste(patterns$x, patterns$y, sep = "_"),
probability = scores[, 2] / rowSums(scores))
# Проверяет конечность основной метрики перед экспортом.
stopifnot(nrow(result) > 1, all(is.finite(result$probability)))
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$pattern
y <- result$probability
if (is.numeric(x)) {
plot(x, y, xlab = "pattern", ylab = "probability", pch = 19, col = "#245d76")
} else {
par(mar = c(7, 4, 2, 1))
barplot(y, names.arg = x, ylab = "probability", las = 2, col = "#245d76")
}
```
### Таблица
```{r}
knitr::kable(result, digits = 3)
```
## Row
### Как читать результат
При условной независимости вес класса равен произведению априорной вероятности и правдоподобий. Оценка (k+1)/(n+2) устраняет нулевые вероятности.
superset.sql
-- Замените portfolio_results именем таблицы, куда импортирован results.csv.
SELECT pattern, probability
FROM portfolio_results
ORDER BY pattern;