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.

Support for profiling Stan programs is available with CmdStan >= 2.26 and requires adding profiling statements to the Stan program.

profiles()

Value

A list of data frames with profiling data if the profiling CSV files were created.

Examples


# \dontrun{
# first fit a model using MCMC
mcmc_program <- write_stan_file(
  'data {
    int<lower=0> N;
    int<lower=0,upper=1> y[N];
  }
  parameters {
    real<lower=0,upper=1> theta;
  }
  model {
    profile("likelihood") {
      y ~ bernoulli(theta);
    }
  }
  generated quantities {
    int y_rep[N];
    profile("gq") {
      y_rep = bernoulli_rng(rep_vector(theta, N));
    }
  }
'
)
mod_mcmc <- cmdstan_model(mcmc_program)
#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-16218658ea62a.stan', line 3, column 4: Declaration
#>     of arrays by placing brackets after a variable name is deprecated and
#>     will be removed in Stan 2.33.0. Instead use the array keyword before the
#>     type. This can be changed automatically using the auto-format flag to
#>     stanc
#> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-16218658ea62a.stan', line 14, column 4: Declaration
#>     of arrays by placing brackets after a variable name is deprecated and
#>     will be removed in Stan 2.33.0. Instead use the array keyword before the
#>     type. This can be changed automatically using the auto-format flag to
#>     stanc

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 0x10afd5e00 0.00037530  0.000375300  0.000000000           0
#> 2 likelihood 0x10afd5e00 0.00114227  0.000802925  0.000339345        7169
#>   no_chain_stack autodiff_calls no_autodiff_calls
#> 1              0              0              1000
#> 2              0           7169                 1
#> 
#> [[2]]
#>         name   thread_id total_time forward_time reverse_time chain_stack
#> 1         gq 0x11a045e00 0.00043389  0.000433890  0.000000000           0
#> 2 likelihood 0x11a045e00 0.00111438  0.000786092  0.000328286        7155
#>   no_chain_stack autodiff_calls no_autodiff_calls
#> 1              0              0              1000
#> 2              0           7155                 1
#> 
#> [[3]]
#>         name   thread_id  total_time forward_time reverse_time chain_stack
#> 1         gq 0x10c67be00 0.000330582  0.000330582  0.000000000           0
#> 2 likelihood 0x10c67be00 0.001059290  0.000746087  0.000313204        6879
#>   no_chain_stack autodiff_calls no_autodiff_calls
#> 1              0              0              1000
#> 2              0           6879                 1
#> 
#> [[4]]
#>         name   thread_id  total_time forward_time reverse_time chain_stack
#> 1         gq 0x108f13e00 0.000365107  0.000365107  0.000000000           0
#> 2 likelihood 0x108f13e00 0.001159310  0.000815756  0.000343553        6892
#>   no_chain_stack autodiff_calls no_autodiff_calls
#> 1              0              0              1000
#> 2              0           6892                 1
#> 
# }