---
title: "Get started with nestedscales"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Get started with nestedscales}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```

nestedscales provides a discrete representation of a data dimension at
several resolutions and the operations that move data between them. This
vignette introduces the representation and the main functions on a small
industry classification. The dimension packages
[timescales](https://github.com/optimal2050/timescales) and
[geoscales](https://github.com/optimal2050/geoscales) apply the same
functions to calendars and region hierarchies. The
[package website](https://optimal2050.github.io/nestedscales/) includes a
roadmap of the package's scope.

```{r setup}
library(nestedscales)
```

## Representation

A **nested scale** is a table with one row per leaf, the finest member of the
dimension, and one column per frame, a resolution at which the leaves are
grouped.
The frames are given coarsest first. Numeric columns that are not frames are
weights.

```{r build}
industries <- data.frame(
  section  = c("C", "C", "C", "D", "D"),
  division = c("C10", "C10", "C11", "D35", "D35"),
  class    = c("C101", "C102", "C110", "D351", "D352"),
  gva      = c(120, 80, 45, 300, 150)
)

ind <- scale_from_leaftable(
  industries,
  frames = c("section", "division", "class"),
  name   = "nace"
)
ind
```

The scale records the frames, the members at each frame, the leaf column and the
weights:

```{r accessors}
scale_frames(ind)
scale_members(ind, "division")
scale_leaf_column(ind)
scale_weights(ind)
```

Every frame partitions the leaves. Frames need not nest in each other: a
member at one frame may belong to several members at a coarser frame, and two
frames may be independent, in which case the leaves are their combinations.
`scale_nests()`, `scale_crosses()` and `scale_is_uniform()` report the
relation a given pair of frames has; no relation is assumed.

```{r structure}
scale_nests(ind, "section", "division")
scale_family(ind, "section", "division")
```

## Transition rules

Data keyed at one frame is converted to another frame with
`recast_scale()`. Each value column follows a rule from `SCALE_RULES`:

| rule | aggregation (fine to coarse) | disaggregation (coarse to fine) |
|---|---|---|
| `sum` | sum | split in proportion to the weight |
| `weighted_mean` | weighted mean | copy |
| `mean` | mean over leaves | copy |
| `copy` | the common value | copy |
| `sd` | standard deviation over leaves | not defined |
| `share` | value as a share of its parent's total | not defined |

The rule is given per value column, so extensive and intensive quantities
convert in one call:

```{r recast-up}
x <- data.frame(
  class = c("C101", "C102", "C110", "D351", "D352"),
  jobs  = c(12, 8, 5, 30, 20),
  wage  = c(31, 29, 27, 45, 40)
)

recast_scale(x, ind,
  source = "class", target = "section",
  rule = c(jobs = "sum", wage = "weighted_mean")
)
```

The same function converts in the other direction. A total at the section
frame is split over the classes in proportion to the weight:

```{r recast-down}
y <- data.frame(section = c("C", "D"), investment = c(100, 400))

recast_scale(y, ind, source = "section", target = "class", rule = "sum")
```

Columns that are neither the key nor a value column are identifiers and are
preserved as groups, so a panel of several years or scenarios converts in
one call. A rule may also be registered once for a named value column with
`register_scale_rule()`, after which `rule =` can be omitted for that
column.

## The same on time

Nothing above is specific to industries. Time is a dimension like any
other: minutes group into hours and hours into days. The scale below is
built from a table generated in code, two days of 24 hours of 60 minutes,
with one row per minute. The codes of the hours and of the minutes include
the day, so that every code names one member.

```{r time-scale}
grid <- expand.grid(m = 0:59, h = 0:23, d = 1:2)
clock <- data.frame(
  day    = sprintf("d%d", grid$d),
  hour   = sprintf("d%d_h%02d", grid$d, grid$h),
  minute = sprintf("d%d_h%02d_m%02d", grid$d, grid$h, grid$m)
)
time <- scale_from_leaftable(clock, frames = c("day", "hour", "minute"),
                             name = "clock")
time
```

A series recorded per minute is recast to hours and to days. A count, here
the visitors entering a shop, is summed; a reading, here the temperature of
the room, is averaged. Both series are invented.

```{r time-recast}
series <- data.frame(
  minute   = clock$minute,
  visitors = grid$h %/% 6 + 1,
  temp     = 18 + grid$h / 4
)
head(series, 3)

hourly <- recast_scale(series, time, source = "minute", target = "hour",
                       rule = c(visitors = "sum", temp = "mean"))
