@@ -36,7 +36,8 @@ remove_likelihood = function(model_file){
36
36
}
37
37
38
38
# ' @noRd
39
- .autoformat <- function (stan_file , overwrite_file = TRUE ){
39
+ .autoformat <- function (stan_file , overwrite_file = TRUE ,
40
+ backend = ' cmdstanr' ){
40
41
41
42
# No need to fill lv_coefs in each iteration if this is a
42
43
# trend_formula model
@@ -52,27 +53,6 @@ remove_likelihood = function(model_file){
52
53
stan_file , fixed = TRUE )] <-
53
54
' trend[i, s] = dot_product(Z[s,], LV[i,]);'
54
55
55
- # if(any(grepl('// derived latent states',
56
- # stan_file, fixed = TRUE))){
57
- # starts <- grep('// derived latent states',
58
- # stan_file, fixed = TRUE) + 1
59
- # ends <- starts + 4
60
- # stan_file <- stan_file[-c(starts:ends)]
61
- # stan_file[grep('// derived latent states',
62
- # stan_file, fixed = TRUE)] <-
63
- # paste0('// derived latent states\n',
64
- # "trend = LV * Z';")
65
- # } else {
66
- # starts <- grep('// derived latent trends',
67
- # stan_file, fixed = TRUE) + 1
68
- # ends <- starts + 4
69
- # stan_file <- stan_file[-c(starts:ends)]
70
- # stan_file[grep('// derived latent trends',
71
- # stan_file, fixed = TRUE)] <-
72
- # paste0('// derived latent trends\n',
73
- # "trend = LV * Z';")
74
- # }
75
-
76
56
stan_file [grep(' // posterior predictions' ,
77
57
stan_file , fixed = TRUE )- 1 ] <-
78
58
paste0(stan_file [grep(' // posterior predictions' ,
@@ -81,13 +61,43 @@ remove_likelihood = function(model_file){
81
61
' matrix[n_series, n_lv] lv_coefs = Z;' )
82
62
stan_file <- readLines(textConnection(stan_file ), n = - 1 )
83
63
}
64
+
65
+ if (backend == ' rstan' & rstan :: stan_version() < ' 2.29.0' ){
66
+ # normal_id_glm became available in 2.29.0; this needs to be replaced
67
+ # with the older non-glm version
68
+ if (any(grepl(' normal_id_glm' ,
69
+ stan_file , fixed = TRUE ))){
70
+ if (any(grepl(" flat_ys ~ normal_id_glm(flat_xs," ,
71
+ stan_file , fixed = TRUE ))){
72
+ start <- grep(" flat_ys ~ normal_id_glm(flat_xs," ,
73
+ stan_file , fixed = TRUE )
74
+ end <- start + 2
75
+ stan_file <- stan_file [- c((start + 1 ): (start + 2 ))]
76
+ stan_file [start ] <- ' flat_ys ~ normal(flat_xs * b, flat_sigma_obs);'
77
+ }
78
+ }
79
+ }
80
+
84
81
# Old ways of specifying arrays have been converted to errors in
85
- # the latest version of Cmdstan (2.34 .0); this coincides with
82
+ # the latest version of Cmdstan (2.32 .0); this coincides with
86
83
# a decision to stop automatically replacing these deprecations with
87
84
# the canonicalizer, so we have no choice but to replace the old
88
- # syntax with this ugly bit of code:
89
- if (requireNamespace(' cmdstanr' ) & cmdstanr :: cmdstan_version() > = " 2.33.0" ){
85
+ # syntax with this ugly bit of code
90
86
87
+ # rstan dependency in Description should mean that updates should
88
+ # always happen (mvgam depends on rstan >= 2.29.0)
89
+ update_code <- TRUE
90
+
91
+ # Tougher if using cmdstanr
92
+ if (backend == ' cmdstanr' ){
93
+ if (cmdstanr :: cmdstan_version() < " 2.32.0" ){
94
+ # If the autoformat options from cmdstanr are available,
95
+ # make use of them to update any deprecated array syntax
96
+ update_code <- FALSE
97
+ }
98
+ }
99
+
100
+ if (update_code ){
91
101
# Data modifications
92
102
stan_file [grep(" int<lower=0> ytimes[n, n_series]; // time-ordered matrix (which col in X belongs to each [time, series] observation?)" ,
93
103
stan_file , fixed = TRUE )] <-
@@ -426,14 +436,21 @@ remove_likelihood = function(model_file){
426
436
}
427
437
}
428
438
429
- stan_file <- cmdstanr :: write_stan_file(stan_file )
430
- cmdstan_mod <- cmdstanr :: cmdstan_model(stan_file , compile = FALSE )
431
- out <- utils :: capture.output(
432
- cmdstan_mod $ format(
433
- max_line_length = 80 ,
434
- canonicalize = TRUE ,
435
- overwrite_file = overwrite_file , backup = FALSE ))
436
- paste0(out , collapse = " \n " )
439
+ if (backend == ' rstan' ){
440
+ options(stanc.allow_optimizations = TRUE ,
441
+ stanc.auto_format = TRUE )
442
+ out <- rstan :: stanc(model_code = stan_file )$ model_code
443
+ } else {
444
+ stan_file <- cmdstanr :: write_stan_file(stan_file )
445
+ cmdstan_mod <- cmdstanr :: cmdstan_model(stan_file , compile = FALSE )
446
+ out <- utils :: capture.output(
447
+ cmdstan_mod $ format(
448
+ max_line_length = 80 ,
449
+ canonicalize = TRUE ,
450
+ overwrite_file = overwrite_file , backup = FALSE ))
451
+ out <- paste0(out , collapse = " \n " )
452
+ }
453
+ return (out )
437
454
}
438
455
439
456
# ### Replacement for MCMCvis functions to remove dependence on rstan for working
0 commit comments