/install lake-warming-attribution-contribution-analysis
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\
')
f.write(f'{dominant},{dominant_pct}\
')
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
- 确保已安装 OpenClaw(本地或 Docker 部署)
- 在对话框中输入安装命令:
/install lake-warming-attribution-contribution-analysis - 安装完成后,直接呼叫该 Skill 的名称或使用
/lake-warming-attribution-contribution-analysis触发 - 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
contribution-analysis 是什么?
Calculate the relative contribution of different factors to a response variable using R² decomposition. Use when you need to quantify how much each factor ex... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 70 次。
如何安装 contribution-analysis?
在 OpenClaw 或 Claude Code 对话框中运行命令「/install lake-warming-attribution-contribution-analysis」即可一键安装,无需额外配置。
contribution-analysis 是免费的吗?
是的,contribution-analysis 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。
contribution-analysis 支持哪些平台?
contribution-analysis 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。
谁开发了 contribution-analysis?
由 wu-uk(@wu-uk)开发并维护,当前版本 v0.1.0。