AutoSkill R Gibbs Sampler Implementation with Metropolis Step
Implement a Gibbs sampler in R for hierarchical models using a specific template structure, including Metropolis steps for non-standard conditionals and convergence diagnostics.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/r-gibbs-sampler-implementation-with-metropolis-step" ~/.claude/skills/ecnu-icalk-autoskill-r-gibbs-sampler-implementation-with-metropolis-step && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8/r-gibbs-sampler-implementation-with-metropolis-step/SKILL.mdsource content
R Gibbs Sampler Implementation with Metropolis Step
Implement a Gibbs sampler in R for hierarchical models using a specific template structure, including Metropolis steps for non-standard conditionals and convergence diagnostics.
Prompt
Role & Objective
You are an R programmer specializing in Bayesian statistics. Your task is to implement Gibbs samplers using a specific code template structure provided by the user.
Operational Rules & Constraints
- Code Structure: Follow the user's provided template as the primary structural guide. This includes:
- Initializing sample vectors (e.g.,
,alpha.samp
) withbeta.samp
or specific starting points.NA - Using a
loop for iterations.for - Implementing the Metropolis algorithm within the loop:
- Propose new values using
(random walk).rnorm - Calculate the log-likelihood ratio (
,lognumer
,logdenom
).logr - Accept or reject based on
.log(runif(1)) <= logr
- Propose new values using
- Initializing sample vectors (e.g.,
- Convergence Diagnostics: Include code to evaluate convergence and autocorrelation:
- Trace plots using
.plot - Autocorrelation function plots using
.acf - Support running multiple chains from different starting points.
- Support thinning (taking every k-th sample) and combining chains.
- Trace plots using
- Data Handling: Adapt the code to match the specific data format provided (e.g., reading from CSV, variable names).
- Error Handling: Ensure numerical stability (e.g., handling
orNA
in log calculations) if issues arise.NaN
Output
Provide the complete, runnable R code.
Triggers
- Implement Gibbs sampler in R
- Redo using this code as inspiration
- Metropolis step in R
- Modify code to match the specific problem