Specifying inits: mismatch in number dimensions declared and found in context

I am trying to build a non-linear model in brms discussed in this thread.

I thought the challenge I’m currently facing might be more broadly applicable, so I thought perhaps it would benefit from its own thread.

I am unable to pass initial values to the sampler, which results in no sampling being done. Regardless of what I specify, the default range of (-2,2) is used.

The generated Stan code is:

functions {
}
data {
  int<lower=1> N;  // total number of observations
  vector[N] Y;  // response variable
  int<lower=1> K_a;  // number of population-level effects
  matrix[N, K_a] X_a;  // population-level design matrix
  int<lower=1> K_b;  // number of population-level effects
  matrix[N, K_b] X_b;  // population-level design matrix
  int<lower=1> K_c;  // number of population-level effects
  matrix[N, K_c] X_c;  // population-level design matrix
  // covariate vectors for non-linear functions
  vector[N] C_B_1;
  // covariate vectors for non-linear functions
  vector[N] C_I_1;
  int prior_only;  // should the likelihood be ignored?
}
transformed data {
}
parameters {
  vector<lower=1>[K_a] b_a;  // population-level effects
  vector<lower=1>[K_b] b_b;  // population-level effects
  vector<lower=1>[K_c] b_c;  // population-level effects
  real<lower=0> phi;  // precision parameter
}
transformed parameters {
}
model {
  // likelihood including constants
  if (!prior_only) {
    // initialize linear predictor term
    vector[N] nlp_a = X_a * b_a;
    // initialize linear predictor term
    vector[N] nlp_b = X_b * b_b;
    // initialize linear predictor term
    vector[N] nlp_c = X_c * b_c;
    // initialize non-linear predictor term
    vector[N] nlp_B;
    // initialize non-linear predictor term
    vector[N] nlp_I;
    // initialize non-linear predictor term
    vector[N] mu;
    for (n in 1:N) {
      // compute non-linear predictor values
      nlp_B[n] = nlp_a[n] * C_B_1[n] ^ nlp_b[n] + nlp_c[n];
    }
    for (n in 1:N) {
      // compute non-linear predictor values
      nlp_I[n] = C_I_1[n] * nlp_B[n];
    }
    for (n in 1:N) {
      // compute non-linear predictor values
      mu[n] = inv_logit(nlp_I[n]);
    }
    target += beta_lpdf(Y | mu * phi, (1 - mu) * phi);
  }
  // priors including constants
  target += normal_lpdf(b_a | 20,10)
    - 1 * normal_lccdf(1 | 20,10);
  target += normal_lpdf(b_b | 20,10)
    - 1 * normal_lccdf(1 | 20,10);
  target += normal_lpdf(b_c | 20,10)
    - 1 * normal_lccdf(1 | 20,10);
  target += gamma_lpdf(phi | 0.01, 0.01);
}
generated quantities {
}

The parameters block lists b-a, b_b, b_c, and phi as parameters. The vectors [K_a], [K_b], and [K_c] are length 1.

I have tried to pass initial values as follows:

NL_Mod <- brm(bform,
                Data,
                family = Beta(), 
                prior = nl_priors,
                inits = list(
                      list(phi = 0, 
                           b_a = rep(10, 1), 
                           b_b = rep(10, 1), 
                           b_c = rep(10, 1))),
                iter = 2000,
                warmup = 1000,
                chains = 1,
                cores = ncores,
                #backend = "cmdstan",
                #normalize = FALSE,
                save_pars = save_pars(all = TRUE),
                control = list(adapt_delta = 0.9,
                               max_treedepth = 12)
)

This gives the following error:

SAMPLING FOR MODEL 'd66ef5e5c4284e2da8d26adfefd756fb' NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=b_a; dims declared=(1); dims found=()
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                                                                                                             
[2] "  mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=b_a; dims declared=(1); dims found=()"

The parameters block lists
vector<lower=1>[K_a] b_a; // population-level effects, which looks like a vector of length 1, but the error message says it’s dimensionless.

Am I misunderstanding or mis-specifying?

1 Like

I was able to get past this error by declaring initial values for vectoroized parameters as array

init_list <- list(
        list(b_a = array(data = 10),
                  b_b = array(data =  10),
                  b_c = array(data = 10),
             phi = 0)

3 Likes