Statistical analysis

From aipoch/medical-research-skills · View interactive version

Statistical analysis is an AI agent skill from the GitHub repository aipoch/medical-research-skills. Guided statistical analysis for test selection, assumption checks, power analysis, and APA-style reporting. Use when you need to choose an appropriate statistical test for your data and produce publication-ready results (including effect sizes and diagnostics).

How to use this skill

Skill file

Source: https://github.com/aipoch/medical-research-skills

When to Use

Use this skill when you need to:

  1. Choose an appropriate statistical test (e.g., t-test vs. ANOVA vs. non-parametric vs. Bayesian) based on study design and variable types.
  2. Validate assumptions before inference (normality, homoscedasticity, linearity, outliers) and decide on remedies when assumptions fail.
  3. Run common inferential analyses (hypothesis tests, correlation, regression) and interpret results with effect sizes and uncertainty.
  4. Plan studies with a priori power analysis (sample size planning) or run sensitivity analysis after data collection.
  5. Write results in APA style with complete reporting elements (test statistic, df, p, effect size, CI, assumption checks).

For programming model-specific workflows (especially regression variants and custom diagnostics), prefer statsmodels directly; this skill focuses on guided selection, checks, interpretation, and reporting.

Key Features

Dependencies

Python (recommended 3.10+) with:

Example Usage

The example below is designed to be runnable end-to-end: it generates synthetic data, checks assumptions, runs an independent-samples t-test with effect size, performs power analysis, and prints an APA-style result string.

import numpy as np
import pandas as pd
import pingouin as pg

from statsmodels.stats.power import tt_ind_solve_power

# If your repo provides this module, use it; otherwise comment it out.
from scripts.assumption_checks import comprehensive_assumption_check

# ----------------------------
# 1) Create example dataset
# ----------------------------
rng = np.random.default_rng(7)
n_a, n_b = 50, 52

group_a = rng.normal(loc=75, scale=9, size=n_a)
group_b = rng.normal(loc=69, scale=9, size=n_b)

df = pd.DataFrame({
    "score": np.r_[group_a, group_b],
    "group": ["A"] * n_a + ["B"] * n_b
})

# ----------------------------
# 2) Assumption checks
# ----------------------------
assump = comprehensive_assumption_check(
    data=df,
    value_col="score",
    group_col="group",
    alpha=0.05
)
print("Assumption check summary:")
print(assump["summary"] if "summary" in assump else assump)

# ----------------------------
# 3) Run test + effect size
# ----------------------------
res = pg.ttest(group_a, group_b, correction="auto")  # Welch if needed
t_stat = float(res["T"].iloc[0])
dfree = float(res["dof"].iloc[0])
pval = float(res["p-val"].iloc[0])
d = float(res["cohen-d"].iloc[0])
ci_low, ci_high = res["CI95%"].iloc[0]

# ----------------------------
# 4) Power analysis (planning)
# ----------------------------
n_required = tt_ind_solve_power(
    effect_size=0.5, alpha=0.05, power=0.80, ratio=1.0, alternative="two-sided"
)

# ----------------------------
# 5) APA-style reporting string
# ----------------------------
m_a, sd_a = group_a.mean(), group_a.std(ddof=1)
m_b, sd_b = group_b.mean(), group_b.std(ddof=1)

apa = (
    f"Group A (n = {n_a}, M = {m_a:.2f}, SD = {sd_a:.2f}) and "
    f"Group B (n = {n_b}, M = {m_b:.2f}, SD = {sd_b:.2f}) differed, "
    f"t({dfree:.0f}) = {t_stat:.2f}, p = {pval:.3f}, d = {d:.2f}, "
    f"95% CI [{ci_low:.2f}, {ci_high:.2f}]."
)

print("\nAPA-style result:")
print(apa)

print(f"\nPlanning note: to detect d = 0.50 with 80% power, "
      f"required n per group ≈ {n_required:.0f}.")

Implementation Details

1) Test Selection Logic (Conceptual)

Use references/test_selection_guide.md as the primary decision aid. The selection is typically driven by:

Common mappings:

2) Assumption Checks and Diagnostics

The automated workflow in scripts/assumption_checks.py (referenced in the original documentation) is expected to cover:

Key parameter:

Recommended remedies (see references/assumptions_and_diagnostics.md):

3) Effect Sizes and Uncertainty

Effect sizes should be reported alongside inferential results (see references/effect_sizes_and_power.md):

Always prefer confidence intervals (frequentist) or credible intervals (Bayesian) to communicate precision.

4) Power Analysis

Implemented via statsmodels.stats.power:

Avoid “post-hoc power” computed from observed p-values; use sensitivity analysis instead.

5) APA-Style Reporting Requirements

Use references/reporting_standards.md to ensure inclusion of:

For Bayesian reporting (see references/bayesian_statistics.md), include:

When Not to Use

Required Inputs

| Field | Required | Format/Source | Example | If Missing |

|---|---|---|---|---|

| User task description | Yes | Text | Research question, writing goal, analysis objective | Stop and ask user to provide |

| Primary input material | Depends on task | Text, file path, ID, table, or literature | PMID, PDF, CSV, DOCX, keywords, etc. | Specify which material type is missing |

| Output preference | No | Text | Language, format, target journal, template | Use skill default format |

Output Contract

Failure Handling

User Checkpoints

Input Validation

This skill accepts requests that match the documented purpose of statistical-analysis and include enough context to complete the workflow safely.

Do not continue the workflow when the request is out of scope, missing a critical input, or would require unsupported assumptions. Instead respond:

statistical-analysis only handles its documented workflow. Please provide the missing required inputs or switch to a more suitable skill.

Quick Validation

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.