
R version 4.5.2 (2025-10-31) -- "[Not] Part in a Rumble"
Copyright (C) 2025 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> pkgname <- "piar"
> source(file.path(R.home("share"), "R", "examples-header.R"))
> options(warn = 1)
> library('piar')
> 
> base::assign(".oldSearch", base::search(), pos = 'CheckExEnv')
> base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv')
> cleanEx()
> nameEx("aggregate.piar_index")
> ### * aggregate.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: aggregate.piar_index
> ### Title: Aggregate elementary price indexes
> ### Aliases: aggregate.piar_index aggregate.chainable_piar_index
> ###   aggregate.direct_piar_index
> 
> ### ** Examples
> 
> prices <- data.frame(
+   rel = 1:8,
+   period = rep(1:2, each = 4),
+   ea = rep(letters[1:2], 4)
+ )
> 
> # A two-level aggregation structure
> 
> pias <- aggregation_structure(
+   list(c("top", "top", "top"), c("a", "b", "c")),
+   weights = 1:3
+ )
> 
> # Calculate Jevons elementary indexes
> 
> (elementary <- elementary_index(prices, rel ~ period + ea))
Period-over-period price index for 2 levels over 2 time periods 
      time
levels        1        2
     a 1.732051 5.916080
     b 2.828427 6.928203
> 
> # Aggregate (note the imputation for elementary index 'c')
> 
> (index <- aggregate(elementary, pias, na.rm = TRUE))
Period-over-period price index for 4 levels over 2 time periods 
      time
levels        1        2
   top 2.462968 6.690949
   a   1.732051 5.916080
   b   2.828427 6.928203
   c   2.462968 6.690949
> 
> # Aggregation can equivalently be done as matrix multiplication
> 
> as.matrix(pias) %*% as.matrix(chain(index[letters[1:3]]))
      time
levels        1       2
   top 2.462968 16.4796
> 
> 
> 
> 
> cleanEx()
> nameEx("aggregation_structure")
> ### * aggregation_structure
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: aggregation_structure
> ### Title: Make a price index aggregation structure
> ### Aliases: aggregation_structure piar_aggregation_structure
> 
> ### ** Examples
> 
> # A simple aggregation structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> #  (1)     (3)     (4)
> 
> aggregation_weights <- data.frame(
+   level1 = c("1", "1", "1"),
+   level2 = c("11", "11", "12"),
+   ea     = c("111", "112", "121"),
+   weight = c(1, 3, 4)
+ )
> 
> aggregation_structure(
+   aggregation_weights[1:3],
+   weights = aggregation_weights[[4]]
+ )
Aggregation structure for 3 elementary aggregates with 2 levels above the elementary aggregates 
  level1 level2  ea weight
1      1     11 111      1
2      1     11 112      3
3      1     12 121      4
> 
> # The aggregation structure can also be made by expanding the
> # elementary aggregates
> 
> with(
+   aggregation_weights,
+   aggregation_structure(expand_classification(ea), weight)
+ )
Aggregation structure for 3 elementary aggregates with 2 levels above the elementary aggregates 
  level1 level2  ea weight
1      1     11 111      1
2      1     11 112      3
3      1     12 121      4
> 
> 
> 
> 
> cleanEx()
> nameEx("as.data.frame.piar_index")
> ### * as.data.frame.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: as.data.frame.piar_index
> ### Title: Coerce an index into a tabular form
> ### Aliases: as.data.frame.piar_index as.matrix.piar_index
> 
> ### ** Examples
> 
> index <- as_index(matrix(1:6, 2))
> 
> as.data.frame(index)
  period level value
1      1     1     1
2      1     2     2
3      2     1     3
4      2     2     4
5      3     1     5
6      3     2     6
> 
> as.matrix(index)
      time
levels 1 2 3
     1 1 3 5
     2 2 4 6
