| Type: | Package |
| Title: | Perform a Relative Weights Analysis |
| Version: | 0.1.0 |
| Description: | Perform a Relative Weights Analysis (RWA) (a.k.a. Key Drivers Analysis) as per the method described in Tonidandel & LeBreton (2015) <doi:10.1007/s10869-014-9351-z>, with its original roots in Johnson (2000) <doi:10.1207/S15327906MBR3501_1>. In essence, RWA decomposes the total variance predicted in a regression model into weights that accurately reflect the proportional contribution of the predictor variables, which addresses the issue of multi-collinearity. In typical scenarios, RWA returns similar results to Shapley regression, but with a significant advantage on computational performance. |
| License: | GPL-3 |
| Encoding: | UTF-8 |
| URL: | https://martinctc.github.io/rwa/,https://github.com/martinctc/rwa |
| BugReports: | https://github.com/martinctc/rwa/issues |
| RoxygenNote: | 7.3.2 |
| Imports: | dplyr, magrittr, stats, tidyr, ggplot2, boot, purrr, utils |
| Suggests: | knitr, rmarkdown, testthat (≥ 3.0.0), rlang, spelling |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| Language: | en-US |
| NeedsCompilation: | no |
| Packaged: | 2025-07-16 14:49:54 UTC; martinchan |
| Author: | Martin Chan [aut, cre] |
| Maintainer: | Martin Chan <martinchan53@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2025-07-16 15:20:02 UTC |
Pipe operator
Description
Seemagrittr::%>% for details.
Usage
lhs %>% rhsExtract confidence intervals from bootstrap object
Description
Extract confidence intervals from bootstrap object
Usage
extract_ci( boot_object, conf_level = 0.95, variable_names = NULL, ci_type = "raw")Arguments
boot_object | Boot object from boot::boot() |
conf_level | Confidence level (default 0.95) |
variable_names | Names of variables for labeling |
ci_type | Type of CI to extract ("raw", "rand_diff", "focal_diff") |
Value
Data frame with confidence intervals
Plot the rescaled importance values from the output ofrwa()
Description
Pass the output ofrwa() and plot a bar chart of the rescaled importance values.Signs are always calculated and taken into account, which is equivalent to setting theapplysignsargument toTRUE inrwa().
Usage
plot_rwa(rwa)Arguments
rwa | Direct list output from |
Examples
library(ggplot2)# Use a smaller sample for faster executiondiamonds_small <- diamonds[sample(nrow(diamonds), 1000), ]diamonds_small %>% rwa(outcome = "price", predictors = c("depth","carat", "x", "y", "z"), applysigns = TRUE) %>% plot_rwa()Remove any columns where all the values are missing
Description
Pass a data frame and returns a version where all columnsmade up of entirely missing values are removed.
Usage
remove_all_na_cols(df)Arguments
df | Data frame to be passed through. |
Details
This is used withinrwa().
Run bootstrap analysis for RWA
Description
Run bootstrap analysis for RWA
Usage
run_rwa_bootstrap( data, outcome, predictors, n_bootstrap = 1000, conf_level = 0.95, focal = NULL, comprehensive = FALSE, include_rescaled = FALSE)Arguments
data | Data frame |
outcome | Outcome variable |
predictors | Predictor variables |
n_bootstrap | Number of bootstrap samples |
conf_level | Confidence level |
focal | Focal variable for comparisons (optional) |
comprehensive | Whether to run comprehensive analysis |
include_rescaled | Whether to bootstrap rescaled weights |
Value
List with bootstrap results and confidence intervals
Create a Relative Weights Analysis (RWA)
Description
This function creates a Relative Weights Analysis (RWA) andreturns a list of outputs. RWA provides a heuristic method for estimatingthe relative weight of predictor variables in multiple regression, whichinvolves creating a multiple regression with on a set of transformedpredictors which are orthogonal to each other but maximally related to theoriginal set of predictors.rwa() is optimised for dplyr pipes and shows positive / negative signs for weights.
Usage
rwa( df, outcome, predictors, applysigns = FALSE, sort = TRUE, bootstrap = FALSE, n_bootstrap = 1000, conf_level = 0.95, focal = NULL, comprehensive = FALSE, include_rescaled_ci = FALSE)Arguments
df | Data frame or tibble to be passed through. |
outcome | Outcome variable, to be specified as a string or bare input.Must be a numeric variable. |
predictors | Predictor variable(s), to be specified as a vector ofstring(s) or bare input(s). All variables must be numeric. |
applysigns | Logical value specifying whether to show an estimate that applies the sign. Defaults to |
sort | Logical value specifying whether to sort results by rescaled relative weights in descending order. Defaults to |
bootstrap | Logical value specifying whether to calculate bootstrap confidence intervals. Defaults to |
n_bootstrap | Number of bootstrap samples to use when bootstrap = TRUE. Defaults to 1000. |
conf_level | Confidence level for bootstrap intervals. Defaults to 0.95. |
focal | Focal variable for bootstrap comparisons (optional). |
comprehensive | Whether to run comprehensive bootstrap analysis including random variable and focal comparisons. |
include_rescaled_ci | Logical value specifying whether to include confidence intervals for rescaled weights. Defaults to |
Details
rwa() produces raw relative weight values (epsilons) as well as rescaledweights (scaled as a percentage of predictable variance) for every predictorin the model. Signs are added to the weights when theapplysigns argumentis set toTRUE.See https://www.scotttonidandel.com/rwa-web for theoriginal implementation that inspired this package.
Value
rwa() returns a list of outputs, as follows:
predictors: character vector of names of the predictor variables used.rsquare: the rsquare value of the regression model.result: the final output of the importance metrics (sorted by Rescaled.RelWeight in descending order by default).The
Rescaled.RelWeightcolumn sums up to 100.The
Signcolumn indicates whether a predictor is positively or negatively correlated with the outcome.When bootstrap = TRUE, includes confidence interval columns for raw weights.
Rescaled weight CIs are available via include_rescaled_ci = TRUE but not recommended for inference.
n: indicates the number of observations used in the analysis.bootstrap: bootstrap results (only present when bootstrap = TRUE), containing:ci_results: confidence intervals for weightsboot_object: raw bootstrap object for advanced analysisn_bootstrap: number of bootstrap samples used
lambda:RXX: Correlation matrix of all the predictor variables against each other.RXY: Correlation values of the predictor variables against the outcome variable.
Examples
library(ggplot2)# Basic RWA (results sorted by default)rwa(diamonds,"price",c("depth","carat"))# RWA without sorting (preserves original predictor order)rwa(diamonds,"price",c("depth","carat"), sort = FALSE)# For faster examples, use a subset of data for bootstrapdiamonds_small <- diamonds[sample(nrow(diamonds), 1000), ]# RWA with bootstrap confidence intervals (raw weights only)rwa(diamonds_small,"price",c("depth","carat"), bootstrap = TRUE, n_bootstrap = 100)# Include rescaled weight CIs (use with caution for inference)rwa(diamonds_small,"price",c("depth","carat"), bootstrap = TRUE, include_rescaled_ci = TRUE, n_bootstrap = 100)# Comprehensive bootstrap analysis with focal variableresult <- rwa(diamonds_small,"price",c("depth","carat","table"), bootstrap = TRUE, comprehensive = TRUE, focal = "carat", n_bootstrap = 100)# View confidence intervalsresult$bootstrap$ci_results