I’m trying to estimate a model in which the DGP involves integrating a custom function. When I try to compile the model (below), I get a Error in sink(type = "output") : invalid connection
error (full error message below). The issue is very clearly coming from the call to integrate_1d().
Sample model below. Anyone have an idea of what might be going wrong?
functions{
real cost_density(real x, // Function argument
real xc, // Complement of function argument
// on the domain (defined later)
real[] theta, // parameters
real[] x_r, // data (real)
int[] x_i) { // data (integer)
real mu = theta[1];
real sigma = theta[2];
real max_c = theta[3];
int num_bidders = x_i[1];
real out;
out = (num_bidders - 1)*(lognormal_lcdf(x | mu, sigma) - lognormal_lcdf(max_c | mu, sigma));
return(exp(out));
}
vector getGenerativeTotalAuctionHazard(int N,
int[] J, // num bidders in each auction
vector costs,
matrix X,
vector beta_costs,
real sigma,
real c_max
){
vector[N] mus;
vector[N] numerators;
vector[N] denominators;
vector[N] out;
mus = X * beta_costs;
// NOTE: This assumes homogeneous bidders so that g_j(b_i) = g(b_i | X_j) = g(b_i | X_i) = g_i(b_i)
for (n in 1:N){
numerators[n] = integrate_1d(cost_density,
0.0, // left limit
c_max, // right limit
{ mus[n], sigma, costs[n] }, //theta
{ 0.0 }, //x_r
{ J[n] }, //x_i
0.001
);
denominators[n] = exp(
(J[n] - 1)*(lognormal_lcdf(costs[n] | mus[n], sigma) - lognormal_lcdf(c_max | mus[n], sigma))
);
out[n] = numerators[n] / denominators[n];
}
return(out);
}
}
data {
int<lower=0> N; // Number of auction-bidder obs
int<lower=0> K; // number of auction-bidder feaures
int J[N]; // Number of bidders in each auction
matrix[N, K] X; // Auction-bidder features
vector[N] bids;
}
parameters {
vector[K] beta;
real<lower=0> sigma;
real<lower = 0> c_max;
vector[N] costs;
}
model {
vector[N] inv_hazards;
//prior
to_vector(beta) ~ normal(0,1);
sigma ~ normal(1,0.01);
c_max ~ normal(3, 1);
//likelihood
for(i in 1:N){
costs[i] ~ normal(X[i] * beta, sigma) T[,c_max]; //empirical bid distribution
}
//
inv_hazards = getGenerativeTotalAuctionHazard(
N,
J, // num bidders in each auction
costs,
X,
beta,
sigma,
c_max
);
(bids - costs) ~ normal(inv_hazards , 1);
}
Full error message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file3d89436904cd.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/rev/core/build_vari_array.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/l
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file3d89436904cd.cpp 2> file3d89436904cd.cpp.err.txt' had status 1
Error in sink(type = "output") : invalid connection