> 
> 
> 
> 
> cleanEx()
> nameEx("as.matrix.piar_aggregation_structure")
> ### * as.matrix.piar_aggregation_structure
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: as.matrix.piar_aggregation_structure
> ### Title: Coerce an aggregation structure into a tabular form
> ### Aliases: as.matrix.piar_aggregation_structure
> ###   as.data.frame.piar_aggregation_structure
> 
> ### ** Examples
> 
> # A simple aggregation structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> #  (1)     (3)     (4)
> 
> aggregation_weights <- data.frame(
+   level1 = c("1", "1", "1"),
+   level2 = c("11", "11", "12"),
+   ea     = c("111", "112", "121"),
+   weight = c(1, 3, 4)
+ )
> 
> pias <- as_aggregation_structure(aggregation_weights)
> 
> as.matrix(pias)
      
levels   111   112 121
    1  0.125 0.375 0.5
    11 0.250 0.750 0.0
    12 0.000 0.000 1.0
> 
> all.equal(as.data.frame(pias), aggregation_weights)
[1] TRUE
> 
> ## Not run: 
> ##D # Visualize as a treemap.
> ##D treemap::treemap(
> ##D   aggregation_weights,
> ##D   index = names(aggregation_weights)[-4],
> ##D   vSize = "weight",
> ##D   title = "aggregation structure"
> ##D )
> ##D 
> ##D # Or turn into a more genereal tree object and plot.
> ##D aggregation_weights$pathString <- do.call(
> ##D   \(...) paste(..., sep = "/"),
> ##D   aggregation_weights[-4]
> ##D )
> ##D plot(data.tree::as.Node(aggregation_weights))
> ## End(Not run)
> 
> 
> 
> 
> cleanEx()
> nameEx("as.ts.piar_index")
> ### * as.ts.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: as.ts.piar_index
> ### Title: Coerce an index into a time series
> ### Aliases: as.ts.piar_index
> 
> ### ** Examples
> 
> as.ts(as_index(matrix(1:9, 3)))
Time Series:
Start = 1 
End = 3 
Frequency = 1 
  1 2 3
1 1 2 3
2 4 5 6
3 7 8 9
> 
> 
> 
> 
> cleanEx()
> nameEx("as_aggregation_structure")
> ### * as_aggregation_structure
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: as_aggregation_structure
> ### Title: Coerce to an aggregation structure
> ### Aliases: as_aggregation_structure as_aggregation_structure.default
> ###   as_aggregation_structure.data.frame as_aggregation_structure.matrix
> 
> ### ** Examples
> 
> # A simple aggregation structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> #  (1)     (3)     (4)
> 
> aggregation_weights <- data.frame(
+   level1 = c("1", "1", "1"),
+   level2 = c("11", "11", "12"),
+   ea     = c("111", "112", "121"),
+   weight = c(1, 3, 4)
+ )
> 
> pias <- aggregation_structure(
+   aggregation_weights[1:3],
+   weights = aggregation_weights[[4]]
+ )
> 
> all.equal(
+   pias,
+   as_aggregation_structure(aggregation_weights)
+ )
[1] TRUE
> 
> all.equal(
+   pias,
+   as_aggregation_structure(as.matrix(aggregation_weights))
+ )
[1] TRUE
> 
> 
> 
> 
> cleanEx()
> nameEx("as_index")
> ### * as_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: as_index
> ### Title: Coerce to a price index
> ### Aliases: as_index as_index.default as_index.matrix as_index.data.frame
> ###   as_index.chainable_piar_index as_index.direct_piar_index as_index.mts
> 
> ### ** Examples
> 
> prices <- data.frame(
+   rel = 1:8,
+   period = rep(1:2, each = 4),
+   ea = rep(letters[1:2], 4)
+ )
> 
> index <- elementary_index(prices, rel ~ period + ea)
> 
> all.equal(as_index(as.data.frame(index)), index)
[1] TRUE
> all.equal(as_index(as.matrix(index)), index)
[1] TRUE
> 
> 
> 
> 
> cleanEx()
> nameEx("chain")
> ### * chain
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: chain
> ### Title: Chain and rebase a price index
> ### Aliases: chain chain.default chain.chainable_piar_index unchain
> ###   unchain.default unchain.direct_piar_index rebase rebase.default
> ###   rebase.direct_piar_index
> 
> ### ** Examples
> 
> index <- as_index(matrix(1:9, 3))
> 
> # Make period 0 the fixed base period
> 
> chain(index)
Fixed-base price index for 3 levels over 3 time periods 
      time
