@@ -277,20 +277,12 @@ jsdgam = function(formula,
277
277
model_file <- model_file [- grep(" matrix[n_series, n_lv] Z; // matrix mapping series to latent states" ,
278
278
model_file , fixed = TRUE )]
279
279
280
- # Update transformed data
281
- if (any(grepl(' transformed data {' , model_file , fixed = TRUE ))){
282
- model_file [grep(' transformed data {' , model_file , fixed = TRUE )] <-
283
- paste0(' transformed data {\n ' ,
284
- ' // Ensures identifiability of the model - no rotation of factors\n ' ,
285
- ' int<lower=1> M;\n ' ,
286
- ' M = n_lv * (n_series - n_lv) + n_lv * (n_lv - 1) / 2 + n_lv;' )
287
- } else {
288
- model_file [grep(' parameters {' , model_file , fixed = TRUE )[1 ]] <-
289
- paste0(' // Ensures identifiability of the model - no rotation of factors\n ' ,
290
- ' int<lower=1> M;\n ' ,
291
- ' M = n_lv * (n_series - n_lv) + n_lv * (n_lv - 1) / 2 + n_lv;' ,
292
- ' }\n parameters {' )
293
- }
280
+ # Add M to data block
281
+ model_file [grep(' int<lower=0> n_lv; // number of dynamic factors' ,
282
+ model_file , fixed = TRUE )] <- paste0(
283
+ ' int<lower=0> n_lv; // number of dynamic factors\n ' ,
284
+ ' int<lower=0> M; // number of nonzero factor loadings'
285
+ )
294
286
model_file <- readLines(textConnection(model_file ), n = - 1 )
295
287
296
288
# Update parameters
@@ -374,6 +366,10 @@ jsdgam = function(formula,
374
366
model_data <- mod $ model_data
375
367
model_data $ Z <- NULL
376
368
369
+ # Add M to model_data
370
+ n_series <- NCOL(model_data $ ytimes )
371
+ model_data $ M <- n_lv * (n_series - n_lv ) + n_lv * (n_lv - 1 ) / 2 + n_lv
372
+
377
373
# ### Autoformat the Stan code ####
378
374
if (requireNamespace(' cmdstanr' , quietly = TRUE ) & backend == ' cmdstanr' ){
379
375
if (requireNamespace(' cmdstanr' ) &
0 commit comments