Change the stanfit object

Hi all,

I was fitting a mixture model and I did not put constraints on the parameters so labels were switched across chains so I switched the labels back by hand (by doing rstan::extract(stan_fit, permuted = FALSE) ) so the dataset (posteior draws I worked on is like this)-
Before (say a b are two my parameters and I specified 3 chains, as in the results obtained using rstan::extract(stan_fit, permuted = FALSE) in R:
if the colnames are as follow:
Chain 1: a | Chain 2: a | Chain 3: a| Chain 1: b | Chain 2: b | Chain 3: b

I changed it to
Chain 1: b | Chain 2: a | Chain 3: a| Chain 1: a | Chain 2: b | Chain 3: b

Now is there an easy way to put this back to the original stanfit and recalculate Rhat and effect size and so on as in print(stanfit)? but with new labels I changed?

psudo code:

stan_fit <-
  stan(
    file = "finitemixture.stan",
    data = data,
    chains = 3,
    iter = 1000
  )
posterior_draws <- rstan::extract(stan_fit, permuted = FALSE)

# relabelling
# original label
print(colnames(posterior_draws))
# Chain 1: a | Chain 2: a | Chain 3: a| Chain 1: b | Chain 2: b | Chain 3: b


colnames(posterior_draws) <-
  colnames(posterior_draws)[4, 2, 3, 1, 5, 6]
print(colnames(posterior_draws))
# relabelled
#Chain 1: **b** | Chain 2: a | Chain 3: a| Chain 1: **a** | Chain 2: b | Chain 3: b
#put it posterior_draws back to the stan_fit and so I can get updated Rhat and so on by runing print(stan_fit_relabeled)

Maybe you could use the monitor function for the extracted draws?

1 Like