# Bugs Fundamentals

> Foundational knowledge for writing BUGS/JAGS models including precision parameterization, declarative syntax, distributions, and R integration. Use when creating or reviewing BUGS/JAGS models.

- **Type:** Skill
- **Install:** `agentstack add skill-choxos-biostatagent-bugs-fundamentals`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [choxos](https://agentstack.voostack.com/s/choxos)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [choxos](https://github.com/choxos)
- **Source:** https://github.com/choxos/BiostatAgent/tree/main/plugins/bayesian-modeling/skills/bugs-fundamentals

## Install

```sh
agentstack add skill-choxos-biostatagent-bugs-fundamentals
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# BUGS/JAGS Fundamentals

## When to Use This Skill

- Writing new WinBUGS or JAGS models
- Understanding BUGS declarative syntax
- Converting between BUGS and Stan
- Integrating with R via R2jags or R2WinBUGS

## Model Structure

BUGS uses a **single declarative block** where order doesn't matter:

```
model {
  # Likelihood (order doesn't matter)
  for (i in 1:N) {
    y[i] ~ dnorm(mu[i], tau)
    mu[i] = threshold
eq <- equals(y, 0)           # 1 if y == 0
```

## Common Priors

```
# Vague normal (variance = 1000)
alpha ~ dnorm(0, 0.001)

# Half-Cauchy on SD (via uniform)
sigma ~ dunif(0, 100)
tau <- pow(sigma, -2)

# Vague gamma on precision
tau ~ dgamma(0.001, 0.001)

# Correlation matrix
Omega ~ dwish(I[,], K + 1)
```

## R Integration

### R2jags (Recommended)
```r
library(R2jags)

jags.data <- list(N = 100, y = y, x = x)
jags.params <- c("alpha", "beta", "sigma")
jags.inits <- function() {
  list(alpha = 0, beta = 0, tau = 1)
}

fit <- jags(
  data = jags.data,
  inits = jags.inits,
  parameters.to.save = jags.params,
  model.file = "model.txt",
  n.chains = 4,
  n.iter = 10000,
  n.burnin = 5000
)

print(fit)
fit$BUGSoutput$summary
```

### R2WinBUGS (Windows)
```r
library(R2WinBUGS)

fit <- bugs(
  data = bugs.data,
  inits = bugs.inits,
  parameters.to.save = bugs.params,
  model.file = "model.txt",
  n.chains = 3,
  n.iter = 10000,
  bugs.directory = "C:/WinBUGS14/"
)
```

## Key Differences from Stan

| Feature | BUGS/JAGS | Stan |
|---------|-----------|------|
| Normal | `dnorm(mu, tau)` precision | `normal(mu, sigma)` SD |
| MVN | `dmnorm(mu, Omega)` precision | `multi_normal(mu, Sigma)` cov |
| Syntax | Declarative (DAG) | Imperative (sequential) |
| Blocks | Single model{} | 7 optional blocks |
| Sampling | Gibbs + Metropolis | HMC/NUTS |
| Discrete | Direct sampling | Marginalization required |

## Common Errors

1. **Using SD instead of precision**: `dnorm(0, 1)` means variance=1, NOT SD=1
2. **Wrong binomial order**: `dbin(p, n)` not `dbin(n, p)`
3. **Missing initial values**: Provide inits for complex models
4. **Invalid parent values**: Check for NA/NaN in data

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [choxos](https://github.com/choxos)
- **Source:** [choxos/BiostatAgent](https://github.com/choxos/BiostatAgent)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-choxos-biostatagent-bugs-fundamentals
- Seller: https://agentstack.voostack.com/s/choxos
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