levels 1  2   3
     1 1  4  28
     2 2 10  80
     3 3 18 162
> 
> # Chaining and unchaining reverse each other
> 
> all.equal(index, unchain(chain(index)))
[1] TRUE
> 
> # Change the base period to period 2 (note the
> # loss of information for period 0)
> 
> index <- chain(index)
> rebase(index, base = index[, 2])
Fixed-base price index for 3 levels over 3 time periods 
      time
levels         1 2 3
     1 0.2500000 1 7
     2 0.2000000 1 8
     3 0.1666667 1 9
> 
> 
> 
> 
> cleanEx()
> nameEx("combine_classifications")
> ### * combine_classifications
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: combine_classifications
> ### Title: Combine hierarchical classifications
> ### Aliases: combine_classifications
> 
> ### ** Examples
> 
> # Combine an unbalanced industry classification with a balanced
> # geographic classification
> 
> industry <- c("111", "112", "12")
> region <- c("11", "21", "22")
> 
> combine_classifications(
+   expand_classification(industry, pad = "0"),
+   expand_classification(region)
+ )
[[1]]
[1] "1" "1" "1"

[[2]]
[1] "11" "11" "12"

[[3]]
[1] "111" "112" "120"

[[4]]
[1] "111.1" "112.2" "120.2"

[[5]]
[1] "111.11" "112.21" "120.22"

> 
> 
> 
> cleanEx()
> nameEx("contrib")
> ### * contrib
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: contrib
> ### Title: Extract percent-change contributions
> ### Aliases: contrib contrib2DF contrib<- set_contrib
> ###   set_contrib_from_index
> 
> ### ** Examples
> 
> prices <- data.frame(
+   rel = 1:8,
+   period = rep(1:2, each = 4),
+   ea = rep(letters[1:2], 4)
+ )
> 
> index <- elementary_index(prices, rel ~ period + ea, contrib = TRUE)
> 
> pias <- aggregation_structure(
+   list(c("top", "top", "top"), c("a", "b", "c")),
+   weights = 1:3
+ )
> 
> index <- aggregate(index, pias, na.rm = TRUE)
> 
> # Percent-change contributions for the top-level index
> 
> contrib(index)
       time
product         1         2
    a.1 0.0000000 0.5081686
    a.2 0.2440169 0.6442213
    b.1 0.3905243 2.0513858
    b.2 0.8284271 2.4871732
> 
> contrib2DF(index)
  period level product     value
1      1   top     a.1 0.0000000
2      1   top     a.2 0.2440169
3      1   top     b.1 0.3905243
4      1   top     b.2 0.8284271
5      2   top     a.1 0.5081686
6      2   top     a.2 0.6442213
7      2   top     b.1 2.0513858
8      2   top     b.2 2.4871732
> 
> # Calculate EA contributions for the chained index
> 
> library(gpindex)
> 
> arithmetic_contributions(
+   as.matrix(chain(index))[c("a", "b", "c"), 2],
+   weights(pias)
+ )
       a        b        c 
1.541158 6.198639 7.739798 
> 
> 
> 
> 
> cleanEx()

detaching ‘package:gpindex’

