Cmdstanr with the "pars" input argument?

Thanks for developing the cmdstanr package!

I was just wondering, since I did not see the input argument “pars” in the mod$sample() call, how I could specify the parameters that I am interested in?

Thank you!

1 Like

Hi!

you can use the variables argument in the fit$draws() function. Have a look here https://mc-stan.org/cmdstanr/reference/fit-method-draws.html

Do let me know if there is any additional questions.

Right now we don’t have a way of specifying which parameters are written to CSV, which is what that RStan argument is doing (at least when specified at sampling time). CmdStan will just write everything to CSV.

That said, you don’t need to read all the parameters back into R. Like @rok_cesnovar said you can use the variables argument to control which parameters, transformed parameters, and generated quantities are read back into R.

2 Likes

glad to know. Thank you both @rok_cesnovar and @jonah!

2 Likes

Hello, I have a follow up question on this if you are still active on this site. Would the sampling be faster if there were a way to specify a “pars” argument in the mod$sample call? Or should I adjust the stan code itself?

You currently cant specify the pars arugment in the sample() call.

Even if you could, sampling would not be substantially faster (if I recall correctly, the output for 5k parameters/GQ takes around 20s in total), except for extremely trivial models.

I have used

fit$draws(variables = c("beta", "alpha", "prec_coeff","prec_sd",   "alpha_normalised", "beta_random_intercept"))

and callgenerate_quantities as such

rng = mod_rng$generate_quantities(
    	fit$draws(variables = c("beta", "alpha", "prec_coeff","prec_sd",   "alpha_normalised", "beta_random_intercept")),
      data = ...
    )

with an rng model as such

functions{
    row_vector average_by_col(matrix beta){
    return
    rep_row_vector(1.0, rows(beta)) * beta / rows(beta);
  }
  }
data {
	int N;
	int M;
	int C;
	int A;
	int exposure[N];

	// Which column of design, coefficient matrices should be used to generate the data
	int length_X_which;
	int length_XA_which;
	int X_which[length_X_which];
	int XA_which[length_XA_which];
	matrix[N, length_X_which] X;
  matrix[N, length_XA_which] Xa; // The variability design

	int is_truncated;
	real<lower=1> truncation_ajustment;



	// Random intercept
	int length_X_random_intercept_which;
	int X_random_intercept_which[length_X_random_intercept_which];
	int N_grouping;
	matrix[N, N_grouping] X_random_intercept;

  // Should I create intercept for generate quantities
  int<lower=0, upper=1> create_intercept;
  int<lower=0> A_intercept_columns;
  
   int N_random_intercepts;
     int N_minus_sum;
}
transformed data{
  // If needed recreate the intercept
  matrix[N,1] X_intercept = to_matrix(rep_vector(1, N));

  // EXCEPTION MADE FOR WINDOWS GENERATE QUANTITIES IF RANDOM EFFECT DO NOT EXIST
  int N_grouping_WINDOWS_BUG_FIX = max(N_grouping, 1);
}
parameters {

  matrix[C, M-1] beta; // matrix with C rows and number of cells (-1) columns
  matrix[A, M] alpha; // Variability
   matrix[A, M] alpha_normalised;

  // To exclude
  real prec_coeff[2];
  real<lower=0> prec_sd;


    matrix[N_grouping_WINDOWS_BUG_FIX, M] beta_random_intercept;

}
...

But I get this error

Running standalone generated quantities after 4 MCMC chains, all chains in parallel ...

Chain 1 Mismatch between model and fitted_parameters csv file "/var/folders/32/w0tqctvj0q5c_11fdbx2lmtr0002l_/T//RtmpdyxzJp/fittedParams-202306062211-1-8b5562.csv"
Chain 1 
Chain 2 Mismatch between model and fitted_parameters csv file "/var/folders/32/w0tqctvj0q5c_11fdbx2lmtr0002l_/T//RtmpdyxzJp/fittedParams-202306062211-2-8b5562.csv"
Chain 2 
Warning: Chain 1 finished unexpectedly!

Warning: Chain 2 finished unexpectedly!

Chain 3 Mismatch between model and fitted_parameters csv file "/var/folders/32/w0tqctvj0q5c_11fdbx2lmtr0002l_/T//RtmpdyxzJp/fittedParams-202306062211-3-8b5562.csv"
Chain 3 
Chain 4 Mismatch between model and fitted_parameters csv file "/var/folders/32/w0tqctvj0q5c_11fdbx2lmtr0002l_/T//RtmpdyxzJp/fittedParams-202306062211-4-8b5562.csv"
Chain 4 
Warning: Chain 3 finished unexpectedly!

Warning: Chain 4 finished unexpectedly!

Warning: Use read_cmdstan_csv() to read the results of the failed chains.Use $output(chain_id) on the fit object for more output of the failed chains.
Warning message:
All chains finished unexpectedly!

My question is, why if I provide draws with few parameters, the error mentions the csv files, which contain all parameters?

The parameters from the draws and the ones from the rng model match as far as I know.

Thanks!