Hi all,

Is there a way to extract the tuned mass matrix of a chain in rstan? I’ve been looking through the documentation and haven’t been able to find a function/ example.

Thanks.

Hi all,

Is there a way to extract the tuned mass matrix of a chain in rstan? I’ve been looking through the documentation and haven’t been able to find a function/ example.

Thanks.

Not easily. You can dump the results into a .csv file using the `sample_file`

argument and that .csv file has a comment in the middle of it where the diagonal elements of the mass matrix are listed, which you could parse.

Thanks. I’ll look into this.

For the benefit of anyone who ends up having the same request as me, I extracted the diagonal of the inverse mass matrix using the following steps:

First, I included the following as an argument in my stan function

sample_file <- “datadump.txt”

Then, the following code can be used to extract the diagonal entries of the inverse mass matrix from the file.

datadump <- readLines(“datadump.txt”)

massmatindex <- grep("^# Diagonal elements", datadump) + 1

xx <- datadump[massmatindex]

yy <- unlist(strsplit(xx, split=", "))

yy[1] <- substring(yy[1], 3)

massmatdiag <- as.numeric(yy)

massmatdiag is a vector containing the diagonal entries.

4 Likes

Is there a way to do this without re-running Stan, i.e. directly working with a stanfit object?

**Edit**: a straightforward solution is `get_adapation_info(stanfit)`

1 Like

As of yesterday it is now easy to get this after fitting a model in CmdStanR (`devtools::install_github("stan-dev/cmdstanr")`

to get the latest):

3 Likes