Scalar multiplication with optional parameter

Hello all,

I am interested in including an optional parameter in my model using an approach as described here

Optional Parameters/Data in Stan (martinmodrak.cz)

I’ve omitted most of the model as I think the main issue is my lack of understanding of Stan’s data types and function signatures.

In the simpler model, there is no data y_age and the logical incl_age is FALSE.

For the extended model, we have as many y_age values as there are observations in the data and incl_age is TRUE and the parameter decay is a positive scalar.

My attempt at this leads to a Stan file similar to

data {
  int<lower=1> n_obs;

  // Other data ...

  int<lower=0, upper=1> incl_age;
  real<lower=0> y_age[incl_age ? n_obs : 0];
}
parameters {
  // Other parameters ...

  real<lower=0> decay[incl_age ? 1 : 0];
}
model {
  // Other model statements ...

  if (incl_age) target += decay * y_age;
}

This fails for both multiplication * and elementwise multiplication .* in the last line.

Is it possible to make these data and parameter optional and still increment a quantity by the scalar multiplied vector?

Would it be simpler, or perhaps more efficient, to simplify the process into separate Stan files?

Any feedback would be appreciated.

Two questions:

  1. are the attempts that are failing ones where y_age has data and incl_age is true, where y_age has no data and incl_age is false, or is it failing on both (and are you using print statements to monitor the status of incl_age)?

  2. Does the hidden part of the model contain a conditional statement for what the model should do if incl_age is false?

I’ve oversimplified this example to the point of confusion. Sorry.

  1. The Stan model was not compiling so it was failing independent of data.
    That is, using the RStudio toolbar option “Check” (runs rstan:::rstudio_stanc("inst/stan/dbm.stan")) was failing due to incompatible data types
  • real array[] * real array[], or previously
  • real array[] * vector[]
  1. Yes, the idea behind the model was to include a renewal process component in an existing model.
    See my next reply for a better demonstration of what I was trying to achieve.

See the following for a Poisson regression with an adjustment relative to the “age” of each observation.

This is a no longer a typical Poisson process as the rate over time is not constant but the Stan code runs which is what I was struggling with.

data {
  int<lower=1> n_obs;
  int<lower=1> n_x;

  array[n_obs] int y;
  matrix[n_obs, n_x] x;

  int<lower=0, upper=1> incl_age;
  vector[incl_age ? n_obs : 0] y_age;
}
parameters {
  real Intercept;
  vector[n_x] regr;

  real<lower=0> decay[incl_age ? 1 : 0];
}
transformed parameters {
  // Expecation adjustment
  vector[n_obs] adjust = rep_vector(0.0, n_obs);
  adjust += Intercept;

  if (incl_age) adjust += decay[1] * y_age;
}
model {
  // Prior Information
  Intercept ~ normal(1, 100);
  regr ~ normal(1, 100);

  if (incl_age) decay ~ gamma(1, 0.1);

  // Log likelihood
  target += poisson_log_glm_lpmf(y | x, adjust, regr);
}

The subsetting of the 1-length array (i.e. decay[1]) doesn’t look natural but should be fine as it is only used inside the conditional and we know it’s length will always be 1

I see you’ve marked your reply as the solution. Is this resolved? If not, could you post the full code that’s not running?