Income distribution — solution

Comparing medians and spread across synthetic groups.

Calculated chart: Income distribution
Figure 1 — Calculated chart: Income distribution

Compare median income across three synthetic groups. The median is the 0.5 quantile. Taking logs preserves ranks and compresses the right tail.

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

Table 1 — Income distribution — solution
group median log_median
A 22620 10.027
B 26292 10.177
C 45123 10.717

analysis.R

# Синтетические данные и расчёт метрик для обоих дашбордов.
set.seed(2026)
group <- rep(c("A", "B", "C"), each = 50)
income <- rlnorm(length(group), rep(c(10, 10.3, 10.6), each = 50), 0.5)
result <- aggregate(cbind(median = income, log_median = log(income)), list(group = group), median)

# Проверяет конечность основной метрики перед экспортом.
stopifnot(nrow(result) > 1, all(is.finite(result$median)))
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$group
y <- result$median
if (is.numeric(x)) {
  plot(x, y, xlab = "group", ylab = "median", pch = 19, col = "#245d76")
} else {
  par(mar = c(7, 4, 2, 1))
  barplot(y, names.arg = x, ylab = "median", las = 2, col = "#245d76")
}
```

### Таблица

```{r}
knitr::kable(result, digits = 3)
```

## Row

### Как читать результат

Медиана — квантиль уровня 0.5. Монотонный логарифм сохраняет порядок наблюдений и уменьшает визуальный разброс правого хвоста.

superset.sql

-- Замените portfolio_results именем таблицы, куда импортирован results.csv.
SELECT group, median
FROM portfolio_results
ORDER BY group;

Related projects, disciplines and materials