skills/43-wentorai-research-plugins/skills/analysis/statistics/data-anomaly-detection/SKILL.md
Detect anomalies and outliers in research data using statistical methods
npx skillsauth add brycewang-stanford/Awesome-Agent-Skills-for-Empirical-Research data-anomaly-detectionInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
A skill for identifying anomalies, outliers, and suspicious patterns in research datasets. Combines classical statistical methods with modern machine learning approaches to flag data points that deviate significantly from expected distributions, helping researchers maintain data integrity and uncover genuine scientific findings.
Anomalous data points in research datasets can arise from measurement errors, instrument malfunction, data entry mistakes, or genuine rare phenomena. Distinguishing between these sources is critical: blindly removing outliers can bias results, while ignoring measurement errors introduces noise. This skill provides a structured framework for detecting, classifying, and handling anomalies in univariate, multivariate, and time-series research data.
The approach follows a three-stage pipeline: detection (flagging candidate anomalies), diagnosis (determining likely cause), and decision (remove, transform, or retain with justification). Every decision is logged for reproducibility and transparent reporting.
import numpy as np
from scipy import stats
def detect_univariate_outliers(data: np.ndarray, method: str = 'iqr') -> dict:
"""
Detect outliers using classical univariate methods.
Methods:
'iqr': Interquartile range (1.5x IQR rule)
'zscore': Z-score threshold (|z| > 3)
'mad': Median absolute deviation (robust)
'grubbs': Grubbs' test for single outlier
"""
results = {'method': method, 'n_total': len(data)}
if method == 'iqr':
q1, q3 = np.percentile(data, [25, 75])
iqr = q3 - q1
lower, upper = q1 - 1.5 * iqr, q3 + 1.5 * iqr
mask = (data < lower) | (data > upper)
elif method == 'zscore':
z = np.abs(stats.zscore(data))
mask = z > 3
elif method == 'mad':
median = np.median(data)
mad = np.median(np.abs(data - median))
modified_z = 0.6745 * (data - median) / mad if mad > 0 else np.zeros_like(data)
mask = np.abs(modified_z) > 3.5
elif method == 'grubbs':
# Grubbs' test for the single most extreme value
n = len(data)
mean, sd = np.mean(data), np.std(data, ddof=1)
g = np.max(np.abs(data - mean)) / sd
t_crit = stats.t.ppf(1 - 0.05 / (2 * n), n - 2)
g_crit = ((n - 1) / np.sqrt(n)) * np.sqrt(t_crit**2 / (n - 2 + t_crit**2))
mask = np.abs(data - mean) / sd >= g_crit
results['outlier_indices'] = np.where(mask)[0].tolist()
results['n_outliers'] = int(mask.sum())
results['pct_outliers'] = round(mask.sum() / len(data) * 100, 2)
return results
from sklearn.covariance import EllipticEnvelope
from sklearn.ensemble import IsolationForest
def detect_multivariate_outliers(X: np.ndarray, method: str = 'mahalanobis') -> dict:
"""
Detect multivariate outliers using distance-based and model-based methods.
"""
if method == 'mahalanobis':
detector = EllipticEnvelope(contamination=0.05, random_state=42)
labels = detector.fit_predict(X) # -1 = outlier, 1 = inlier
elif method == 'isolation_forest':
detector = IsolationForest(
n_estimators=100, contamination=0.05, random_state=42
)
labels = detector.fit_predict(X)
outlier_mask = labels == -1
return {
'method': method,
'outlier_indices': np.where(outlier_mask)[0].tolist(),
'n_outliers': int(outlier_mask.sum()),
'contamination_assumed': 0.05
}
Once candidate anomalies are flagged, classify each by likely cause:
| Category | Indicators | Action | |----------|-----------|--------| | Measurement error | Value physically impossible, instrument log shows malfunction | Remove with documentation | | Data entry error | Obvious typo (e.g., extra digit), inconsistent units | Correct if source available, else remove | | Sampling artifact | Unusual but plausible value from edge of population | Retain; use robust methods | | Genuine extreme | Verified measurement, consistent with other variables | Retain; report sensitivity analysis | | Contamination | Data from wrong population or experimental condition | Remove with justification |
def detect_timeseries_anomalies(series: np.ndarray, window: int = 20) -> dict:
"""
Detect anomalies in time-series data using rolling statistics.
"""
rolling_mean = pd.Series(series).rolling(window=window).mean()
rolling_std = pd.Series(series).rolling(window=window).std()
upper_bound = rolling_mean + 3 * rolling_std
lower_bound = rolling_mean - 3 * rolling_std
anomalies = (series > upper_bound) | (series < lower_bound)
return {
'anomaly_indices': np.where(anomalies)[0].tolist(),
'n_anomalies': int(anomalies.sum()),
'window_size': window
}
When reporting anomaly handling in publications:
tools
Recommend AND run open-source AI tools, agents, Claude Code / Codex skills, and MCP servers for any stage of a literature review — searching, reading, extracting, synthesizing, screening, citation-checking, and paper writing. Use when the user asks "what tool should I use to..." OR "install/run/use <tool> to ..." for research/lit-review work: automating a survey or related-work section, PDF→Markdown extraction for LLMs (MinerU/marker/docling), PRISMA / systematic review (ASReview), citation-backed Q&A over PDFs (PaperQA2), wiring papers into Claude/Cursor via MCP (arxiv/paper-search/zotero servers), or chatting with a Zotero library. Ships a launcher (scripts/litrun.py) that installs each tool in an isolated venv and runs it. Curated catalog of 70+ vetted projects. 支持中英文(用于「文献综述工具选型」与「一键安装/运行」)。
development
Route empirical-research requests through the Auto-Empirical Research Skills catalog when this whole repository is installed as one skill in Codex, CodeBuddy, Claude Code, or another IDE. Use to choose and load the right vendored AERS skill for causal inference, econometrics, replication, data acquisition, manuscript writing, peer review and referee responses, citation checking, de-AIGC editing, or full empirical-paper workflows without reading the entire repository at once.
documentation
Use when the project collects primary data or runs a field, lab, or survey experiment, before the intervention begins — write the pre-analysis plan, size the sample from a power calculation, and register with the AEA RCT Registry. Apply after the design is chosen in aer-identification and before any outcome data are seen.
tools
Guide economists to authoritative data sources with explicit, confirmed data specifications before retrieval; interfaces with Playwright MCP to navigate portals and extract real data, not articles about data.