Learn R Programming

SSBtools (version 1.7.0)

max_contribution: Find Major Contributions to Aggregates and Count Contributors

Description

These functions analyze contributions to aggregates, assuming that the aggregates are calculated using a dummy matrix with the formula: z = t(x) %*% y.

Usage

max_contribution(
  x,
  y,
  n = 1,
  id = NULL,
  output = "y",
  drop = TRUE,
  decreasing = TRUE,
  remove_fraction = NULL,
  do_abs = TRUE
)

n_contributors(x, y = rep(1L, nrow(x)), id = NULL, output = "n_contr", ...)

Value

A list or a single element, depending on the values of the output and drop parameters.

Arguments

x

A (sparse) dummy matrix

y

A numeric vector of input values (contributions).

n

Integer. The number of largest contributors to identify for each aggregate. Default is 1.

id

An optional vector for grouping. When non-NULL, major contributions are found after aggregation within each group specified by id. Aggregates with missing id values are excluded.

output

A character vector specifying the desired output. Possible values:

  • "y": A matrix with the largest contributions in the first column, the second largest in the second column, and so on.

  • "id": A matrix of IDs associated with the largest contributions. If an id vector is provided, it returns these IDs; otherwise, it returns indices.

  • "n_contr": An integer vector indicating the number of contributors to each aggregate.

  • "n_0_contr": An integer vector indicating the number of contributors that contribute a value of 0 to each aggregate.

  • "n_non0_contr": An integer vector indicating the number of contributors that contribute a nonzero value to each aggregate.

  • "sums": A numeric vector containing the aggregate sums of y.

  • "n_contr_all", "n_0_contr_all", "n_non0_contr_all", "sums_all": Same as the corresponding outputs above, but without applying the remove_fraction parameter.

drop

Logical. If TRUE (default) and output has length 1, the function returns the single list element directly instead of a list containing one element.

decreasing

Logical. If TRUE (default), finds the largest contributors. If FALSE, finds the smallest contributors.

remove_fraction

A numeric vector containing values in the interval [0, 1], specifying contributors to be removed when identifying the largest contributions.

  • If an id vector is provided, remove_fraction must be named according to the IDs of the contributors to be removed.

  • If no id vector is provided, the length of remove_fraction must match the length of y. In this case, contributors not to be removed should have a value of NA in remove_fraction.

  • The actual values in remove_fraction are used for calculating "sums" (see description above).

do_abs

Logical. If TRUE (default), uses the absolute values of the summed contributions. The summation is performed for all contributions from the same contributor, within each aggregate being computed.

...

Further arguments to max_contribution (used by n_contributors).

Details

The max_contribution function identifies the largest contributions to these aggregates, while the wrapper function n_contributors is designed specifically to count the number of contributors for each aggregate.

Examples

Run this code

z <- SSBtoolsData("magnitude1")
a <- ModelMatrix(z, formula = ~sector4 + geo, crossTable = TRUE)

cbind(a$crossTable, 
      y =  max_contribution(x = a$modelMatrix, y = z$value, n = 2), 
      id = max_contribution(x = a$modelMatrix, y = z$value, n = 2, output = "id"),
      n =  n_contributors(  x = a$modelMatrix, y = z$value, n = 2))

cbind(a$crossTable, 
      y = max_contribution(x = a$modelMatrix, y = z$value, n = 3, id = z$company), 
      id = max_contribution(a$modelMatrix, z$value, 3, id = z$company, output = "id"))

max_contribution(x = a$modelMatrix, 
                 y = z$value, 
                 n = 3, 
                 id = z$company, 
                 output = c("y", "id", "n_contr", "sums"))

as.data.frame(
  max_contribution(x = a$modelMatrix, 
                   y = z$value, 
                   n = 3, 
                   id = z$company, 
                   output = c("y", "id", "n_contr", "sums", "n_contr_all", "sums_all"), 
                   remove_fraction = c(B = 1)))

Run the code above in your browser using DataLab