Skip to content

Commit

Permalink
update model examples wrt new stan syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi Zhang committed May 18, 2022
1 parent 50dc755 commit 0168482
Show file tree
Hide file tree
Showing 25 changed files with 1,611 additions and 497 deletions.
28 changes: 14 additions & 14 deletions example-models/FK_coupled/FribergKarlsson.stan
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
functions{
vector FK_ODE(real t, vector y, vector y_pk, real[] theta, real[] rdummy, int[] idummy){
vector FK_ODE(real t, vector y, vector y_pk, array[] real theta, array[] real rdummy, array[] int idummy){
/* PK variables */
real VC = theta[3];

Expand Down Expand Up @@ -33,16 +33,16 @@ data{
int<lower = 1> nt;
int<lower = 1> nObsPK;
int<lower = 1> nObsPD;
int<lower = 1> iObsPK[nObsPK];
int<lower = 1> iObsPD[nObsPD];
real<lower = 0> amt[nt];
int<lower = 1> cmt[nt];
int<lower = 0> evid[nt];
real<lower = 0> time[nt];
real<lower = 0> ii[nt];
int<lower = 0> addl[nt];
int<lower = 0> ss[nt];
real rate[nt];
array[nObsPK] int<lower = 1> iObsPK;
array[nObsPD] int<lower = 1> iObsPD;
array[nt] real<lower = 0> amt;
array[nt] int<lower = 1> cmt;
array[nt] int<lower = 0> evid;
array[nt] real<lower = 0> time;
array[nt] real<lower = 0> ii;
array[nt] int<lower = 0> addl;
array[nt] int<lower = 0> ss;
array[nt] real rate;
vector<lower = 0>[nObsPK] cObs;
vector<lower = 0>[nObsPD] neutObs;

Expand Down Expand Up @@ -97,10 +97,10 @@ transformed parameters{
vector<lower = 0>[nObsPK] cHatObs;
row_vector[nt] neutHat;
vector<lower = 0>[nObsPD] neutHatObs;
real<lower = 0> theta[nTheta];
array[nTheta] real<lower = 0> theta;
matrix[nOde + 3, nt] x;
real biovar[nTheta] = rep_array(1.0, nTheta);
real tlag[nTheta] = rep_array(0.0, nTheta);
array[nTheta] real biovar = rep_array(1.0, nTheta);
array[nTheta] real tlag = rep_array(0.0, nTheta);

theta[1] = CL;
theta[2] = Q;
Expand Down
35 changes: 18 additions & 17 deletions example-models/FribergKarlsson/FribergKarlsson.stan
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
functions{
vector twoCptNeutModelODE(real t, vector x, real[] parms, real[] rdummy, int[] idummy){
vector twoCptNeutModelODE(real t, vector x, array[] real parms,
array[] real rdummy, array[] int idummy){
real k10;
real k12;
real k21;
Expand Down Expand Up @@ -64,24 +65,24 @@ data{
int<lower = 1> nt;
int<lower = 1> nObsPK;
int<lower = 1> nObsPD;
int<lower = 1> iObsPK[nObsPK];
int<lower = 1> iObsPD[nObsPD];
real<lower = 0> amt[nt];
int<lower = 1> cmt[nt];
int<lower = 0> evid[nt];
real<lower = 0> time[nt];
real<lower = 0> ii[nt];
int<lower = 0> addl[nt];
int<lower = 0> ss[nt];
real rate[nt];
array[nObsPK] int<lower = 1> iObsPK;
array[nObsPD] int<lower = 1> iObsPD;
array[nt] real<lower = 0> amt;
array[nt] int<lower = 1> cmt;
array[nt] int<lower = 0> evid;
array[nt] real<lower = 0> time;
array[nt] real<lower = 0> ii;
array[nt] int<lower = 0> addl;
array[nt] int<lower = 0> ss;
array[nt] real rate;
vector<lower = 0>[nObsPK] cObs;
vector<lower = 0>[nObsPD] neutObs;

// data for population model
int<lower = 1> nSubjects;
int<lower = 1> start[nSubjects];
int<lower = 1> end[nSubjects];
real<lower = 0> weight[nSubjects];
array[nSubjects] int<lower = 1> start;
array[nSubjects] int<lower = 1> end;
array[nSubjects] real<lower = 0> weight;

real<lower = 0> circ0HatPrior;
real<lower = 0> circ0HatPriorCV;
Expand All @@ -102,7 +103,7 @@ transformed data{
int nTheta;
int nIIV;

int len[nSubjects];
array[nSubjects] int len;

logCObs = log(cObs);
logNeutObs = log(neutObs);
Expand Down Expand Up @@ -143,7 +144,7 @@ transformed parameters{
row_vector[nt] neutHat;
vector[nObsPD] neutHatObs;
matrix[8, nt] x;
real<lower = 0> parms[nSubjects, nTheta]; // The [1] indicates the parameters are constant
array[nSubjects, nTheta] real<lower = 0> parms; // The [1] indicates the parameters are constant

// variables for Matt's trick
vector<lower = 0>[nIIV] thetaHat;
Expand Down Expand Up @@ -214,7 +215,7 @@ model{

generated quantities {
matrix[8, nt] xPred;
real<lower = 0> parmsPred[nSubjects, nTheta]; // [1] indicates the parameters are constant
array[nSubjects, nTheta] real<lower = 0> parmsPred; // [1] indicates the parameters are constant
row_vector[nt] cHatPred;
row_vector[nt] neutHatPred;
vector<lower = 0>[nObsPK] cHatObsCond;
Expand Down
31 changes: 16 additions & 15 deletions example-models/chemical_reactions/chem.stan
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
functions{
real[] reaction(real t, real[] x, real[] p, real[] r, int[] i){
real dxdt[3];
vector reaction(real t, vector x, array[] real p, array[] real r,
array[] int i){
vector[3] dxdt;
real p1 = p[1];
real p2 = p[2];
real p3 = p[3];
Expand All @@ -13,19 +14,19 @@ functions{

data {
int<lower=1> nsub;
int<lower=1> len[nsub];
array[nsub] int<lower=1> len;
int<lower=1> ntot;
real ts[ntot];
real obs[ntot];
array[ntot] real ts;
array[ntot] real obs;
}

transformed data {
int i1[nsub];
int i2[nsub];
array[nsub] int i1;
array[nsub] int i2;
real t0 = 0.0;
real xr[0];
int xi[0];
real theta[3] = {0.04, 1.0e4, 3.0e7};
array[0] real xr;
array[0] int xi;
array[3] real theta = {0.04, 1.0e4, 3.0e7};
i1[1] = 1;
i2[1] = len[1];
for (i in 2:nsub) {
Expand All @@ -37,19 +38,19 @@ transformed data {
parameters {
/* p1=0.04, p2=1e4, and p3=3e7 */
real<lower = 0> y0_mu;
real<lower = 0> y0_1[nsub];
array[nsub] real<lower = 0> y0_1;
real<lower = 0> sigma;
}

transformed parameters {
real y0[3];
real x[ntot, 3];
real x3[ntot];
vector[3] y0;
array[ntot] vector[3] x;
array[ntot] real x3;
for (i in 1:nsub) {
y0[1] = y0_1[i];
y0[2] = 0.0;
y0[3] = 0.0;
x[i1[i]:i2[i], ] = pmx_integrate_ode_bdf(reaction, y0, t0, ts[i1[i]:i2[i]], theta, xr, xi, 1.e-4, 1.e-8, 10000);
x[i1[i]:i2[i], ] = pmx_ode_bdf_ctrl(reaction, y0, t0, ts[i1[i]:i2[i]], 1.e-4, 1.e-8, 10000, theta, xr, xi);
}
x3 = x[ , 3];
}
Expand Down
25 changes: 13 additions & 12 deletions example-models/chemical_reactions/chem_group.stan
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
functions{
real[] reaction(real t, real[] x, real[] p, real[] r, int[] i){
real dxdt[3];
array[] real reaction(real t, array[] real x, array[] real p, array[] real r,
array[] int i){
array[3] real dxdt;
real p1 = p[1];
real p2 = p[2];
real p3 = p[3];
Expand All @@ -13,19 +14,19 @@ functions{

data {
int<lower=1> nsub;
int<lower=1> len[nsub];
array[nsub] int<lower=1> len;
int<lower=1> ntot;
real ts[ntot];
real obs[ntot];
array[ntot] real ts;
array[ntot] real obs;
}

transformed data {
int i1[nsub];
int i2[nsub];
array[nsub] int i1;
array[nsub] int i2;
real t0 = 0.0;
real xr[0];
int xi[0];
real theta[3] = {0.04, 1.0e4, 3.0e7};
array[0] real xr;
array[0] int xi;
array[3] real theta = {0.04, 1.0e4, 3.0e7};
i1[1] = 1;
i2[1] = len[1];
for (i in 2:nsub) {
Expand All @@ -37,12 +38,12 @@ transformed data {
parameters {
/* p1=0.04, p2=1e4, and p3=3e7 */
real<lower = 0> y0_mu;
real<lower = 0> y0_1[nsub];
array[nsub] real<lower = 0> y0_1;
real<lower = 0> sigma;
}

transformed parameters {
real y0_pop[nsub, 3];
array[nsub, 3] real y0_pop;
matrix[3, ntot] x;
row_vector[ntot] x3;
for (i in 1:nsub) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
functions{
real[] reaction(real t, real[] x, real[] p, real[] r, int[] i){
real dxdt[3];
array[] real reaction(real t, array[] real x, array[] real p, array[] real r,
array[] int i){
array[3] real dxdt;
real p1 = p[1];
real p2 = p[2];
real p3 = p[3];
Expand All @@ -15,21 +16,21 @@ data {
int<lower=1> nsub;
int<lower=1> nsub1;
int<lower=1> nsub2;
int<lower=1> len[nsub];
array[nsub] int<lower=1> len;
int<lower=1> ntot;
real ts[ntot];
real obs[ntot];
array[ntot] real ts;
array[ntot] real obs;
}

transformed data {
int i1[nsub];
int i2[nsub];
array[nsub] int i1;
array[nsub] int i2;
real t0 = 0.0;
real xr[0];
int xi[0];
real theta1[3] = {0.04, 1.0e4, 3.0e7};
real theta2[3] = {0.04, 0.1, 0.1};
real theta[nsub, 3];
array[0] real xr;
array[0] int xi;
array[3] real theta1 = {0.04, 1.0e4, 3.0e7};
array[3] real theta2 = {0.04, 0.1, 0.1};
array[nsub, 3] real theta;
i1[1] = 1;
i2[1] = len[1];
for (i in 2:nsub) {
Expand All @@ -46,12 +47,12 @@ transformed data {

parameters {
real<lower = 0> y0_mu;
real<lower = 0> y0_1[nsub];
array[nsub] real<lower = 0> y0_1;
real<lower = 0> sigma;
}

transformed parameters {
real y0_pop[nsub, 3];
array[nsub, 3] real y0_pop;
matrix[3, ntot] x;
row_vector[ntot] x3;
for (i in 1:nsub) {
Expand Down
Loading

0 comments on commit 0168482

Please sign in to comment.