--- pretty_name: StatAgent language: - en tags: - biostatistics - r - rag - agents - mcp ---
# StatAgent [![Typing SVG](https://readme-typing-svg.demolab.com?font=Fira+Code&pause=1200¢er=true&vCenter=true&width=640&lines=Agentic+biostatistics+assistant;R+statistics+%2B+Python+agent+%2B+RAG;Human-approved+experiments%2C+full+audit+log)](https://github.com/mnoorchenar/StatAgent) [![Code](https://img.shields.io/badge/Code-GitHub-181717?style=for-the-badge&logo=github)](https://github.com/mnoorchenar/StatAgent) [![Dataset](https://img.shields.io/badge/Mirror-Hugging%20Face%20dataset-yellow?style=for-the-badge&logo=huggingface)](https://huggingface.co/datasets/mnoorchenar/StatAgent) [![Author](https://img.shields.io/badge/Author-mnoorchenar-0b6e99?style=for-the-badge)](https://mnoorchenar.github.io/) ![R](https://img.shields.io/badge/R-4.4-276DC3?logo=r&logoColor=white) ![Shiny](https://img.shields.io/badge/Shiny-bslib-0b6e99) ![Python](https://img.shields.io/badge/Python-3.11-3776AB?logo=python&logoColor=white) ![FastAPI](https://img.shields.io/badge/FastAPI-009688?logo=fastapi&logoColor=white) ![MCP](https://img.shields.io/badge/MCP-server-444) ![Groq](https://img.shields.io/badge/Groq-LLM-f55036) ![Docker](https://img.shields.io/badge/Docker-2496ED?logo=docker&logoColor=white) Ask a plain-English question about experiment or manufacturing data. The agent retrieves FDA and PubMed evidence, runs validated R statistics, states the uncertainty, and proposes the next experiment for a human to approve. [![Features](https://img.shields.io/badge/-Features-475569)](#features) [![How it works](https://img.shields.io/badge/-How%20it%20works-475569)](#how-it-works) [![Results](https://img.shields.io/badge/-Results-475569)](#results) [![Documentation](https://img.shields.io/badge/-Documentation-475569)](#documentation) [![Disclaimer](https://img.shields.io/badge/-Disclaimer-475569)](#disclaimer) [![Author](https://img.shields.io/badge/-Author-475569)](#author) Screen recording of the app: a drift question, the answer with citations, the R tool trace and the approval queue Recorded from the running app on the built-in SECOM dataset (first sample question).
| 125 | 8 / 8 | 4 / 4 | 16 | |:---:|:---:|:---:|:---:| | cited regulatory and PubMed passages | retrieval hit@5 on the eval set | statistical checks passing | automated tests (9 R, 7 Python) | ## Features | Capability | Detail | |---|---| | Statistical tools in R | Control-chart drift with bootstrap changepoint CI, four-parameter dose-response (EC50 CI), mixed-model batch variance, power and factorial design | | Retrieval with citations | BM25 and LSA hybrid over 21 CFR Parts 11, 211 and 820 and PubMed abstracts; answers cite passage ids | | Human approval gate | The agent cannot run experiments; proposals wait in a queue for Approve or Reject | | Audit trail | Every question, tool call and decision is stored in SQLite with a hash of each tool result | | MCP server | The same tools are exposed as typed MCP tools for any MCP client | | Resilience | Model fallback, retries, prompt-injection sanitiser, and a replayed saved run if the LLM is rate-limited | ## How it works Architecture: Shiny UI to FastAPI agent, which calls the Groq LLM, the RAG index and the R stats tools; proposals go to human approval and the audit log ## Results Evaluation scores Measured with `python eval/run_eval.py`. The eval sets are small (8 retrieval queries, 4 statistical checks, 5 tool-call questions; one tool-call run hit the Groq free-tier rate limit and is counted as a miss), so treat the scores as a regression guard, not a benchmark. ## Documentation
Getting started ```bash pip install -r requirements.txt Rscript -e 'install.packages(c("shiny","bslib","httr2","commonmark","DT","ggplot2","lme4","drc","pwr","plumber","jsonlite"))' cp .env.example .env # add GROQ_API_KEY Rscript r/run_api.R & # stats tools, port 8001 python -m uvicorn statagent.api:app --port 8000 & Rscript scripts/run_app.R # UI, port 7860 ``` Docker: `docker build -t statagent . && docker run -p 7860:7860 -e GROQ_API_KEY=... statagent`
Engineering decisions | Decision | Reason | |---|---| | Statistics in R, orchestration in Python | Reference-quality packages (lme4, drc) stay the single source of numbers; the LLM never computes statistics | | Residual bootstrap for the changepoint CI | Resampling raw values would destroy the shift being estimated | | BM25 and LSA instead of a neural embedder | Small corpus, no model download, runs on a laptop CPU | | Tools return units notes | An early run misread the shift size; explicit units fixed it | | Real public data only | Synthetic data is used only in tests with a known truth |
Limitations - The statistical tools run on three built-in datasets; upload mode is not implemented. - The Bayesian (brms) model from the plan is not included. - The corpus holds U.S. regulations and PubMed abstracts, not the ICH guideline texts. - SECOM sensors are anonymised, so a detected drift has no physical explanation here. - There is no hosted demo: Hugging Face now requires a PRO plan for Docker Spaces, so the app is run locally or with the Dockerfile. The Hugging Face dataset repository is a mirror of the code. - The free Groq tier limits tokens per minute; a saved example run is shown when it is exceeded. - No automated check that the model's prose matches the tool numbers; review answers against the Tool trace tab.
Configuration | Variable | Default | Purpose | |---|---|---| | `GROQ_API_KEY` | none | Required for live answers | | `GROQ_MODEL` | `openai/gpt-oss-120b` | Primary model; `gpt-oss-20b` is the fallback | | `R_API_URL` | `http://127.0.0.1:8001` | R plumber service | | `STATAGENT_API` | `http://127.0.0.1:8000` | Agent service used by the UI | | `STATAGENT_DB` | `audit.db` | SQLite audit and proposal store |
API `POST /ask`, `GET /datasets`, `GET /series/{dataset}/{column}`, `GET /proposals`, `POST /proposals/{id}`, `GET /audit`, `GET /health`. MCP: `python -m statagent.mcp_server`.
Tests and project structure ```bash cd r/tests && Rscript -e 'testthat::test_dir(".")' # includes known-truth coverage tests python -m pytest tests python eval/run_eval.py --offline ``` ``` r/R/ drift, dose-response, mixed model, design r/plumber.R HTTP wrapper for the R tools statagent/ rag, tools, agent loop, audit, FastAPI, MCP server app/app.R Shiny UI corpus/ fetched 21 CFR and PubMed passages data/ small real datasets (see data/README.md) eval/ evaluation harness ```
## Disclaimer A portfolio demonstration. It is not validated for GxP use, and its output is not regulatory or medical advice. Do not enter confidential data. A qualified statistician should review any result before it is acted on. ## Author
mnoorchenar mnoorchenar
Data science, biostatistics and AI engineering
GitHub Hugging Face