Skip to content

Commit 095e020

Browse files
Merge pull request #3 from andrjohns/rstan2.36-compat
Update deprecated Stan array syntax
2 parents fdf3322 + 1af7010 commit 095e020

File tree

4 files changed

+138
-138
lines changed

4 files changed

+138
-138
lines changed

inst/stan/VAR_latent.stan

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,64 @@
22
data {
33
int<lower=1> N; // number of observational units
44
int<lower=1> D; // number of latent constructs
5-
int<lower=1> D_np[D]; // number of indicators per construct
5+
array[D] int<lower=1> D_np; // number of indicators per construct
66
int<lower=1> n_p; // number of manifest indicators
7-
int D_perP[n_p]; // indicate dimension per indicator
8-
int is_SI[n_p]; // indicate if single-indicator per construct
9-
int D_pos_is_SI[D]; // indicate position of single-indicator per construct
7+
array[n_p] int D_perP; // indicate dimension per indicator
8+
array[n_p] int is_SI; // indicate if single-indicator per construct
9+
array[D] int D_pos_is_SI; // indicate position of single-indicator per construct
1010
int<lower=1, upper=3> maxLag; // maximum lag
1111
int<lower=1> N_obs; // observations in total: N * TP
1212
int<lower=1> n_pars;
1313
int<lower=D> n_random; // number of random effects
1414
int n_fixed;
15-
int is_fixed[1,n_fixed];
16-
int is_random[n_random]; // which parameters to model person-specific
17-
int<lower=1> N_obs_id[N]; // number of observations for each unit
18-
vector[N_obs] y[n_p]; // observations as array of vectors
15+
array[1,n_fixed] int is_fixed;
16+
array[n_random] int is_random; // which parameters to model person-specific
17+
array[N] int<lower=1> N_obs_id; // number of observations for each unit
18+
array[n_p] vector[N_obs] y; // observations as array of vectors
1919

2020
// handling of missing values
2121
int n_miss; // total number of missings across indicators
22-
int n_miss_p[n_p]; // missings per indicator
23-
int pos_miss_p[n_p,max(n_miss_p)]; // array of missings' positions
22+
array[n_p] int n_miss_p; // missings per indicator
23+
array[n_p,max(n_miss_p)] int pos_miss_p; // array of missings' positions
2424

2525
// model adaptations based on user inputs:
2626
// - fixing parameters to constant values:
2727
// - innovation variances
28-
int<lower=0,upper=1> innos_rand[D];
28+
array[D] int<lower=0,upper=1> innos_rand;
2929
int n_innos_fix;
30-
int innos_fix_pos[D];
31-
int innos_pos[D];
30+
array[D] int innos_fix_pos;
31+
array[D] int innos_pos;
3232

3333
// - covariances of innovations:
3434
int n_inno_covs; // number of potential innovation covs to include
3535
int n_obs_cov; // total number of residuals
36-
int inno_cov_pos[1,n_inno_covs];
37-
int<lower=-1,upper=1> inno_cov_load[2];
36+
array[1,n_inno_covs] int inno_cov_pos;
37+
array[2] int<lower=-1,upper=1> inno_cov_load;
3838

3939

4040
// - dynamic model specification per D
41-
int<lower=1> N_pred[D]; // Number of predictors per dimension
42-
int<lower=0> D_pred[D,D*maxLag]; // matrix to index predictors to use per dimension
43-
int<lower=0> Lag_pred[D,D*maxLag]; // matrix to index lag of used predictors
44-
int Dpos1[D]; // index positions of danymic effect parameters
45-
int Dpos2[D];
41+
array[D] int<lower=1> N_pred; // Number of predictors per dimension
42+
array[D,D*maxLag] int<lower=0> D_pred; // matrix to index predictors to use per dimension
43+
array[D,D*maxLag] int<lower=0> Lag_pred; // matrix to index lag of used predictors
44+
array[D] int Dpos1; // index positions of danymic effect parameters
45+
array[D] int Dpos2;
4646

4747
// - time-invariant variables:
4848
// covariates as predictors of random effects
4949
int<lower=1> n_cov; // number of covariates - minimum of 1 for intercepts
5050
int n_cov_bs;
51-
int n_cov_mat[n_cov_bs, 2];
51+
array[n_cov_bs, 2] int n_cov_mat;
5252
matrix[N, n_cov] W; // predictors of individual parameters
5353

5454
// outcome prediction
5555
int n_out; // number of outcome variables
56-
int n_out_bs[n_out,1]; // number of predictors per outcome
56+
array[n_out,1] int n_out_bs; // number of predictors per outcome
5757
int n_out_bs_max; // number of predictors per outcome
5858
int n_out_bs_sum; // number of predictors per outcome
59-
int n_out_b_pos[n_out,n_out_bs_max]; // index positions
59+
array[n_out,n_out_bs_max] int n_out_b_pos; // index positions
6060
int n_z; // number of additional time-invariant as outcome predictors
6161
matrix[N, n_z] Z; // observations of Z
62-
vector[N] out[n_out]; // outcome
62+
array[n_out] vector[N] out; // outcome
6363

6464
// indexing information on constraints
6565
int n_etaW_free;
@@ -68,16 +68,16 @@ data {
6868
int n_alphafree;
6969
int n_sigmaBfree;
7070
int n_sigmaWfree;
71-
int pos_loadBfree[n_loadBfree]; // positions in relation to the 1:n_p indicators
72-
int pos_loadWfree[n_loadWfree];
73-
int pos_alphafree[n_alphafree];
74-
int pos_sigmaBfree[n_sigmaBfree];
75-
int pos_sigmaWfree[n_sigmaWfree];
71+
array[n_loadBfree] int pos_loadBfree; // positions in relation to the 1:n_p indicators
72+
array[n_loadWfree] int pos_loadWfree;
73+
array[n_alphafree] int pos_alphafree;
74+
array[n_sigmaBfree] int pos_sigmaBfree;
75+
array[n_sigmaWfree] int pos_sigmaWfree;
7676
// index random manifest indicator means
7777
int n_YB_free; // number of indicators for which mu (YB) is not determined by random item mean
78-
int YB_free_pos[n_p]; //
79-
int mu_is_etaB[n_p]; //
80-
int mu_etaB_pos[n_p]; // indicate whether to use etaB or random item mean
78+
array[n_p] int YB_free_pos; //
79+
array[n_p] int mu_is_etaB; //
80+
array[n_p] int mu_etaB_pos; // indicate whether to use etaB or random item mean
8181
// get SDs for standardized results
8282
int<lower=0,upper=1> standardized;
8383

@@ -113,7 +113,7 @@ transformed data{
113113
}
114114

115115
parameters {
116-
vector[n_random] b_free[N]; // person-specific parameter
116+
array[N] vector[n_random] b_free; // person-specific parameter
117117
vector<lower=0>[n_random] sd_R; // random effect SD
118118
vector<lower=0>[n_innos_fix] sigma; // SDs of fixed innovation variances
119119
cholesky_factor_corr[n_random] L; // cholesky factor of random effects correlation matrix
@@ -124,23 +124,23 @@ parameters {
124124
vector[n_out] alpha_out; // outcome precition intercepts
125125
vector<lower=0>[n_out] sigma_out; // residual SD(s) of outcome(s)
126126
vector[n_out_bs_sum] b_out_pred; // regression coefs of out prediction
127-
vector[n_obs_cov] eta_cov[n_inno_covs];
127+
array[n_inno_covs] vector[n_obs_cov] eta_cov;
128128

129129
// measurement model parameters
130130
vector[n_loadBfree] loadB_free;
131131
vector[n_loadWfree] loadW_free;
132132
vector[n_alphafree] alpha_free;
133133
vector<lower=0>[n_sigmaBfree] sigmaB_free;
134134
vector<lower=0>[n_sigmaWfree] sigmaW_free;
135-
vector[N_obs] etaW_free[n_etaW_free];
136-
vector[N] YB_free[n_YB_free];
135+
array[n_etaW_free] vector[N_obs] etaW_free;
136+
array[n_YB_free] vector[N] YB_free;
137137
}
138138

139139
transformed parameters {
140140
matrix[N, n_random] bmu; // gammas of person-specific parameters
141141
matrix[N,n_pars] b;
142-
vector<lower = 0>[N] sd_noise[D];
143-
vector<lower = 0>[N] sd_inncov[n_inno_covs];
142+
array[D] vector<lower = 0>[N] sd_noise;
143+
array[n_inno_covs] vector<lower = 0>[N] sd_inncov;
144144
matrix[n_cov, n_random] b_re_pred_mat = rep_matrix(0, n_cov, n_random);
145145

146146
vector[n_p] loadB = rep_vector(1, n_p); // measurement model parameters
@@ -199,9 +199,9 @@ model {
199199
int p_miss = 1; // running counter variable to index positions on y_impute
200200
int obs_id = 1; // declare local variable to store variable number of obs per person
201201
matrix[n_random, n_random] SIGMA = diag_pre_multiply(sd_R, L);
202-
vector[N_obs] y_merge[n_p];
203-
vector[N_obs] Ymus[n_p];
204-
vector[N] YB[n_p];
202+
array[n_p] vector[N_obs] y_merge;
203+
array[n_p] vector[N_obs] Ymus;
204+
array[n_p] vector[N] YB;
205205

206206
y_merge = y; // add observations
207207
if(n_miss>0){ // add imputed values for missings on each indicator
@@ -253,7 +253,7 @@ model {
253253
// store number of observations per person
254254
obs_id = (N_obs_id[pp]);
255255
int pos_etaW_free = 1; // running counter variable to index positition on etaW_free
256-
vector[obs_id] etaW_id[D];
256+
array[D] vector[obs_id] etaW_id;
257257
for(d in 1:D){
258258
if(D_np[d] == 1){
259259
etaW_id[d,] = segment(y_merge[D_pos_is_SI[d],], pos, obs_id) - YB[D_pos_is_SI[d],pp];
@@ -271,8 +271,8 @@ model {
271271
}
272272

273273
// dynamic process
274-
vector[obs_id-maxLag] mus[D];
275-
vector[obs_id-maxLag] eta_cov_id[n_inno_covs];
274+
array[D] vector[obs_id-maxLag] mus;
275+
array[n_inno_covs] vector[obs_id-maxLag] eta_cov_id;
276276
if(n_inno_covs > 0){
277277
for(i in 1:n_inno_covs){
278278
eta_cov_id[i,] = segment(eta_cov[i,], pos_cov, (obs_id-maxLag));
@@ -345,7 +345,7 @@ model {
345345
generated quantities{
346346
matrix[n_random,n_random] bcorr; // random coefficients correlation matrix
347347
vector[n_SD_etaW_all] SD_etaW;
348-
vector[n_SD_etaW_i] SD_etaW_i[n_SD_etaW_all];
348+
array[n_SD_etaW_all] vector[n_SD_etaW_i] SD_etaW_i;
349349
bcorr = multiply_lower_tri_self_transpose(L);
350350
if(standardized == 1){
351351
for(i in 1:n_SD_etaW_all){

inst/stan/VAR_latentCovsFix.stan

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,57 @@
22
data {
33
int<lower=1> N; // number of observational units
44
int<lower=1> D; // number of latent constructs
5-
int<lower=1> D_np[D]; // number of indicators per construct
5+
array[D] int<lower=1> D_np; // number of indicators per construct
66
int<lower=1> n_p; // number of manifest indicators
7-
int D_perP[n_p]; // indicate dimension per indicator
8-
int is_SI[n_p]; // indicate if single-indicator per construct
9-
int D_pos_is_SI[D]; // indicate position of single-indicator per construct
7+
array[n_p] int D_perP; // indicate dimension per indicator
8+
array[n_p] int is_SI; // indicate if single-indicator per construct
9+
array[D] int D_pos_is_SI; // indicate position of single-indicator per construct
1010
int<lower=1, upper=3> maxLag; // maximum lag
1111
int<lower=1> N_obs; // observations in total: N * TP
1212
int<lower=1> n_pars;
1313
int<lower=D> n_random; // number of random effects
1414
int n_fixed;
15-
int is_fixed[1,n_fixed];
16-
int is_random[n_random]; // which parameters to model person-specific
17-
int<lower=1> N_obs_id[N]; // number of observations for each unit
18-
vector[N_obs] y[n_p]; // observations as array of vectors
15+
array[1,n_fixed] int is_fixed;
16+
array[n_random] int is_random; // which parameters to model person-specific
17+
array[N] int<lower=1> N_obs_id; // number of observations for each unit
18+
array[n_p] vector[N_obs] y; // observations as array of vectors
1919

2020
// handling of missing values
2121
int n_miss; // total number of missings across indicators
22-
int n_miss_p[n_p]; // missings per indicator
23-
int pos_miss_p[n_p,max(n_miss_p)]; // array of missings' positions
22+
array[n_p] int n_miss_p; // missings per indicator
23+
array[n_p,max(n_miss_p)] int pos_miss_p; // array of missings' positions
2424

2525
// model adaptations based on user inputs:
2626
// - fixing parameters to constant values:
2727
// - innovation variances
28-
int<lower=0,upper=1> innos_rand[D];
28+
array[D] int<lower=0,upper=1> innos_rand;
2929
int n_innos_fix;
30-
int innos_fix_pos[D];
31-
int innos_pos[D];
30+
array[D] int innos_fix_pos;
31+
array[D] int innos_pos;
3232

3333
// - dynamic model specification per D
34-
int<lower=1> N_pred[D]; // Number of predictors per dimension
35-
int<lower=0> D_pred[D,D*maxLag]; // matrix to index predictors to use per dimension
36-
int<lower=0> Lag_pred[D,D*maxLag]; // matrix to index lag of used predictors
37-
int Dpos1[D]; // index positions of danymic effect parameters
38-
int Dpos2[D];
34+
array[D] int<lower=1> N_pred; // Number of predictors per dimension
35+
array[D,D*maxLag] int<lower=0> D_pred; // matrix to index predictors to use per dimension
36+
array[D,D*maxLag] int<lower=0> Lag_pred; // matrix to index lag of used predictors
37+
array[D] int Dpos1; // index positions of danymic effect parameters
38+
array[D] int Dpos2;
3939

4040
// - time-invariant variables:
4141
// covariates as predictors of random effects
4242
int<lower=1> n_cov; // number of covariates - minimum of 1 for intercepts
4343
int n_cov_bs;
44-
int n_cov_mat[n_cov_bs, 2];
44+
array[n_cov_bs, 2] int n_cov_mat;
4545
matrix[N, n_cov] W; // predictors of individual parameters
4646

4747
// outcome prediction
4848
int n_out; // number of outcome variables
49-
int n_out_bs[n_out,1]; // number of predictors per outcome
49+
array[n_out,1] int n_out_bs; // number of predictors per outcome
5050
int n_out_bs_max; // number of predictors per outcome
5151
int n_out_bs_sum; // number of predictors per outcome
52-
int n_out_b_pos[n_out,n_out_bs_max]; // index positions
52+
array[n_out,n_out_bs_max] int n_out_b_pos; // index positions
5353
int n_z; // number of additional time-invariant as outcome predictors
5454
matrix[N, n_z] Z; // observations of Z
55-
vector[N] out[n_out]; // outcome
55+
array[n_out] vector[N] out; // outcome
5656

5757
// indexing information on constraints
5858
int n_etaW_free;
@@ -61,16 +61,16 @@ data {
6161
int n_alphafree;
6262
int n_sigmaBfree;
6363
int n_sigmaWfree;
64-
int pos_loadBfree[n_loadBfree]; // positions in relation to the 1:n_p indicators
65-
int pos_loadWfree[n_loadWfree];
66-
int pos_alphafree[n_alphafree];
67-
int pos_sigmaBfree[n_sigmaBfree];
68-
int pos_sigmaWfree[n_sigmaWfree];
64+
array[n_loadBfree] int pos_loadBfree; // positions in relation to the 1:n_p indicators
65+
array[n_loadWfree] int pos_loadWfree;
66+
array[n_alphafree] int pos_alphafree;
67+
array[n_sigmaBfree] int pos_sigmaBfree;
68+
array[n_sigmaWfree] int pos_sigmaWfree;
6969
// index random manifest indicator means
7070
int n_YB_free; // number of indicators for which mu (YB) is not determined by random item mean
71-
int YB_free_pos[n_p]; //
72-
int mu_is_etaB[n_p]; //
73-
int mu_etaB_pos[n_p]; // indicate whether to use etaB or random item mean
71+
array[n_p] int YB_free_pos; //
72+
array[n_p] int mu_is_etaB; //
73+
array[n_p] int mu_etaB_pos; // indicate whether to use etaB or random item mean
7474
// get SDs for standardized results
7575
int<lower=0,upper=1> standardized;
7676

@@ -104,7 +104,7 @@ transformed data{
104104
}
105105

106106
parameters {
107-
vector[n_random] b_free[N]; // person-specific parameter
107+
array[N] vector[n_random] b_free; // person-specific parameter
108108
vector<lower=0>[n_random] sd_R; // random effect SD
109109
vector<lower=0>[n_innos_fix] sigma; // SDs of fixed innovation variances
110110
cholesky_factor_corr[n_random] L; // cholesky factor of random effects correlation matrix
@@ -123,14 +123,14 @@ parameters {
123123
vector[n_alphafree] alpha_free;
124124
vector<lower=0>[n_sigmaBfree] sigmaB_free;
125125
vector<lower=0>[n_sigmaWfree] sigmaW_free;
126-
vector[N_obs] etaW_free[n_etaW_free];
127-
vector[N] YB_free[n_YB_free];
126+
array[n_etaW_free] vector[N_obs] etaW_free;
127+
array[n_YB_free] vector[N] YB_free;
128128
}
129129

130130
transformed parameters {
131131
matrix[N, n_random] bmu; // gammas of person-specific parameters
132132
matrix[N,n_pars] b;
133-
vector<lower = 0>[D] sd_noise[N];
133+
array[N] vector<lower = 0>[D] sd_noise;
134134
matrix[n_cov, n_random] b_re_pred_mat = rep_matrix(0, n_cov, n_random);
135135

136136
vector[n_p] loadB = rep_vector(1, n_p); // measurement model parameters
@@ -182,9 +182,9 @@ model {
182182
int obs_id = 1; // declare local variable to store variable number of obs per person
183183
matrix[n_random, n_random] SIGMA = diag_pre_multiply(sd_R, L);
184184
matrix[D, D] SIGMA_inno = diag_pre_multiply(sd_noise[1,], L_inno);
185-
vector[N_obs] y_merge[n_p];
186-
vector[N_obs] Ymus[n_p];
187-
vector[N] YB[n_p];
185+
array[n_p] vector[N_obs] y_merge;
186+
array[n_p] vector[N_obs] Ymus;
187+
array[n_p] vector[N] YB;
188188

189189
y_merge = y; // add observations
190190
if(n_miss>0){ // add imputed values for missings on each indicator
@@ -237,8 +237,8 @@ model {
237237
// store number of observations per person
238238
obs_id = (N_obs_id[pp]);
239239
int pos_etaW_free = 1; // running counter variable to index positition on etaW_free
240-
vector[D] etaW_use[obs_id - maxLag];
241-
vector[obs_id] etaW_id[D];
240+
array[obs_id - maxLag] vector[D] etaW_use;
241+
array[D] vector[obs_id] etaW_id;
242242
for(d in 1:D){
243243
if(D_np[d] == 1){
244244
etaW_id[d,] = segment(y_merge[D_pos_is_SI[d],], pos, obs_id) - YB[D_pos_is_SI[d],pp];
@@ -256,7 +256,7 @@ model {
256256
}
257257

258258
// dynamic process
259-
vector[D] mus[obs_id-maxLag];
259+
array[obs_id-maxLag] vector[D] mus;
260260
for(d in 1:D){ // start loop over dimensions
261261

262262
// build prediction matrix for specific dimensions
@@ -317,7 +317,7 @@ generated quantities{
317317
matrix[n_random,n_random] bcorr; // random coefficients correlation matrix
318318
matrix[D,D] bcorr_inn; // random coefficients correlation matrix
319319
vector[n_SD_etaW_all] SD_etaW;
320-
vector[n_SD_etaW_i] SD_etaW_i[n_SD_etaW_all];
320+
array[n_SD_etaW_all] vector[n_SD_etaW_i] SD_etaW_i;
321321
bcorr = multiply_lower_tri_self_transpose(L);
322322
bcorr_inn = multiply_lower_tri_self_transpose(L_inno);
323323
if(standardized == 1){

0 commit comments

Comments
 (0)