I’m trying to change the color of a trace plot in Stan. I have used
color_scheme_set("gray")
followed by mcmc_trace, and this works fine in almost all cases. It seems not possible to change the color in stan_trace. In any case, I am now encountering the following problem. I am running
Error in `select_parameters()`:
! Some 'pars' don't match parameter names: alpha, p, p_prod FALSE
Run `rlang::last_error()` to see where the error occurred.
However, if I run stan_trace, it produces the plots as expected, but with different colors for the different chains, which I need to change. I’m not sure why this is happening, and it hasn’t happened in other analyses.
I’m not sure why the error is occurring, but mcmc_trace and stan_trace are using ggplot2 under the hood and they return a ggplot object. You can therefore use any of ggplot’s functions on them, including using ggplot’s scale_colour_*** functions to change the colors. For example:
library(bayesplot)
library(ggplot2)
x <- example_mcmc_draws()
color_scheme_set("gray")
mcmc_trace(x)
library(rstan)
# I kept getting parsing errors when trying to load the built-in example models,
# so I've created the model below for illustration
stan.mod = "data {
int<lower=0> N;
vector[N] x1;
vector[N] x2;
vector[N] y;
}
parameters {
real alpha;
real beta1;
real beta2;
real<lower=0> sigma;
}
model {
y ~ normal(alpha + beta1*x1 + beta2*x2, sigma);
}"
dat = list(N=10, x1=rnorm(10), x2=rnorm(10), y=rnorm(10))
fit = stan(model_code=stan.mod, data=dat, verbose=FALSE)
stan_trace(fit, pars=c("alpha", "beta1"))