【问题标题】:Changing the title of individual plots within a panel of traceplots in ggplot/stan在 ggplot/stan 的跟踪图中更改单个图的标题
【发布时间】:2019-08-08 11:48:28
【问题描述】:

我想知道,有没有什么方法可以更改 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(例如)。

【问题讨论】:

    标签: r ggplot2 rstan


    【解决方案1】:

    了解如何执行此操作。 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)
    

    瞧!

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多