data {
int<lower=0> N; // number of observations
vector[N] x; // predictor variables
vector[N] y; // response variable
}
parameters {
real mu; // intercept
real<lower=0> sigma; // residual standard deviation
vector[N] f; // function values at observed x
}
model {
f ~ bart_custom_lpdf(0, 0, 0.5, 2, x); // BART prior
y ~ normal(f, sigma); // likelihood
sigma ~ gamma(0.5, 0.5); // gamma prior on sigma
}
generated quantities {
vector[N] y_pred; // predicted values
for (i in 1:N) {
y_pred[i] = normal_rng(f[i], sigma);
}
}
functions {
real bart_custom_lpdf(vector f, real a, real b, real tau, vector x) {
// Define custom probability function for bart_custom
// f: function values at observed x
// a: alpha
// b: beta
// tau: tau
// x: predictor variables
int num_trees = 50; // number of trees
int N = size(x);
matrix[N, num_trees] trees;
vector[num_trees] weights;
vector[N] mu;
real lp = 0;
for (j in 1:num_trees) {
for (i in 1:N) {
trees[i, j] = normal_rng(0, 1);
}
weights[j] = normal_rng(0, sqrt(tau));
}
mu = x * trees * weights;
lp += normal_lpdf(f | mu, a);
lp += normal_lpdf(weights | 0, sqrt(b));
lp += normal_lpdf(trees | 0, 1);
return lp;
}
}
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:
vector ~ bart_custom(int, int, real, int, vector)
Distribution bart_custom not found. Require function with _lpdf or _lpmf or _log suffix
Real return type required for probability function.
error in ‘modelb2b47f9297ad_BART_function’ at line 14, column 40
12:
13: model {
14: f ~ bart_custom_lpdf(0, 0, 0.5, 2, x); // BART prior
^
15: y ~ normal(f, sigma); // likelihood
Error in stanc(filename, allow_undefined = TRUE) :
failed to parse Stan model ‘BART function’ due to the above error.