Skip to content
Back to skills

Rstan

ASecurity

R rstan package for Stan interface. Use for full Bayesian inference with Stan probabilistic programming.

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

Security analysis

A100/100

Scanned June 4, 2026

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

Installs into .claude/skills of the current project.

Are you the author of Rstan?

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

Security grade badge for Rstan
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-rstan/badge)](https://www.skillsdirectory.com/skills/leolin990405-rstan)

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: rstan
description: R rstan package for Stan interface. Use for full Bayesian inference with Stan probabilistic programming.
---

# rstan Package

R interface to Stan for Bayesian inference.

## Basic Model

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

# Stan model code
stan_code <- "
data {
  int<lower=0> N;
  vector[N] y;
}
parameters {
  real mu;
  real<lower=0> sigma;
}
model {
  mu ~ normal(0, 10);
  sigma ~ cauchy(0, 5);
  y ~ normal(mu, sigma);
}
"

# Fit model
fit <- stan(model_code = stan_code,
  data = list(N = length(y), y = y),
  iter = 2000,
  chains = 4
)
```

## From File

```r
# Save as model.stan
fit <- stan(file = "model.stan", data = stan_data)
```

## Extract Results

```r
# Summary
print(fit)
summary(fit)$summary

# Extract samples
samples <- extract(fit)
samples$mu
samples$sigma

# As array
posterior <- as.array(fit)
```

## Diagnostics

```r
# Trace plots
traceplot(fit)
traceplot(fit, pars = c("mu", "sigma"))

# Pairs plot
pairs(fit, pars = c("mu", "sigma"))

# R-hat and ESS
summary(fit)$summary[, c("Rhat", "n_eff")]

# Check divergences
check_divergences(fit)
check_treedepth(fit)
```

## Posterior Analysis

```r
library(bayesplot)

# Density
mcmc_dens(as.array(fit), pars = c("mu", "sigma"))

# Intervals
mcmc_intervals(as.array(fit), pars = c("mu", "sigma"))

# Areas
mcmc_areas(as.array(fit), pars = c("mu", "sigma"), prob = 0.95)
```

## Generated Quantities

```stan
generated quantities {
  vector[N] y_rep;
  for (n in 1:N)
    y_rep[n] = normal_rng(mu, sigma);
}
```

```r
# Posterior predictive checks
y_rep <- extract(fit)$y_rep
ppc_dens_overlay(y, y_rep[1:50, ])
```

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…