> nameEx("cut.piar_aggregation_structure")
> ### * cut.piar_aggregation_structure
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: cut.piar_aggregation_structure
> ### Title: Cut an aggregation structure
> ### Aliases: cut.piar_aggregation_structure
> 
> ### ** Examples
> 
> # A simple aggregation structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> #  (1)     (3)     (4)
> 
> aggregation_weights <- data.frame(
+   level1 = c("1", "1", "1"),
+   level2 = c("11", "11", "12"),
+   ea     = c("111", "112", "121"),
+   weight = c(1, 3, 4)
+ )
> 
> pias <- aggregation_structure(
+   aggregation_weights[1:3],
+   weights = aggregation_weights[[4]]
+ )
> 
> # Turn it into
> #            1
> #      |-----+-----|
> #      11          12
> #     (4)         (4)
> 
> cut(pias, 2)
Aggregation structure for 2 elementary aggregates with 1 levels above the elementary aggregates 
  level1 level2 weight
1      1     11      4
2      1     12      4
> 
> 
> 
> 
> cleanEx()
> nameEx("elementary_index")
> ### * elementary_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: elementary_index
> ### Title: Make elementary/elemental price indexes
> ### Aliases: elementary_index elementary_index.default
> ###   elementary_index.numeric elementary_index.data.frame elemental_index
> 
> ### ** Examples
> 
> library(gpindex)
> 
> prices <- data.frame(
+   rel = 1:8,
+   period = rep(1:2, each = 4),
+   ea = rep(letters[1:2], 4)
+ )
> 
> # Calculate Jevons elementary indexes
> 
> elementary_index(prices, rel ~ period + ea)
Period-over-period price index for 2 levels over 2 time periods 
      time
levels        1        2
     a 1.732051 5.916080
     b 2.828427 6.928203
> 
> # Same as using lm() or tapply()
> 
> exp(coef(lm(log(rel) ~ ea:factor(period) - 1, prices)))
eaa:factor(period)1 eab:factor(period)1 eaa:factor(period)2 eab:factor(period)2 
           1.732051            2.828427            5.916080            6.928203 
> 
> with(
+   prices,
+   t(tapply(rel, list(period, ea), geometric_mean, na.rm = TRUE))
+ )
         1        2
a 1.732051 5.916080
b 2.828427 6.928203
> 
> # A general function to calculate weights to turn the geometric
> # mean of the arithmetic and harmonic mean (i.e., Fisher mean)
> # into an arithmetic mean
> 
> fw <- grouped(nested_transmute(0, c(1, -1), 1))
> 
> # Calculate a CSWD index (same as the Jevons in this example)
> # as an arithmetic index by using the appropriate weights
> 
> elementary_index(
+   prices,
+   rel ~ period + ea,
+   weights = fw(rel, group = interaction(period, ea)),
+   r = 1
+ )
Period-over-period price index for 2 levels over 2 time periods 
      time
levels        1        2
     a 1.732051 5.916080
     b 2.828427 6.928203
> 
> 
> 
> 
> cleanEx()

detaching ‘package:gpindex’

> nameEx("expand_classification")
> ### * expand_classification
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: expand_classification
> ### Title: Expand a hierarchical classification
> ### Aliases: expand_classification interact_classifications
> 
> ### ** Examples
> 
> # A simple classification structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> 
> expand_classification(c("111", "112", "121"))
[[1]]
[1] "1" "1" "1"

[[2]]
[1] "11" "11" "12"

[[3]]
[1] "111" "112" "121"

> 
> # Expanding more complex classifications
> # ... if last 'digit' is either TA or TS
> 
> expand_classification(
+   c("111TA", "112TA", "121TS"),
+   width = c(1, 1, 1, 2)
+ )
[[1]]
[1] "1" "1" "1"

[[2]]
[1] "11" "11" "12"

[[3]]
[1] "111" "112" "121"

[[4]]
[1] "111TA" "112TA" "121TS"

> 
> # ... if first 'digit' is either 11 or 12
> 
> expand_classification(c("111", "112", "121"), width = c(2, 1))
[[1]]
[1] "11" "11" "12"

[[2]]
[1] "111" "112" "121"

