agentleFS
kaito41418-uxkaito41418-ux/r-stats-skills/skills/regression-analytics/SKILL.md

regression-analytics

Skill0 starsChanged 4 months agoMITView on GitHub

當用戶需要對數據做線性迴歸或 Logistic 迴歸分析(含係數估計、模型診斷、多重共線性、殘差檢驗、Q-Q plot),並要求最終交付 Word 格式(.docx)的分析報告時觸發。用於連續型或二元型結果變量的預測建模與變量重要性評估。 EN: Use for linear or logistic regression — coefficients + CI, VIF for multicollinearity, residual / Q-Q diagnostics, and for logistic: ROC / AUC / Hosmer-Lemeshow. Word report. Trigger phrases: "linear regression on X", "logistic regression predicting Y", "check multicollinearity", "regress Y on X1..Xn".

The file

Files
SKILL.md

Skill: regression-analytics

迴歸建模與 Word 報告匯出(線性迴歸 / Logistic 迴歸 + 完整診斷)。

Metadata

  • Description: 對數據執行迴歸分析,自動診斷模型假設(正態性、方差齊性、多重共線性、離群值/影響點),輸出係數表、模型指標、診斷圖,交付 Word 報告。
  • Version: 1.0.0
  • Entrypoint: orchestrator.py(CLI: --source {local,sql,hadoop} + --analysis {linear,logistic} + --y <column> + --x <cols> + --output-dir)
  • Related: 假設檢驗(t-test/ANOVA/相關性)請用 advanced-data-analytics skill。

Analysis Modes

1. linear — 線性迴歸 (OLS)

  • 輸入: 一個 y 欄位(連續數值)+ 一個或多個 predictor 欄位
  • 模型: y ~ x1 + x2 + ...
  • 主要輸出:
    • 係數表: β 估計、標準誤、t 值、p 值、95% CI(每個 predictor)
    • 模型指標: R²、Adjusted R²、F 統計量、殘差標準誤、AIC、BIC
  • 診斷(自動全跑):
    • 殘差正態性: Shapiro-Wilk on 殘差
    • 異方差: Breusch-Pagan Test(lmtest)
    • 獨立性: Durbin-Watson Test(car)
    • 多重共線性: VIF(car)—— predictor ≥2 時觸發;VIF > 10 標記警告
    • 離群值/影響點: Cook's Distance(標記 D > 4/n 的觀測)
  • 可視化:
    • Q-Q plot(殘差正態性)
    • Residuals vs Fitted(線性 + 異方差)
    • Scale-Location(同方差性)
    • Cook's Distance 條狀圖

2. logistic — 二元 Logistic 迴歸

  • 輸入: 一個 y 欄位(0/1 或兩類別)+ 一個或多個 predictor 欄位
  • 模型: logit(P(y=1)) ~ x1 + x2 + ... (family = binomial)
  • 主要輸出:
    • 係數表: β 估計、Std Err、z 值、p 值、Odds Ratio(e^β)+ 95% CI
    • 模型指標: Null/Residual Deviance、AIC、Nagelkerke R²、混淆矩陣、Accuracy、AUC
  • 診斷:
    • 配適度: Hosmer-Lemeshow Test(如 ResourceSelection 可用;否則跳過並註記)
    • 多重共線性: VIF(同 linear)
    • 影響點: dfbeta / Cook's Distance
  • 可視化:
    • ROC 曲線 + AUC
    • Predicted probability distribution by class
    • Coefficient forest plot(Odds Ratio + CI)

Output Location

預設輸出目錄:./outcome-temp/

  • fetched_data.csv:原始資料快照
  • Regression_Report.docx:Word 報告
  • (中間圖表由 R 產生於 tmp_data/,最終嵌入 docx)

可用 --output-dir <path> 覆寫;建議每次不同分析指定子目錄避免覆蓋。

Workflow (SOP)

  1. 數據獲取: data_fetcher.py 拉取 → fetched_data.csv,校驗 y 與所有 --x 指定欄位存在。
  2. 迴歸執行: r_executor.py 呼叫 lm() 或 glm(family=binomial),跑完診斷 + 繪 4 張圖 → 匯出 JSON。
  3. Word 生成: word_generator.py 動態組裝章節:
    • 模型摘要(公式、n、模型指標表)
    • 係數表(含顯著性標記)
    • 診斷結果表 + 各檢驗判定(PASS/警告)
    • 4 張診斷圖(linear)或 ROC + forest plot(logistic)
    • 結論與建議(自動根據診斷結果生成,例如「殘差不正態,建議考慮 log 轉換或 GLM」)
  4. 交付: .docx 落地在 output-dir。

Constraints

  • 所有數值結果保留 4 位小數(templates/word_styles.json 的 numeric_precision 控制)。
  • Word 報告有清晰層級 + 圖表編號 + 標題;樣式集中於 templates。
  • Predictor 欄位若為類別型(字串),R 自動處理成 factor + dummy 編碼;報告中會標出「參考類別」。
  • 缺失值:任一列缺 y 或任一 --x 值 → listwise deletion(並在報告中記錄丟棄筆數)。

Dependencies

  • Python 3.10+: pandas sqlalchemy python-docx openpyxl pyhive thrift thrift-sasl pure-sasl
  • R 4.x(Rscript 需在 PATH):
    • 必要: ggplot2 jsonlite car lmtest broom
    • 可選: pROC(logistic ROC/AUC)、ResourceSelection(Hosmer-Lemeshow)、showtext sysfonts(中文字型)
    • 可選套件缺失時對應功能靜默降級並在報告中註記。

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.