---
title: "Imputation"
vignette: >
  %\VignetteIndexEntry{Imputation}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
knitr:
  opts_chunk: 
    collapse: true
    comment: '#>'
---

The example in `vignette("piar")` used parental imputation to both
impute missing price relatives when calculating the elementary indexes
and to impute missing elementary indexes during aggregation. Although
parental imputation is simple and transparent, it is not the only way to
impute missing prices or index values.

## Imputing missing prices

Instead of implicitly imputing missing price relatives by ignoring
missing values, a common explicit (but methodologically dubious)
imputation strategy when making elementary indexes is to carry forward
the previous price to impute for missing prices. As the
`elementary_index()` function accepts price relatives as its input, any
imputations can be done prior to passing price relatives to this
function. (Missing values still need to be removed in this example
because not all missing prices can be imputed.)

```{r}
library(piar)

elementals <- ms_prices |>
  transform(
    imputed_price = impute_prices(
      price,
      period = period,
      product = product,
      method = "carry-forward"
    )
  ) |>
  elementary_index(
    price_relative(imputed_price, period = period, product = product) ~
      period + business,
    na.rm = TRUE
  )

elementals
```

## Non-parental imputation during aggregation

Parental imputation is the usual way to impute missing elementary index
values during aggregation, and it is simple to do with `aggregate()`. In
some cases, however, an elementary index may get imputed with the value
for, say, another elementary aggregate, rather than for an entire group
of elementary aggregates. The simplest way to do this sort of imputation
is to alter the elementary indexes prior to aggregation.

As an example, suppose that missing index values for business B4 should
be imputed as 1, rather than the value for group 12. This replacement
can be done as if the index was a matrix.

```{r}
elementals2 <- elementals
elementals2["B4", 1:3] <- 1

elementals2
```

Aggregating these elementary indexes now incorporates this imputation.

```{r}
ms_weights[c("level1", "level2")] <-
  expand_classification(ms_weights$classification)

pias <- ms_weights[c("level1", "level2", "business", "weight")] |>
  as_aggregation_structure()

aggregate(elementals2, pias, na.rm = TRUE)
```

It is also possible to supply a function with these rules that can be used
by `aggregate()`.

```{r}
impute <- function(x, pias) {
  if (is.na(x["B4"])) x["B4"] <- 1
  x
}

aggregate(elementals, pias, na.rm = TRUE, impute_rules = impute)
```
