Contribution Analysis
by benchflow-ai
Calculate the relative contribution of different factors to a response variable using R² decomposition. Use when you need to quantify how much each factor explains the variance of an outcome.
Skill Details
Repository Files
1 file in this skill directory
name: contribution-analysis description: Calculate the relative contribution of different factors to a response variable using R² decomposition. Use when you need to quantify how much each factor explains the variance of an outcome. license: MIT
Contribution Analysis Guide
Overview
Contribution analysis quantifies how much each factor contributes to explaining the variance of a response variable. This skill focuses on R² decomposition method.
Complete Workflow
When you have multiple correlated variables that belong to different categories:
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from factor_analyzer import FactorAnalyzer
# Step 1: Combine ALL variables into one matrix
pca_vars = ['Var1', 'Var2', 'Var3', 'Var4', 'Var5', 'Var6', 'Var7', 'Var8']
X = df[pca_vars].values
y = df['ResponseVariable'].values
# Step 2: Standardize
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Step 3: Run ONE global PCA on all variables together
fa = FactorAnalyzer(n_factors=4, rotation='varimax')
fa.fit(X_scaled)
scores = fa.transform(X_scaled)
# Step 4: R² decomposition on factor scores
def calc_r2(X, y):
model = LinearRegression()
model.fit(X, y)
y_pred = model.predict(X)
ss_res = np.sum((y - y_pred) ** 2)
ss_tot = np.sum((y - np.mean(y)) ** 2)
return 1 - (ss_res / ss_tot)
full_r2 = calc_r2(scores, y)
# Step 5: Calculate contribution of each factor
contrib_0 = full_r2 - calc_r2(scores[:, [1, 2, 3]], y)
contrib_1 = full_r2 - calc_r2(scores[:, [0, 2, 3]], y)
contrib_2 = full_r2 - calc_r2(scores[:, [0, 1, 3]], y)
contrib_3 = full_r2 - calc_r2(scores[:, [0, 1, 2]], y)
R² Decomposition Method
The contribution of each factor is calculated by comparing the full model R² with the R² when that factor is removed:
Contribution_i = R²_full - R²_without_i
Output Format
contributions = {
'Category1': contrib_0 * 100,
'Category2': contrib_1 * 100,
'Category3': contrib_2 * 100,
'Category4': contrib_3 * 100
}
dominant = max(contributions, key=contributions.get)
dominant_pct = round(contributions[dominant])
with open('output.csv', 'w') as f:
f.write('variable,contribution\n')
f.write(f'{dominant},{dominant_pct}\n')
Common Issues
| Issue | Cause | Solution |
|---|---|---|
| Negative contribution | Suppressor effect | Check for multicollinearity |
| Contributions don't sum to R² | Normal behavior | R² decomposition is approximate |
| Very small contributions | Factor not important | May be negligible driver |
Best Practices
- Run ONE global PCA on all variables together, not separate PCA per category
- Use factor_analyzer with varimax rotation
- Map factors to category names based on loadings interpretation
- Report contribution as percentage
- Identify the dominant (largest) factor
Related Skills
Attack Tree Construction
Build comprehensive attack trees to visualize threat paths. Use when mapping attack scenarios, identifying defense gaps, or communicating security risks to stakeholders.
Grafana Dashboards
Create and manage production Grafana dashboards for real-time visualization of system and application metrics. Use when building monitoring dashboards, visualizing metrics, or creating operational observability interfaces.
Matplotlib
Foundational plotting library. Create line plots, scatter, bar, histograms, heatmaps, 3D, subplots, export PNG/PDF/SVG, for scientific visualization and publication figures.
Scientific Visualization
Create publication figures with matplotlib/seaborn/plotly. Multi-panel layouts, error bars, significance markers, colorblind-safe, export PDF/EPS/TIFF, for journal-ready scientific plots.
Seaborn
Statistical visualization. Scatter, box, violin, heatmaps, pair plots, regression, correlation matrices, KDE, faceted plots, for exploratory analysis and publication figures.
Shap
Model interpretability and explainability using SHAP (SHapley Additive exPlanations). Use this skill when explaining machine learning model predictions, computing feature importance, generating SHAP plots (waterfall, beeswarm, bar, scatter, force, heatmap), debugging models, analyzing model bias or fairness, comparing models, or implementing explainable AI. Works with tree-based models (XGBoost, LightGBM, Random Forest), deep learning (TensorFlow, PyTorch), linear models, and any black-box model
Pydeseq2
Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.
Query Writing
For writing and executing SQL queries - from simple single-table queries to complex multi-table JOINs and aggregations
Pydeseq2
Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.
Scientific Visualization
Meta-skill for publication-ready figures. Use when creating journal submission figures requiring multi-panel layouts, significance annotations, error bars, colorblind-safe palettes, and specific journal formatting (Nature, Science, Cell). Orchestrates matplotlib/seaborn/plotly with publication styles. For quick exploration use seaborn or plotly directly.
