更改 ggplot/stan 中跟踪图面板中各个图的标题

Changing the title of individual plots within a panel of traceplots in ggplot/stan

我想知道,有没有什么方法可以更改 rstan/ggplot 中跟踪图中各个图的标题,而不必更改变量本身的名称?

见以下模型和mcmc链

dList <- list(gIndex = rep(1:2,times=20), nG=2, score = rnorm(40, c(0,7), 1), N = 40)
mc <- "
  data{
  int N;
  int nG;
  int gIndex[N];
  real score[N];
  }
  parameters{
  vector[nG] a;
  real sigma;
  }
  model{
  score ~ normal(a[gIndex], sigma);
  }
"
mod <- stan_model(model_code = mc) # compiles model
fit <- sampling(mod, data = dList, warmup = 1e3, iter = 2e3, chains = 3)
tp <- stan_trace(fit, pars = "a") 
tp

我想将原来地块的名称a[1]a[2]更改为TreatmentPlacebo(例如)。

了解如何执行此操作。 bayesplot 包允许对象 x 是 3d 数组而不是 stanfit 对象,因此我们可以将 fit 对象更改为数组

fitArray <- as.array(fit)[,,-4] # remove the superfluous `_lpd` chain
dim(fitArray)
# [1] 1000    3    3

然后重命名数组的维度

dimnames(fitArray)[[2]] <- c("chain1", "chain2", "chain3")
dimnames(fitArray)[[3]] <- c("treatment", "placebo", "sigma")
dimnames(fitArray)

# $iterations
# NULL

# $chains
# [1] "chain1" "chain2" "chain3"

# $parameters
# [1] "treatment" "placebo"   "sigma" 

现在我们可以使用bayesplot包中的mcmc_trace功能

library(bayesplot)
mcmc_trace(fitArray)

瞧!