Reproducing acf plot with math symbols for the parameters

Hi!

I was following this thread to understand the correct way to change the names of the parameters in an Stan model using the bayesplot package: Bayesplot, facet labels, labeller, and label_parsed

However, using the \texttt{mcmc_acf_bar()} function, I get the following result:

draws <- example_mcmc_draws()
my_labeller <- as_labeller(
  x = c(
    'alpha' = 'alpha',
    'sigma' = 'tau'), 
     default = label_parsed)

mcmc_acf_bar(draws, pars = c("alpha", "sigma"),
                        facet_args = list(labeller = my_labeller))

As you can see, the labels for chains now are \texttt{NA}. I’ve tried to solve the problem but I could not.

I will be grateful if you can help me!
Best,

I think the issue is that the named vector x specifies how to map “alpha” and “sigma” and everything else is mapped to “NA” before the labeller is applied.

From the documentation of ggplot2::as_labeller:

x : Object to coerce to a labeller function. If a named character vector, it is used as a lookup table before being passed on to default.

Let’s try the mapping without the labeller.

x <- c("alpha" = "alpha", "sigma" = "tau")

# Apply the hard-coded mapping to the parameters
x[c("alpha", "sigma")]
#>   alpha   sigma 
#> "alpha"   "tau"

# Apply the hard-coded mapping to the chain IDs
x[c("1", "2", "3", "4")]
#> <NA> <NA> <NA> <NA> 
#>   NA   NA   NA   NA

So one solution is to expand the mapping:

x <- c("alpha" = "alpha", "sigma" = "tau", "1" = "1", "2" = "2", "3" = "3", "4" = "4")

But that seems unwieldy. It might be easier to specify that we want to map “sigma” to “tau” and otherwise leave all other inputs unchanged.

my_labeller <- as_labeller(
  # First we use the `case_match` function to transform input labels.
  function(x) {
    case_match(
      x,
      "alpha" ~ "alpha", # It's redundant to map "alpha" to "alpha".
      "sigma" ~ "tau",
      .default = x # Values other than "alpha" and "sigma" remain the same.
    )
  },
  # Then we apply `label_parsed` to the mapped values.
  default = label_parsed
)
2 Likes

Great! It works perfectly!
Thank you for your help @desislava