The $profiles() method returns a list of data frames with
profiling data if any profiling data was written to the profile CSV files.
See save_profile_files() to control where the files are saved.
Profiling requires adding profiling statements to the Stan program. See Examples for a demonstration.
Examples
# \dontrun{
# first fit a model using MCMC
mcmc_program <- write_stan_file(
'data {
int<lower=0> N;
array[N] int<lower=0,upper=1> y;
}
parameters {
real<lower=0,upper=1> theta;
}
model {
profile("likelihood") {
y ~ bernoulli(theta);
}
}
generated quantities {
array[N] int y_rep;
profile("gq") {
y_rep = bernoulli_rng(rep_vector(theta, N));
}
}
'
)
mod_mcmc <- cmdstan_model(mcmc_program)
data <- list(N = 10, y = c(1,1,0,0,0,1,0,1,0,0))
fit <- mod_mcmc$sample(data = data, seed = 123, refresh = 0)
#> Running MCMC with 4 sequential chains...
#>
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#>
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.5 seconds.
#>
fit$profiles()
#> [[1]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 gq 140637665015616 0.000167421 0.000167421 0.000000000 0
#> 2 likelihood 140637665015616 0.000758086 0.000549880 0.000208206 6721
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 0 0 1000
#> 2 6721 6721 1
#>
#> [[2]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 likelihood 140170778298176 0.000749362 0.000539712 0.00020965 6792
#> 2 gq 140170778298176 0.000170439 0.000170439 0.00000000 0
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 6792 6792 1
#> 2 0 0 1000
#>
#> [[3]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 likelihood 140092730173248 0.000762999 0.000552489 0.00021051 6797
#> 2 gq 140092730173248 0.000166546 0.000166546 0.00000000 0
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 6797 6797 1
#> 2 0 0 1000
#>
#> [[4]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 gq 140291494061888 0.000170743 0.000170743 0.000000000 0
#> 2 likelihood 140291494061888 0.000770105 0.000554802 0.000215303 6979
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 0 0 1000
#> 2 6979 6979 1
#>
# }