---
title: "The Story: Equations from District 7"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{The Story: Equations from District 7}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 7,
  fig.height = 5
)
library(RougeLM)
```

> *"Pamiętaj, Beta. Statystyka to nie prawda.
> To narzędzie do kwestionowania kłamstw."*
>
> — The Statistician, District 9

---

## WaszKrak, 2047

Eighteen million people. Twenty-five districts. One algorithm.

LifeCalc — QuantumCorp's master scoring engine — assigns every resident of
WaszKrak a number updated every Friday at midnight. The number determines
access to healthcare, credit, employment programmes, and district
reclassification. It is described, in the official documentation, as
objective.

It was trained on fourteen years of historical data.

**Beta** is a mathematician with neural implants connected directly to data
streams. She can see probability distributions the way other people see
colour. She left QuantumCorp after discovering that her own models were
being used for algorithmic district segregation.

**Bit** is a self-taught programmer from the outskirts, where algorithms
assign social scores before people are born. His younger sister Ola was
denied treatment by a medical AI that calculated her expected quality-adjusted
life years at twelve — too few to justify the cost. She was eight years old.

They work from a squat in District 7. The walls are covered in printouts
of regressions, correlation matrices, and confusion tables. The servers
are salvaged from corporate landfill.

---

## What this package contains

`RougeLM` provides the datasets Beta and Bit collected over twelve months —
one case at a time, one subscore at a time — before they finally saw the
full picture.

```{r datasets, echo = FALSE}
knitr::kable(
  data.frame(
    Dataset   = c("`medical`", "`curiosity`", "`curiosity_quantum`",
                  "`mobility`", "`employment`", "`nutrition`", "`lifecalc`"),
    Chapter   = c("Strong Enough / The Shape of Cost",
                  "The Same Direction",
                  "The Threshold",
                  "The Crossing",
                  "The Second Chance",
                  "The Outlier",
                  "The Wall"),
    Method    = c("Simple regression, Box-Cox",
                  "One-way ANOVA",
                  "Piecewise regression, MED",
                  "Two-way ANOVA, interaction",
                  "ANCOVA, cream skimming",
                  "Nested ANOVA, fixed effects",
                  "Multiple regression, LASSO, Ridge"),
    n         = c(96, 1200, 1012, 412, 431, 1066, 5000)
  ),
  col.names = c("Dataset", "Chapter", "Method", "n")
)
```

---

## The full picture

After twelve months and six cases, Ghost — their data broker, the one who
never gives anything for free — sent them the complete LifeCalc dataset.
No charge. No explanation. Just a file and one sentence:

> *Knew you'd get here eventually.*

The dataset had 47 variables. After LASSO regularisation, four survived
with non-zero coefficients.

```{r lasso-preview, eval = FALSE}
library(RougeLM)
library(glmnet)

data(lifecalc)

X  <- model.matrix(SocialScore ~ ., data = lifecalc)[, -1]
y  <- lifecalc$SocialScore
cv <- cv.glmnet(X, y, alpha = 1, nfolds = 10)

coef(cv, s = "lambda.min")[
  coef(cv, s = "lambda.min")[, 1] != 0, , drop = FALSE
]
```

The variable with the largest coefficient — larger than district, larger
than employment, larger than education — was named `prior_flag`.

A binary flag. Zero or one. Indicating whether any parent of the resident
had ever recorded an EmploymentScore below 25 during the resident's
childhood.

Set before you were born. Permanent. Non-reversible.

Bit checked his own profile in the system.

`prior_flag`: **1**.

---

## Getting started

```{r quickstart}
library(RougeLM)

# The dataset from the first chapter
data(medical)
head(medical)

# Correlation between LifeContract partner medical
cor(medical$partner_a, medical$partner_b)

# Simple regression
model <- lm(partner_b ~ partner_a, data = medical)
coef(model)
```

Each dataset has a vignette. Each vignette has a story. Each story has
a number. The number is never just a number.

---

*"We're poor but we have something most data scientists don't."*

*"What?"*

*"The ability to look in a mirror without disgust."*
