Help Extracting Fitted Values from CmdStanR

One way to do it would create a stanfit object from the cmdstan csv output files:

stanfit <- rstan::read_stan_csv(linear.fit.3$output_files())

then you could proceed as you would using RStan.

Another way would be to use the posterior package to get a data frame that you can play with:

library(posterior)

# an array of draws from posterior
draws_array <- linear.fit.3$draws() 

# convert to data frame 
draws_df <- as_draws_df(draws_array) #  or as_draws_matrix() for matrix

The posterior package also can be used in conjunction with bayesplot:

mcmc_hist(linear.fit.3$draws("y_rep"))

See the getting started with cmdstanr documentation for more.

3 Likes