Make comparisons between two versions of the same outcome by some grouping variables and rank them by the greatest mean absolute percentage change to the smallest.

rank_level(
  new_data,
  old_data,
  threshold = 0,
  comparison_var,
  id_vars,
  group_vars,
  displace_old_zero = 1e-10
)

Arguments

new_data

data.table() New version of data for comparison.

old_data

data.table() Old version of data for comparison.

threshold

numeric() The threshold must be greater than or equal to zero. It represents the maximum absolute percent difference of comparison_var by group_vars to be included in the analysis. The default is zero.

comparison_var

numeric() Outcome variable to make comparisons over.

id_vars

Vector of variable names that uniquely identify both new_data and old_data.

group_vars

Level to summarize change variables for ranking. group_vars are a subset of id_vars.

displace_old_zero

numeric() Displace zeros in old_data to prevent NaNs in change calculations. Default is 1e-10.

Value

data.table() of ranked merged data.tables, which may be a subset of the input data.tables. Rank number 1 corresponds to the greatest change in comparison_var by group_vars between new_data and old_data, rank number 2 corresponds to the second greatest change in comparison_var by group_vars, etc. NOTE: Ranks may be duplicated by group if they have the same values used for ranking.

Examples

rank_level(new_data, old_data, threshold = 5, comparison_var = "outcome", id_vars = c("year", "group"), group_vars = "group")
#> year group new_outcome old_outcome pert_diff max_abs_pert_diff #> 1: 1 b 12 3 300 300 #> 2: 2 b 8 2 300 300 #> 3: 3 b 4 1 300 300 #> 4: 1 c 0 1 -100 100 #> 5: 2 c 0 2 -100 100 #> 6: 3 c 0 3 -100 100 #> 7: 1 a 2 1 100 100 #> 8: 2 a 4 2 100 100 #> 9: 3 a 6 3 100 100 #> mean_abs_pert_diff rank #> 1: 300 1 #> 2: 300 1 #> 3: 300 1 #> 4: 100 2 #> 5: 100 2 #> 6: 100 2 #> 7: 100 2 #> 8: 100 2 #> 9: 100 2