> 
> # ...if there are delimiters in the classification (like COICOP)
> 
> expand_classification(c("01.1.1", "01.1.2", "01.2.1"), width = 2)
[[1]]
[1] "01" "01" "01"

[[2]]
[1] "01.1" "01.1" "01.2"

[[3]]
[1] "01.1.1" "01.1.2" "01.2.1"

> 
> 
> 
> 
> cleanEx()
> nameEx("head.piar_index")
> ### * head.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: head.piar_index
> ### Title: Return the first/last parts of an index
> ### Aliases: head.piar_index tail.piar_index
> 
> ### ** Examples
> 
> index <- as_index(matrix(1:9, 3))
> 
> head(index, 1)
Period-over-period price index for 1 levels over 3 time periods 
      time
levels 1 2 3
     1 1 4 7
> 
> tail(index, 1)
Period-over-period price index for 1 levels over 3 time periods 
      time
levels 1 2 3
     3 3 6 9
> 
> 
> 
> 
> cleanEx()
> nameEx("impute_prices")
> ### * impute_prices
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: impute_prices
> ### Title: Impute missing prices
> ### Aliases: impute_prices impute_prices.default impute_prices.matrix
> ###   impute_prices.numeric impute_prices.data.frame carry_forward
> ###   carry_backward shadow_price
> 
> ### ** Examples
> 
> prices <- data.frame(
+   price = c(1:7, NA),
+   period = rep(1:2, each = 4),
+   product = 1:4,
+   ea = rep(letters[1:2], 4)
+ )
> 
> impute_prices(prices, price ~ period + product, method = "carry-forward")
[1] 1 2 3 4 5 6 7 4
>
> impute_prices(
+   prices,
+   price ~ period + product,
+   ea = ea,
+   method = "overall-mean"
+ )
[1]  1  2  3  4  5  6  7 12
>
> # Can also be done with current price-back price formulation.
> prices$back_price <- with(
+   prices,
+   price[gpindex::back_period(period, product)]
+ )
> 
> impute_prices(
+   prices,
+   cbind(price, back_price) ~ period + product,
+   ea = ea,
+   method = "overall-mean"
+ )
     price back_price
[1,]     1          1
[2,]     2          2
[3,]     3          3
[4,]     4          4
[5,]     5          1
[6,]     6          2
[7,]     7          3
[8,]    12          4
> 
> 
> 
> 
> cleanEx()
> nameEx("is.na.piar_index")
> ### * is.na.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: is.na.piar_index
> ### Title: Missing values in a price index
> ### Aliases: is.na.piar_index anyNA.piar_index
> 
> ### ** Examples
> 
> index <- as_index(matrix(c(1, 2, 3, NA, 5, NA), 2))
> 
> anyNA(index)
[1] TRUE
> is.na(index)
      time
levels     1     2     3
     1 FALSE FALSE FALSE
     2 FALSE  TRUE  TRUE
> 
> # Carry forward imputation
> 
> index[is.na(index)] <- 1
> index
Period-over-period price index for 2 levels over 3 time periods 
      time
levels 1 2 3
     1 1 3 5
     2 2 1 1
> 
> 
> 
> 
> cleanEx()
> nameEx("mean.piar_index")
> ### * mean.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: mean.piar_index
> ### Title: Aggregate a price index over subperiods
> ### Aliases: mean.piar_index mean.chainable_piar_index
> ###   mean.direct_piar_index
> 
> ### ** Examples
> 
> index <- as_index(matrix(c(1:12, 12:1), 2, byrow = TRUE), chainable = FALSE)
> 
> # Turn a monthly index into a quarterly index
> mean(index, window = 3)
Fixed-base price index for 2 levels over 4 time periods 
      time
levels  1 4 7 10
     1  2 5 8 11
     2 11 8 5  2