head(hourly, 3)

recast_scale(series, time, source = "minute", target = "day",
             rule = c(visitors = "sum", temp = "mean"))
```

The hourly table moves on in the same way, since `hour` is a frame like
any other:

```{r time-hours}
recast_scale(hourly[c("hour", "visitors")], time,
             source = "hour", target = "day", rule = "sum")
```

This example is only the bare mechanism: its minutes carry no dates, and
every hour and every day has the same length.
[timescales](https://github.com/optimal2050/timescales) builds on it for
calendars with real dates, time zones, months of unequal length and typical
or sampled periods.

## Labels

`join_scale()` attaches the scale's labels to a dataset: the member at the
keyed frame, optionally the coarser frames (`attach =`) and the member's share
and weight (`meta =`):

```{r join}
join_scale(x, ind, frame = "class", attach = TRUE)
```

## Subsets

`filter_scale()` keeps the leaves under given members; the result records what
fraction of each weight it retains. `prune_scale()` collapses a scale to a
coarser leaf frame.

```{r subset}
manufacturing <- filter_scale(ind, "section", "C")
scale_members(manufacturing)
scale_coverage(manufacturing)

scale_frames(prune_scale(ind, "division"))
```

## Attributes and aliases

A scale may carry values that belong to its members but are not weights:
the persons per household of a city, a price per item, a wage per employee.
They live in an attribute table keyed by `(frame, member)`, at whatever
frame they are published, and `scale_attributes()` resolves one value per
member of a frame, walking to the nearest coarser frame when a member has
none of its own. `convert_scale()` applies the resolved value row by row.
Alternative names live in an alias table, and `scale_match()` resolves
names against member names and aliases exactly, reporting ambiguity rather
than guessing. A small scale of cities shows all three: seven cities in
three regions in two countries, with a household size for region `N1` and
for city `e`, and a longer name for city `a`. The values are invented.

```{r attributes}
places <- scale_from_leaftable(
  data.frame(
    country = c("North", "North", "North", "North", "South", "South", "South"),
    region  = c("N1", "N1", "N2", "N2", "S1", "S1", "S1"),
    city    = c("a", "b", "c", "d", "e", "f", "g")
  ),
  frames = c("country", "region", "city"), name = "places",
  attributes = data.frame(
    frame = c("region", "city"), member = c("N1", "e"),
    attribute = "hh_size", kind = "property", value = c(2.5, 2),
    unit_of_measure = "persons/household", source = "example"
  ),
  aliases = data.frame(frame = "city", member = "a", alias = "Alder",
                       source = "example")
)
scale_attributes(places, "hh_size", frame = "city")[
  c("member", "value", "unit_of_measure", "origin_frame")]

people <- data.frame(city = c("a", "b", "e"), people = c(3, 2, 6))
convert_scale(people, places, "hh_size",
              from_unit = "persons", to_unit = "household")

scale_match(places, c("Alder", "B", "Zed"))
```

The cities `a` and `b` take the value of their region. The people are
counted in thousands, so the converted column holds thousands of
households.

`vignette("attributes")` has the full account.

## Definition files

A scale is stored as a definition: a JSON header with its tables inline, or
a bundle directory with Arrow or CSV tables, which the Python binding reads
as well. `write_scale()` and `read_scale()` exchange them, and
`scale_catalog()` lists the definitions installed packages ship:

```{r definition}
path <- file.path(tempdir(), "nace.json")
write_scale(ind, path)
scale_frames(read_scale(path))
scale_catalog()[c("id", "name", "frames", "n_leaves", "package")]
```

```{r definition-cleanup, include = FALSE}
unlink(path)
```

## Several dimensions

`scale_product()` combines scales into one multi-dimensional index without
materialising the product of their leaves. `recast_product()`,
`join_product()` and `filter_product()` apply the single-scale operations
axis by axis.

```{r product}
p <- scale_product(a = scale_example(), b = scale_example2())
product_size(p)
```

## Further operations

* `cluster_scale()` groups members by their data and returns the scale with
  the clusters as a new frame.
* `reconcile_scale()` compares aggregated data with an independent total.
* `as_scale_array()` converts long data to a dense array over a scale, and
  `write_scale_dataset()` stores data as an Arrow dataset together with its
  scales.

All operations accept a `data.frame`, tibble, `data.table`, dtplyr table or
arrow dataset and return the same class; lazy inputs return a query unless
`collect = TRUE`.
