Jonah, thanks for your help on this. I am finding it very helpful in deepening my understanding of R and brms.
Unfortunately, I am running into errors with both approaches. I believe the init
values created by the methods are not formatted correctly.
Here’s what I get, (note model was compiled with `backend = “cmdstanr”)
> code <- stancode(model) # uses brms object
> data <- standata(model)
> mod <- cmdstan_model(write_stan_file(model$"model")) # create temporary file with code
Model executable is up to date!
> fit_opt <- mod$optimize(data = data, iter = 1E4)
Initial log joint probability = -3.18939e+06
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
99 -17562.2 0.989999 855.474 0.4151 1 132
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
199 -16881 0.440666 83.7803 1 1 250
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
299 -16789.7 3.65428 411.479 1 1 363
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
399 -16706.1 0.18111 202.882 0.0573 1 471
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
499 -16545.1 1.61231 135.575 1 1 584
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
599 -16479 0.0118606 27.9198 1 1 694
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
699 -16474.9 0.00416162 6.2371 1 1 808
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
799 -16472.7 0.0569348 2.3115 1 1 926
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
858 -16471.8 0.00773682 0.402875 1 1 992
Optimization terminated normally:
Convergence detected: relative gradient magnitude is below tolerance
Finished in 3.3 seconds.
> model2 <- update(model, backend = "cmdstanr", init = fit_opt)
Start sampling
Running MCMC with 4 sequential chains, with 5 thread(s) per chain...
Chain 1 Unrecoverable error evaluating the log probability at the initial value.
Chain 1 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 1 finished unexpectedly!
Chain 2 Unrecoverable error evaluating the log probability at the initial value.
Chain 2 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 2 finished unexpectedly!
Chain 3 Unrecoverable error evaluating the log probability at the initial value.
Chain 3 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 3 finished unexpectedly!
Chain 4 Unrecoverable error evaluating the log probability at the initial value.
Chain 4 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 4 finished unexpectedly!
Warning: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Warning: No chains finished successfully. Unable to retrieve the fit.
Error: Fitting failed. Unable to retrieve the metadata.
This is what I get when I try using the laplace output
fit_laplace <- mod$laplace(data = data, opt_args = list(iter = 1E4), init = 0)
model3 <- update(model, backend = "cmdstanr", init = fit_laplace)
Initial log joint probability = -84738.9
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
99 -17902.9 0.117577 224.32 1 1 110
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
199 -17841.9 0.0557256 28.49 1 1 217
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
299 -17830.3 0.0161296 68.4053 0.3937 1 329
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
399 -17828 0.0207503 43.5726 0.8657 0.8657 435
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
499 -17827.1 0.00670058 12.3503 1 1 537
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
599 -17826.5 0.00266746 12.3755 1 1 644
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
699 -17825.5 0.0026736 6.6563 1 1 747
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
799 -17825.3 0.00258842 5.34482 1 1 859
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
899 -17825.2 0.00285119 19.5476 0.3879 0.3879 964
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
999 -17825 0.00127582 5.40387 1 1 1071
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1099 -17824.9 0.00489369 5.29319 1 1 1178
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1199 -17824.8 0.000392955 3.2629 1 1 1288
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1299 -17824.8 0.000342069 1.87174 1 1 1397
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1399 -17824.7 0.000177246 5.78395 0.2039 0.2039 1506
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1499 -17824.7 0.000899042 1.85878 0.8683 0.8683 1611
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1599 -17824.6 0.0042151 6.3119 0.669 0.669 1716
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1699 -17824.5 0.000239514 4.90635 0.7316 0.7316 1826
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
1762 -17824.5 0.00016997 1.69032 1 1 1892
Optimization terminated normally:
Convergence detected: relative gradient magnitude is below tolerance
Finished in 5.1 seconds.
Calculating Hessian
Calculating inverse of Cholesky factor
Generating draws
iteration: 0
iteration: 100
iteration: 200
iteration: 300
iteration: 400
iteration: 500
iteration: 600
iteration: 700
iteration: 800
iteration: 900
Finished in 5.8 seconds.
> model3 <- update(model, backend = "cmdstanr", init = fit_laplace)
Start sampling
Running MCMC with 4 sequential chains, with 5 thread(s) per chain...
Chain 1 Unrecoverable error evaluating the log probability at the initial value.
Chain 1 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 1 finished unexpectedly!
Chain 2 Unrecoverable error evaluating the log probability at the initial value.
Chain 2 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 2 finished unexpectedly!
Chain 3 Unrecoverable error evaluating the log probability at the initial value.
Chain 3 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 3 finished unexpectedly!
Chain 4 Unrecoverable error evaluating the log probability at the initial value.
Chain 4 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,7); dims found=(7) (in '/tmp/Rtmp3uiv2e/model-304fba437c45ec.stan', line 78, column 2 to column 29)
Warning: Chain 4 finished unexpectedly!
Warning: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Warning: No chains finished successfully. Unable to retrieve the fit.
Error: Fitting failed. Unable to retrieve the metadata.
>
Any help would be appreciated.