Linear regression — solution

Income, experience and education in a log-scale model.

Calculated chart: Linear regression
Figure 1 — Calculated chart: Linear regression

Fit least squares to log income. Holding other predictors fixed, a one-unit increase multiplies the fitted level by exp(β).

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 — Linear regression — solution
term multiplier
years 1.0325
education 1.0613

analysis.R

# Синтетические данные и расчёт метрик для обоих дашбордов.
set.seed(2026)
years <- runif(150, 0, 30)
education <- sample(10:18, 150, replace = TRUE)
income <- exp(9 + 0.035 * years + 0.07 * education + rnorm(150, 0, 0.22))
model <- lm(log(income) ~ years + education)
result <- data.frame(term = names(coef(model))[-1], multiplier = exp(unname(coef(model)[-1])))
stopifnot(abs(mean(residuals(model))) < 1e-10)

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

### Таблица

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

## Row

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

МНК минимизирует сумму квадратов остатков. В модели логарифма дохода изменение признака на единицу связано с множителем exp(β) при фиксированных остальных признаках.

superset.sql

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

Related projects, disciplines and materials