Compilation Error when running a model based on a user defined likelihood function

Hi,
I have defined my own log-likelihood function named as “aft_lpdf” in Stan, and I have tested the function by plugging in data and specific parameter values. The function returned the value I expected. So I think the function works well.

Then I would like to use Stan to estimate the parameters in my likelihood function. (The “aft_lpdf” function is a bit complicated, so I omitted the details)

function{
real aft_lpdf(…,…) {

}
}

data {
int spline_degree;
int num_knots;
int num_obs;
int num_cov;
real int_knots[num_knots-2];
matrix[num_obs,num_cov] Cov;
real Time[num_obs];
int delta[num_obs];
}

parameters{
vector[num_cov] P_Beta;
vector[num_knots + spline_degree - 1] P_Gamma;
}

model {
target+= aft_lpdf(Time|Cov,delta, spline_degree,num_knots,int_knots,P_Beta, P_Gamma);
}

I ran the Stan model in R using the command below:
aft_fit <- stan(file = ‘aft_likelihood.stan’,
data = list(Cov=Cov,Time=Time.obs,delta=delta,
spline_degree=2,num_knots=3,
int_knots=median(Time.obs)))

However, I got the following compilation error. Any suggestion on how to solve this?
Very much appreciated!

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file1927e10619.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resource
In addition: Warning message:
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file1927e10619.cpp 2> file1927e10619.cpp.err.txt’ had status 1

That doesn’t tell us what the compiler error actually is. Look for something that starts with Error:

Thanks Ben! Where should I go look for this? These are all the error messages I get; although there is a long log printed in the R console before the compilation error message, e.g.,
1077: …
1078: .method(“unconstrained_param_names”,
1079: &rstan::stan_fit<model19bd13211235_aft_likelihood_namespace::model19bd13211235_aft_likelihood, boost::random::ecuyer1988>::unconstrained_param_names)
1080: .method(“constrained_param_names”,
1081: &rstan::stan_fit<model19bd13211235_aft_likelihood_namespace::model19bd13211235_aft_likelihood, boost::random::ecuyer1988>::constrained_param_names)
1082: ;
1083: }
1084:
1085: // declarations
1086: extern “C” {
1087: SEXP file19bd60325f62( ) ;
1088: }
1089:
1090: // definition
1091:
1092: SEXP file19bd60325f62( ){
1093: return Rcpp::wrap(“aft_likelihood”);
1094: }

It should be the first thing after you call stan()

Thank you! I got “error: no matching function for call to ‘integrate_ode_rk45(…)”
I used this function in my likelihood function “aft_lpdf”, and my parameters are used as input arguments in the integrate function. Is this the reason why I got the compilation error?

Does Stan model allow parameters in ‘integrate_ode_rk45’ function?

Many thanks!

Could you include the entire error you get? Or your Stan program so we can reproduce the error you’re getting.

It should be telling you why if you don’t get a match.

Just for initial state and parameters, not for time or real data.

1 Like

Thank you Bob so much for trying to help me solving my problem.

Attached is the file that includes the entire error I got. I also spotted the following message which indicates that there is a failure in conversion the x_r argument in the integrate_ode_rk45 function: "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/arr/functor/integrate_ode_rk45.hpp:70:5: note: candidate function not viable: no known conversion from ‘const vectorstan::math::var’ to ‘const vector’ for 6th argument integrate_ode_rk45. "

error.txt (88.7 KB)

This is my first experience with Stan and I am eager to make my model work. I really appreciate your help!

It is at or near line 92 of your Stan program and has something to do with not adhering to the signature of integrate_ode_rk45 which must be

function ode, real[] initial_state, real initial_time, real[] times, real[] theta, real[] x_r, int[] x_i

Thank you a lot Ben for checking my error message! However, I still couldn’t figure out why this error occurs and how to fix it. Like I mentioned earlier, after I use expose_stan_functions() in R, my likelihood function works, but somehow the entire Stan program couldn’t be compiled.

I am attaching my Stan and R program here. Could you please take a bit time to check where I have made a mistake?
likelihood.stan (5.5 KB)
fit.R (1.0 KB)

Many many thanks!

I believe the problem is that you need the data keyword before real [] x_r in your function, as in

real ode_integ(real time,real[] theta, data real[] x_r, int[] x_i)

Howevs, when I do that I get a parser error:

Function argument error, function: ode_integ, argument: 3 must be data only, found expression containing a parameter varaible.
  error in 'model14bb1f56566b_aft_likelihood' at line 122, column 118
  -------------------------------------------------
   120:         log_haz_i[1]=log(Hazard(P_Beta, to_row_vector(Cov[i,:]), Time_obs[i], P_Gamma, spline_degree,num_knots,int_knots,Cov,to_vector(Time_obs)));
   121:         
   122:         loglik[i]=delta[i]*(Cov[i,:]*P_Beta+log_haz_i[1])-exp(Cov[i,:]*P_Beta)*ode_integ(Time_obs[i],theta, x_r, x_i);
                                                                                                                             ^
   123:       }
  -------------------------------------------------

which means that it cannot verify that x_r is an array of known values. The scalar type of things that are local to a function is either int or var. I think you are going to have to create x_r on the outside and pass it as an argument (which again will need the data keyword) to aft_lpdf.

1 Like

Thank you Ben very much for going through my program. As you can see that all the elements of x_r are indeed from data only. I tried to add the data keyword for all the data input in the aft_lpdf as well, and it still gave me the same parser error.

You suggested create x_r outside of aft_lpdf, however, I don’t know how it is feasible to do so in my likelihood function. I use the integrate function to calculate the log-likelihood of each individual(each row) of my data which consist of some elements of x_r, then I sum up the individual log-likelihood to get my overall likelihood for the entire data. Therefore, the individual specific element of x_r cannot be passed as an argument to the overall likelihood function aft_lpdf.

Do you have more advice for how to deal with the error in this situation?
Thanks a lot!

It may be easier to comment out your aft_likelihood function (now that it is working) and implement the log-likelihood as a loop in the model block. It looks as if x_r is basically just Cov, in which case you could construct it in the transformed data block.

Hi Ben, many thanks for such a quick reply.

In addition to Cov (the entire covariate matrix), x_r is consisted of int_knots, Time_obs, and Cov[i,] (the ith row of Cov (i=1,…,number of rows in Cov) ). Cov[i,] is individual specific, and it is essential to calculate the individual likelihood.

If x_r only includes Cov, int_knots, and Time_obs, then I can imagine that creating x_r on the outside of aft_lpdf could work. But, I think it is mainly Cov[i,] that causes the difficulty here.

For your second solution, there is no problem of constructing x_r as transformed data based on Cov, int_knots, and Time_obs, but how could I incorporate Cov[i,] in it as well?

Do you have further thoughts?
Merci Beaucoup

You just need an array that is N by whatever length x_r is supposed to be. Fill all that in transformed data and then pass the i-th element of that to integrate_ode_rk45.

1 Like

Thanks a million, Ben. It finally worked!
I really appreciated all your help and advice.
Thanks for creating such a friendly and helpful community for Stan users.