Hi

I have an external C++ file and want to read this in the function block. This file contains several functions with a list of returns.

The header of this file is #include <Rcpp.h>. I would be thankful if you can help me in this case.

Thanks. I have done the same as this vignette. but when I want to call my function in C++ file I face with this error:

stan_model(model_code = st, model_name = âexternalâ, allow_undefined = TRUE,

includes = paste0(â\n#include ââ,

file.path(getwd(), âforward_sim.hppâ), 'â\nâ))

PARSER EXPECTED: whitespace to end of file.

FOUND AT line 1:

stan-sim.stan

Error in stanc(file = file, model_code = model_code, model_name = model_name, :

failed to parse Stan model âexternalâ due to the above error.

What happens when you do `stanc(file)`

?

the same error:

try(readLines(stanc(model_code = st, allow_undefined = TRUE)$cppcode))

PARSER EXPECTED: whitespace to end of file.

FOUND AT line 1:

stan-sim.stan

Error in stanc(model_code = st, allow_undefined = TRUE) :

failed to parse Stan model âstan-sim.stanâ due to the above error.

Well, the `stan-sim.stan`

file you wrote is not valid and is not close enough to being valid that the parser can tell you what is wrong with it. Can you post that code?

write("// stan-sim

functions{

real forward_sim(real drug, real T, real Hbss, real Hbcirc, real Hbrho, real Emax, real x50, real PMQs, real rhomax, real MCHb, int kk, real BV){

real T_E_star;

int k;

real Hb_steady_state;

real Hb_50_circ;

real Hb_50_rho;

real E_max;

real x_50;

real PMQ_slope;

real rho_max;

real MeanCellHb;

real BloodVolume;

real drug_regimen;

real out[9];

```
T_E_star=T;
k=kk;
Hb_steady_state=Hbss;
Hb_50_circ=Hbcirc;
Hb_50_rho=Hbrho;
E_max=Emax;
x_50=x50;
PMQ_slope=PMQs;
rho_max=rhomax;
MeanCellHb=MCHb;
BloodVolume=BV;
drug_regimen=drug;
```

out = forward_sim(drug_regimen = drug_regimen, rho_max = rho_max,

Hb_steady_state = Hb_star,Hb_50_rho = Hb_50_rho,

Hb_50_circ = Hb_50_circ, k = k,

E_max = E_max,x_50 = x_50,

T_E_star = T_E_star,PMQ_slope = PMQ_slope,

MeanCellHb = MeanCellHb,BloodVolume = BloodVolume);

return out;

}

}

data {

real T_E_star;

int k;

int nhour;

real Hb_steady_state;

real Hb_50_circ;

real Hb_50_rho;

real E_max;

real x_50;

real PMQ_slope;

real rho_max;

real MeanCellHb;

real BloodVolume;

real drug_regimen[nhour];

}

transformed data {}

model {}

generated quantities {

real D;

real out1[nhour,1];

real out2[nhour,1];

real Hb[nhour,1];

real retic_per[nhour,1];

D = f(drug_regimen = pars[âdrug_regimenâ] , T_E_star = pars[âT_E_starâ], rho_max = pars[ârho_maxâ],

BloodVolume = pars[âBloodVolumeâ],

MeanCellHb = pars[âMeanCellHbâ],

Hb_50_circ = pars[âHb_50_circâ],

Hb_50_rho = pars[âHb_50_rhoâ],

E_max = pars[âE_maxâ],

x_50 = pars[âx_50â],

PMQ_slope = pars[âPMQ_slopeâ],

Hb_steady_state = pars[âHb_steady_stateâ]);

out1 = lognormal_rng(log(out$Hb) , 1);

out2 = lognormal_rng(log(out$retic_percent) , 1);

}

", âstan-sim.stanâ)

There are several problems here. First, if you are going to have the `forward_sim`

function call the `forward_sim`

function recursively, then you need to forward declare it before you define it by putting

```
real forward_sim(real drug, real T, real Hbss, real Hbcirc, real Hbrho, real Emax, real x50, real PMQs, real rhomax, real MCHb, int kk, real BV);
```

at the top of the functions block. Second, you cannot call function arguments by name in the Stan language, so all the

```
out = forward_sim(drug_regimen = drug_regimen, ...
```

need to remove things like `drug_regimen =`

so that the `forward_sim`

function is called with arguments by position. Third, the `Hb_star`

symbol is not defined. Finally, it would be much easier for you find your own syntax errors if your code were indented properly.

Suppose I have the attached functions file, and I want to get the results in generated quantities block. How can I define and call it?

drug_regimen = array(sapply(c(7.5,15,22.5,30,0,0),rep,24*5))/60
pars = (list(drug_regimen = drug_regimen ,
T_E_star = as.integer(115*24),

nhour=720,

rho_max=5,

Hb_steady_state=14.5,

Hb_50_rho=10,

Hb_50_circ=10,

k = 10^(-6),

E_max = 0.3,

PMQ_slope = 3,

x_50 = 15/60,

MeanCellHb = 30,

BloodVolume = 5 ))

mc <-

â

functions{

real forward_sim(real[] drug_regimen,

real rho_max,

real Hb_steady_state,

real Hb_50_rho,

real Hb_50_circ,

real k ,

real E_max ,

real PMQ_slope ,

real x_50 ,

real T_E_star ,

real MeanCellHb,

real BloodVolume);

}

data {

real T_E_star;

int k;

int nhour;

real Hb_steady_state;

real Hb_50_circ;

real Hb_50_rho;

real E_max;

real x_50;

real PMQ_slope;

real rho_max;

real MeanCellHb;

real BloodVolume;

real drug_regimen[nhour];

}

generated quantities {

real D;

real out1[nhour,1];

real out2[nhour,1];

D = forward_sim(drug_regimen, rho_max ,Hb_steady_state,Hb_50_rho,Hb_50_circ,k, E_max ,PMQ_slope ,

x_50 , T_E_star, MeanCellHb , BloodVolume);

out1 = lognormal_rng(log(D[,1]) , 1);

out2 = lognormal_rng(log(D[,2]) , 1);

}

â

Iâm not seeing the external C++ file.