stanrstan

R-hat against iterations RStan


I am trying to generate a similar plot as below to show the change in R-hat over iterations: enter image description here

I have tried the following options :

Can you please help me to get R-hat for each iteration for a given parameter?


Solution

  • rstan provides the Rhat() function, which takes a matrix of iterations x chains and returns R-hat. We can extract this matrix from the fitted model and apply Rhat() cumulatively over it. The code below uses the 8 schools model as an example (copied from the getting started guide).

    library(tidyverse)
    library(purrr)
    library(rstan)
    theme_set(theme_bw())
    
    # Fit the 8 schools model.
    schools_dat <- list(J = 8, 
                        y = c(28,  8, -3,  7, -1,  1, 18, 12),
                        sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
    fit <- stan(file = 'schools.stan', data = schools_dat)
    
    # Extract draws for mu as a matrix; columns are chains and rows are iterations.
    mu_draws = as.array(fit)[,,"mu"]
    
    # Get the cumulative R-hat as of each iteration.
    mu_rhat = map_dfr(
      1:nrow(mu_draws),
      function(i) {
        return(data.frame(iteration = i,
                          rhat = Rhat(mu_draws[1:i,])))
      }
    )
    
    # Plot iteration against R-hat.
    mu_rhat %>%
      ggplot(aes(x = iteration, y = rhat)) +
      geom_line() +
      labs(x = "Iteration", y = expression(hat(R)))
    

    enter image description here