> 
> 
> 
> 
> cleanEx()
> nameEx("merge.piar_index")
> ### * merge.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: merge.piar_index
> ### Title: Merge price indexes
> ### Aliases: merge.piar_index merge.chainable_piar_index
> ###   merge.direct_piar_index
> 
> ### ** Examples
> 
> index1 <- as_index(matrix(1:6, 2))
> 
> index2 <- index1
> levels(index2) <- 3:4
> 
> merge(index1, index2)
Period-over-period price index for 4 levels over 3 time periods 
      time
levels 1 2 3
     1 1 3 5
     2 2 4 6
     3 1 3 5
     4 2 4 6
> 
> 
> 
> 
> cleanEx()
> nameEx("price_relative")
> ### * price_relative
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: price_relative
> ### Title: Calculate period-over-period price relatives
> ### Aliases: price_relative price_relative.default
> ###   price_relative.data.frame
> 
> ### ** Examples
> 
> price_relative(
+   1:6,
+   period = rep(1:2, each = 3),
+   product = rep(letters[1:3], 2)
+ )
  a   b   c   a   b   c 
1.0 1.0 1.0 4.0 2.5 2.0 
> 
> 
> 
> 
> cleanEx()
> nameEx("split.piar_index")
> ### * split.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: split.piar_index
> ### Title: Split an index into groups
> ### Aliases: split.piar_index split<-.piar_index
> 
> ### ** Examples
> 
> index <- as_index(matrix(1:6, 2))
> 
> split(index, 1:2)
$`1`
Period-over-period price index for 1 levels over 3 time periods 
      time
levels 1 2 3
     1 1 3 5

$`2`
Period-over-period price index for 1 levels over 3 time periods 
      time
levels 1 2 3
     2 2 4 6

> 
> split(index, c(1, 1, 2), along = "time")
$`1`
Period-over-period price index for 2 levels over 2 time periods 
      time
levels 1 2
     1 1 3
     2 2 4

$`2`
Period-over-period price index for 2 levels over 1 time periods 
      time
levels 3
     1 5
     2 6

> 
> 
> 
> 
> cleanEx()
> nameEx("split_classification")
> ### * split_classification
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: split_classification
> ### Title: Split a hierarchical classification
> ### Aliases: split_classification
> 
> ### ** Examples
> 
> #' # A simple classification structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> 
> split_classification(c("111", "112", "121"), "")
[[1]]
[1] "1" "1" "1"

[[2]]
[1] "1.1" "1.1" "1.2"

[[3]]
[1] "1.1.1" "1.1.2" "1.2.1"

> 
> # Useful if there are delimiters in the classification (like COICOP)
> 
> split_classification(c("01.1.1", "01.1.2", "01.2.1"), ".", fixed = TRUE)
[[1]]
[1] "01" "01" "01"

[[2]]
[1] "01.1" "01.1" "01.2"

[[3]]
[1] "01.1.1" "01.1.2" "01.2.1"

> 
> 
> 
> 
> cleanEx()
> nameEx("stack.piar_index")
> ### * stack.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: stack.piar_index
> ### Title: Stack price indexes
> ### Aliases: stack.piar_index stack.chainable_piar_index
> ###   stack.direct_piar_index unstack.chainable_piar_index
> ###   unstack.direct_piar_index
> 
> ### ** Examples
> 
> index1 <- as_index(matrix(1:6, 2))
> 
> index2 <- index1
> time(index2) <- 4:6
> 
> stack(index1, index2)
Period-over-period price index for 2 levels over 6 time periods 
      time
levels 1 2 3 4 5 6
     1 1 3 5 1 3 5
     2 2 4 6 2 4 6
> 
> # Unstack does the reverse
> 
> all.equal(
+   c(unstack(index1), unstack(index2)),
+   unstack(stack(index1, index2))
+ )
[1] TRUE
> 
> 
> 
> 
> cleanEx()
> nameEx("sub-.piar_index")
> ### * sub-.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: [.piar_index
> ### Title: Extract index values
> ### Aliases: [.piar_index [<-.piar_index
> 
> ### ** Examples
> 
> index <- as_index(matrix(1:6, 2))
> 
> index["1", ]
Period-over-period price index for 1 levels over 3 time periods 
      time
