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);
}"