Error in compileCode

Hello,

I am comparatively new to stan. I am running the following model and am getting an error message. I followed instructions for installation, checked that rtools works well (by running that example which returns 10), but still the same message.

Any suggestions on what could be causing this error?
Thanks,
Krina

Stan Model:

functions{
  real twoCptModelODE(real t,
                        real[] x,
                        real[] parms,
                        real[] rdata,
                        int[] idata){
    real ka = parms[1];
    real Kdeg = parms[2];
    real CL = rdata[1];
    real V1 = rdata[2];
    real V2 = rdata[3];
    real Q = rdata[4];
    
    real dose = rdata[5];
    real t_inf = rdata[6];
    
    real k10 =  CL/V1;
    real k12 = Q/V1;
    real k21 = Q/V2;
    
    real dxdt[4];
    
    if (t<=t_inf){
    dxdt[1] = (dose/t_inf) - ka*x[1] - Kdeg*x[1];
    }
    else {
    dxdt[1] = -ka*x[1] - Kdeg*x[1];  
    }
    dxdt[2] = ka*x[1] - (k10 + k12)* x[2] + k21*x[3];
    dxdt[3] = k12*x[2] - k21*x[3];
    dxdt[4] = dxdt[2]/V1;
    
    return dxdt[4];}

}

data{
  real CL;
  real V1;
  real V2;
  real Q;
  real dose;
  real t_inf;
  int nCmt;
  real y0[nCmt];                   //initial concentration in compartments
  real t_init;                    //initial
  int<lower = 1> nrow;
  int<lower = 1> nObsPK;
  real<lower = 0> TIME[nObsPK];
  vector<lower = 0>[nObsPK] DV;
}

transformed data{
  real rdata[13];
  int idata[0];
  vector[nObsPK] logCObs = log(DV);
}

parameters{
  real<lower = 0, upper = 50> ka;
  real<lower = 0, upper = 50> Kdeg;
  real<lower = 0> sigma;
  }

transformed parameters{
  real parms[2];
  
  parms[1] = ka; 
  parms[2] = Kdeg;
}

model{
  real x[nrow, nObsPK];
  ka ~ lognormal(0.3, 0.25);
  Kdeg ~ lognormal(0.3, 0.25);
  sigma ~ cauchy(0, 2);
  
  x = integrate_ode_bdf(twoCptModelODE, y0, t_init,
                        TIME, parms, rdata, idata, 
                        1e-10, 1e-10, 1e8);
                        
  for (j in 1:nObsPK){ 
   logCObs[j] ~ normal(log(x[j,2]), sigma);
  }
}

generated quantities{
}

RCode:

library(rstan)
library(Rcpp)
library(ggplot2)
library(dplyr)
library(parallel)

options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

## Specify the variables for which you want history and density plots
parametersToPlot <- c("parms", "sigma", "omega")
otherRVs <- c("cObsPred")
parameters <- c(parametersToPlot, otherRVs)
parametersToPlot <- c("lp__", parametersToPlot)

# get data file
xdata <- read.csv("Sims_Trastuzumab_Simple2.csv", header = T, stringsAsFactors = F)

#Create Initial Estimates
iObsPK <- with(xdata, (1:nrow(xdata))[!is.na(DV) & EVID == 0])
nObsPK <- length(iObsPK)
nt <- nrow(xdata)
nCmt <- 3

## create data set
data <- with(xdata,
             list(CL=0.111,
                  V1=2.91,
                  V2=3.06,
                  Q=0.445,
                  dose=600,
                  t_inf=0.0035,
                  nCmt=3,
                  y0=rep(0, times=3),
                  t_init=0.0,
                  nrow=nt,
                  nObsPK=nObsPK,
                  time=TIME[iObsPK],
                  DV = DV[iObsPK]
               ))

init <- function()
  list(kaHat = 0.5,
       KdegHat = 0.5,
       sigma = 1)

## run Stan
fit <- stan(file="Two_Comp_Linear3.stan", 
          data = data, pars = NULL, chains = 1,
         iter = 500, warmup = 250, thin = 1, 
         init = init, seed = 15142, 
         algorithm = "HMC", 
         cores = getOption("mc.cores", 4))

Error message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file1ae0481f11fb.cpp: In function ‘SEXPREC* file1ae0481f11fb(SEXP, SEXP)’:
file1ae0481f11fb.cpp:19:3: error: expected primary-expression before ‘return’

  • return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
    ^
    file1ae0481f11fb.cpp:21:55: error: wrong type argument to unary plus
    Rf_warning(“your C++ program does not return anything”);
    ^
    At global scope:
    cc1plus.exe: warning: unrecognized command line option "-Wno-ignored-attributes"
    make: *** [file1ae0481f11fb.o] Error 1
    Warning message:
    running command ‘make -f “C:/PROGRA~1/R/R-34~1.1/etc/x64/Makeconf” -f “C:/PROGRA~1/R/R-34~1.1/share/make/winshlib.mk” -f “C:/Users/kmehta/Documents/.R/Makevars” SHLIB_LDFLAGS=’$(SHLIB_CXXLDFLAGS)’ SHLIB_LD=’$(SHLIB_CXXLD)’ SHLIB=“file1ae0481f11fb.dll” WIN=64 TCLBIN=64 OBJECTS=“file1ae0481f11fb.o”’ had status 2
    In addition: Warning message:
    running command ‘C:/PROGRA~1/R/R-34~1.1/bin/x64/R CMD SHLIB file1ae0481f11fb.cpp 2> file1ae0481f11fb.cpp.err.txt’ had status 1

This is a Stan bug, but the user-defined function you are integrating must return a real[] rather than a real. So, change the signature and return dxdt, rather than dxdt[4].

Many thanks, Ben.

That got the stan model started. Now, I am getting the following message and initialization is failing. I am not sure what it refers to when it says continuous data[1] is nan. There are no negatives. I have also filtered out 0s from data.

SAMPLING FOR MODEL ‘Two_Comp_Linear3’ NOW (CHAIN 1).
Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: integrate_ode_bdf: continuous data[1] is nan, but must be finite! (in ‘model1b184c567430_Two_Comp_Linear3’ at line 78)

I also tried updating prior from lognormal distribution to normal and changing prior values slightly, but still same message.

thanks,
Krina

In the transformed data block, real rdata[13]; is declared but not defined. Hence its elements are NaN.

thank you, Ben. It works now.

I confirmed it’s a Stan issue. I’ll try to create a simpler version of the problem and file an issue. Thansk for the pointer, @bgoodri.

OK, here’s the issue:

https://github.com/stan-dev/stan/issues/2410

This is the minimal buggy example I came up with:

functions{
  real twoCptModelODE(real t,
                      real[] x,
                      real[] parms,
                      real[] rdata,
                      int[] idata){
    real dxdt[2];
    return dxdt[2];
  }
}
model {
  real x[2, 2]
    = integrate_ode_bdf(twoCptModelODE,
                        {1, 1.3}, 1.0, { 2.2, 3 }, { 1.0 }, { 1.0 }, { 2 },
                        10, 10, 10);
}

We’ll get this fixed for the next release. Thanks for reporting.