levels 1 2 3
     1 1 3 5
> 
> index[, 2]
Period-over-period price index for 2 levels over 1 time periods 
      time
levels 2
     1 3
     2 4
> 
> index[1, ] <- 1 # can be useful for doing specific imputations
> 
> index
Period-over-period price index for 2 levels over 3 time periods 
      time
levels 1 2 3
     1 1 1 1
     2 2 4 6
> 
> 
> 
> 
> cleanEx()
> nameEx("update.piar_aggregation_structure")
> ### * update.piar_aggregation_structure
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: update.piar_aggregation_structure
> ### Title: Update an aggregation structure
> ### Aliases: update.piar_aggregation_structure
> 
> ### ** Examples
> 
> # A simple aggregation structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> #  (1)     (3)     (4)
> 
> aggregation_weights <- data.frame(
+   level1 = c("1", "1", "1"),
+   level2 = c("11", "11", "12"),
+   ea     = c("111", "112", "121"),
+   weight = c(1, 3, 4)
+ )
> 
> pias <- as_aggregation_structure(aggregation_weights)
> 
> index <- as_index(
+   matrix(1:9, 3, dimnames = list(c("111", "112", "121"), NULL))
+ )
> 
> weights(pias, ea_only = FALSE)
$level1
1 
8 

$level2
11 12 
 4  4 

$ea
111 112 121 
  1   3   4 

> 
> weights(update(pias, index), ea_only = FALSE)
$level1
  1 
916 

$level2
 11  12 
268 648 

$ea
111 112 121 
 28 240 648 

> 
> 
> 
> 
> cleanEx()
> nameEx("weights.piar_aggregation_structure")
> ### * weights.piar_aggregation_structure
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: weights.piar_aggregation_structure
> ### Title: Get the weights for an aggregation structure
> ### Aliases: weights.piar_aggregation_structure weights<-
> ###   weights<-.piar_aggregation_structure set_weights
> 
> ### ** Examples
> 
> # A simple aggregation structure
> #            1
> #      |-----+-----|
> #      11          12
> #  |---+---|       |
> #  111     112     121
> #  (1)     (3)     (4)
> 
> aggregation_weights <- data.frame(
+   level1 = c("1", "1", "1"),
+   level2 = c("11", "11", "12"),
+   ea     = c("111", "112", "121"),
+   weight = c(1, 3, 4)
+ )
> 
> pias <- as_aggregation_structure(aggregation_weights)
> 
> # Extract the weights
> 
> weights(pias)
111 112 121 
  1   3   4 
> 
> # ... or update them
> 
> weights(pias) <- 1:3
> weights(pias)
111 112 121 
  1   2   3 
> 
> 
> 
> 
> cleanEx()
> nameEx("window.piar_index")
> ### * window.piar_index
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: window.piar_index
> ### Title: Window a price index
> ### Aliases: window.piar_index window<-.piar_index
> 
> ### ** Examples
> 
> x <- as_index(matrix(1:9, 3))
> 
> window(x, "2")
Period-over-period price index for 3 levels over 2 time periods 
      time
levels 2 3
     1 4 7
     2 5 8
     3 6 9
> 
> window(x, "2") <- 1
> x
Period-over-period price index for 3 levels over 3 time periods 
      time
levels 1 2 3
     1 1 1 1
     2 2 1 1
     3 3 1 1
> 
> 
> 
> 
> ### * <FOOTER>
> ###
> cleanEx()
> options(digits = 7L)
> base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
Time elapsed:  0.277 0.027 0.316 0 0 
> grDevices::dev.off()
null device 
          1 
> ###
> ### Local variables: ***
> ### mode: outline-minor ***
> ### outline-regexp: "\\(> \\)?### [*]+" ***
> ### End: ***
> quit('no')
