Make comparisons between two versions of the same outcome by some grouping variables where trend changes are ranked first by the number of slopes that changed sign and then by the greatest mean absolute percentage change to the smallest.

rank_trend(
  new_data,
  old_data,
  numb_bins = 1L,
  comparison_var,
  id_vars,
  group_vars,
  trend_var,
  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.

numb_bins

numeric() numb_bins represents the number of bins to use in the trend analysis within each grouping by group_vars. The default numb_bins is 1, which would examine the change in the slope (i.e. change in comparison_var over change in trend_var) by group_vars in the trend analysis.

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.

trend_var

numeric() Predictor variable used to examine trend changes. trend_var is a subset of id_vars of length 1.

displace_old_zero

numeric() Displace zeros in old_data trend 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_trend(new_data, old_data_alt, numb_bins = 1L, comparison_var = "outcome", id_vars = c("year", "group"), group_vars = "group", trend_var = "year")
#> group year new_outcome old_outcome bin old_slope new_slope sign_change #> 1: b 1 12 4 1 4 -4 1 #> 2: b 2 8 8 1 4 -4 1 #> 3: b 3 4 12 1 4 -4 1 #> 4: c 1 0 1 1 1 0 0 #> 5: c 2 0 2 1 1 0 0 #> 6: c 3 0 3 1 1 0 0 #> 7: a 1 2 2 1 2 2 0 #> 8: a 2 4 4 1 2 2 0 #> 9: a 3 6 6 1 2 2 0 #> pert_diff mean_abs_pert_diff numb_sign_change rank #> 1: -200 200 1 1 #> 2: -200 200 1 1 #> 3: -200 200 1 1 #> 4: -100 100 0 2 #> 5: -100 100 0 2 #> 6: -100 100 0 2 #> 7: 0 0 0 3 #> 8: 0 0 0 3 #> 9: 0 0 0 3