Crossed Random effect-DIM mismatch

Data:

subjects receives 1 or 2 or 3 different drugs in each study. Ex: study1 =1 drug, study2 = 2drugs, study3 = 3 drugs etc..; total 5 studies. drugs within study are constant. single dose of each drug at time = 0. Multiple drug measurements for each drug. Allowing crossed Random effect between drugs and subjects and also allowing covariance between drugs within subject. Getting error as below stemming from matrix[J, K] dose_amount; Dim mismatch.

Error:

"Chain 1 Unrecoverable error evaluating the log probability at the initial value.

Chain 1 Exception: multiply: Columns of m1 (13) and Rows of m2 (851) must match in size (in at line 61) :

Can you please guide me on how to input dose_amount in stan in this scenario?

Current dose_amount in R:

amt_time ← con_time_from_first_dose %>%

group_by(ID, COMPN) %>%

arrange(ID, TIME, COMPN) %>%

slice(1) %>%

ungroup() %>%

select(ID, TIME, COMPN, COMP, DOSEA)

dose_amount ← matrix(0, nrow = nsubj, ncol = ndrug) # Assuming dose_amount is a matrix

for (i in 1:nrow(amt_time)) {

dose_amount[subject_id_map[as.character(amt_time$ID[i])], drug_id[i]] ← amt_time$DOSEA[i]

}



model functions {

  real twoCptModel1(real time, real dose, real CL, real Q, real V1, real V2, real ka, int is_iv) {

    real k10;

    real k12;

    real k21;

    real predicted_concentration;

 

    k10 = CL / V1;

    k12 = Q / V1;

    k21 = Q / V2;

 

    if (is_iv == 1) {

      // IV administration

      predicted_concentration = (dose / V1) * (exp(-k10 * time) - exp(-k21 * time)) / (k21 - k10);

    } else {

      // SC administration

      predicted_concentration = (dose * ka / (V1 * (ka - k10))) * (exp(-k10 * time) - exp(-ka * time));

    }

 

    return predicted_concentration;

  }

 

  vector twoCptModel(real[] time, real dose, real CL, real Q, real V1, real V2, real ka, int is_iv) {

    vector[size(time)] conc;

    for (i in 1:size(time)) {

      conc[i] = twoCptModel1(time[i], dose, CL, Q, V1, V2, ka, is_iv);

    }

    return conc;

  }

}

 

data {

  int<lower=0> N;  // Number of concentration measurements

  int<lower=0> J;  // Number of subjects

  int<lower=1> K;  // Number of drugs

  int<lower=1,upper=J> subject_id[N];

  int<lower=1,upper=K> drug_id[N];

  vector[N] time;

  vector[N] concentration;

  matrix[J, K] dose_amount;  // Dose amount for each subject and drug

  int<lower=0,upper=1> is_iv[N];

}

 

parameters {

  real<lower=0> CL;

  real<lower=0> V1;

  real<lower=0> V2;

  real<lower=0> Q;

  real<lower=0> ka;

  matrix[J, K] drug_effect;  // Adjust type to matrix

  cholesky_factor_corr[K] L_Omega;

  vector<lower=0>[K] sigma_drug;

  real<lower=0> sigma;

}

 

transformed parameters {

  matrix[J, K] drug_effects;

  vector[N] predicted_concentration;

 

  drug_effects = diag_pre_multiply(sigma_drug, L_Omega) * drug_effect;

 

  for (n in 1:N) {

    real z_effect = drug_effects[subject_id[n], drug_id[n]];

    real dose = dose_amount[subject_id[n], drug_id[n]];

 

    predicted_concentration[n] = twoCptModel1(time[n], dose, CL, Q, V1, V2, ka, is_iv[n]);

    predicted_concentration[n] += z_effect;  // Apply random effect

  }

}

 

model {

  // Priors

  for (j in 1:J) {

    drug_effect[j] ~ normal(0, 1);

  }

  L_Omega ~ lkj_corr_cholesky(2.0);

  sigma_drug ~ cauchy(0, 2.5);

 

  // Likelihood

  concentration ~ lognormal(log(predicted_concentration), sigma);

}"

Hi, @somkamalin and welcome to Stan’s forums.

the key to the error is this:

Chain 1 Exception: multiply: Columns of m1 (13) and Rows of m2 (851) must match in size (in at line 61) :

Unfortunately, the line numbers were corrupted in the attachment. Maybe that’s why there are so many blank vertical lines?

I would recommend putting your Stan program in its own file so that you can goto the appropriate line indicated in an error message. Our math library doesn’t have the variable names at the point an error is thrown, hence the generic m1 and m2. The best we have is a line number.

What this error says is that you are trying to multiply two matrices m1 * m2, but the sizes don’t conform—to conform, the number of columns of m1 has to be the same as number of rows in m2.

As @Bob_Carpenter said, it’s hard to tell which line is the culprit, but I suspect it’s this one.

drug_effects and drug_effect are defined as J \times K matrices. diag_pre_multiply(sigma_drug, L_Omega) will produce a K \times K matrix. If it doesn’t break something somewhere else in your code, I think that defining drug_effects and drug_effect as K \times J matrices will fix the problem.

I fixed it. Thank you. Now getting this error, I think due to crossed random effect. Does it mean model needs to be simplified?.

Running MCMC with 1 chain…

Chain 1 Rejecting initial value:

Chain 1 Error evaluating the log probability at the initial value.

Chain 1 Exception: lognormal_lpdf: Location parameter[1] is nan, but must be finite! (in ‘/tmp/RtmpMXTE3M/model-418b3b6d64e3.stan’ at line 82)

Your likelihood is

concentration ~ lognormal(log(predicted_concentration), sigma);

I suspect the problem is that you’ve defined predicted_concentration as follows.

That allows it to take on any values in (-\infty, \infty). If you were to define it so that it takes on values only in (0, \infty), that might fix the problem.

 vector<lower=0>[N] predicted_concentration;
1 Like