Skip to content
Back to skills

R Stats Bayesian

ASecurity

R Bayesian analysis with Stan, brms, rjags. Use for MCMC, posterior inference, and hierarchical models.

  • 5 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added June 4, 2026
data

Security analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned June 4, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill r-stats-bayesian --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of R Stats Bayesian?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for R Stats Bayesian
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-r-stats-bayesian/badge)](https://www.skillsdirectory.com/skills/leolin990405-r-stats-bayesian)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: r-stats-bayesian
description: R Bayesian analysis with Stan, brms, rjags. Use for MCMC, posterior inference, and hierarchical models.
---

# R Bayesian Analysis

MCMC and Bayesian inference.

## brms (High-level Stan)

```r
library(brms)

# Simple regression
model <- brm(y ~ x1 + x2, data = df, family = gaussian())

# With priors
model <- brm(
  y ~ x1 + x2,
  data = df,
  prior = c(
    prior(normal(0, 10), class = "b"),
    prior(cauchy(0, 2), class = "sigma")
  ),
  chains = 4, iter = 2000, warmup = 1000
)

# Hierarchical model
model <- brm(y ~ x + (1 + x | group), data = df)

# Logistic regression
model <- brm(y ~ x, data = df, family = bernoulli())

# Results
summary(model)
plot(model)
pp_check(model)

# Posterior
posterior_samples(model)
hypothesis(model, "x1 > 0")

# Predictions
predict(model, newdata = new_df)
fitted(model)
```

## rstan

```r
library(rstan)
options(mc.cores = parallel::detectCores())

stan_code <- "
data {
  int<lower=0> N;
  vector[N] x;
  vector[N] y;
}
parameters {
  real alpha;
  real beta;
  real<lower=0> sigma;
}
model {
  alpha ~ normal(0, 10);
  beta ~ normal(0, 10);
  sigma ~ cauchy(0, 5);
  y ~ normal(alpha + beta * x, sigma);
}
"

fit <- stan(
  model_code = stan_code,
  data = list(N = nrow(df), x = df$x, y = df$y),
  chains = 4, iter = 2000
)

print(fit)
plot(fit)
traceplot(fit)
pairs(fit)

# Extract samples
samples <- extract(fit)
mean(samples$beta)
quantile(samples$beta, c(0.025, 0.975))
```

## rstanarm

```r
library(rstanarm)

# Linear regression
model <- stan_glm(y ~ x1 + x2, data = df)

# Hierarchical
model <- stan_glmer(y ~ x + (1 | group), data = df)

# Logistic
model <- stan_glm(y ~ x, data = df, family = binomial())

summary(model)
posterior_interval(model)
```

## Diagnostics

```r
library(bayesplot)

# Trace plots
mcmc_trace(as.array(model))

# Density
mcmc_dens(as.array(model))

# R-hat and ESS
mcmc_rhat(rhat(model))
mcmc_neff(neff_ratio(model))

# Posterior predictive check
pp_check(model, type = "dens_overlay")
pp_check(model, type = "stat", stat = "mean")
```

## Model Comparison

```r
# LOO-CV
loo1 <- loo(model1)
loo2 <- loo(model2)
loo_compare(loo1, loo2)

# WAIC
waic(model)

# Bayes factor
library(bridgesampling)
bf <- bayes_factor(bridge_sampler(model1), bridge_sampler(model2))
```

Files in this skill

  • MCMCpack/SKILL.md2.6 KB
  • SKILL.md2.2 KB
  • brms/SKILL.md3 KB
  • coda/SKILL.md2.8 KB
  • rstan/SKILL.md1.6 KB
  • rstanarm/SKILL.md1.7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…