In my example, suppose that there are some observations
y_1,y_2,\dots,y_n \sim \pi N(\mu_1,\sigma_1)+(1-\pi)N(\mu_2,\sigma_2).
However, in the real experiment, we observe some $y_i$s with right censoring at y=K.
Now I get the data y_i with its censoring indicator I_i and want to estimate parameters \mu and \sigma. Obviously, it is a mixture Normal problem with censoring data.
For a certain y_i and the censoring indicator I_i, which I_i=1 means right censoring,
y_i \sim \pi ~f(y_i|\mu_1,\sigma_1)^{1-I_i}(1-F(y_i|\mu_1,\sigma_1))^{I_i}+(1-\pi)~f(y_i|\mu_2,\sigma_2)^{1-I_i}(1-F(y_2|\mu_1,\sigma_2))^{I_i}.
So, according to the density function above, here is my stan code:
User-defined functions
Here is some user-defined functions, which mean that
{fun}_1=f(y_i|\mu_1,\sigma_1)^{1-I_i}(1-F(y_i|\mu_1,\sigma_1)),
{fun}_2=f(y_i|\mu_2,\sigma_2)^{1-I_i}(1-F(y_i|\mu_2,\sigma_2)),
and
fun1\_lpdf=\log(fun_1),fun2\_lpdf=\log(fun_2).
This 3 formulas define the p.d.f of two mixture right-censoring normal distributions.
functions {
real fun1_log(real y,real ind ,real mu1,vector sigma){
return (1-ind)*normal_lpdf(y|mu1,sigma[1])+ind*log(1-normal_cdf(y,mu1,sigma[1]));
}
real fun2_log(real y,real ind ,real mu2,vector sigma){
return (1-ind)*normal_lpdf(y|mu2,sigma[2])+ind*log(1-normal_cdf(y,mu2,sigma[2]));
}
}
Parameters and Data
parameters {
real mu1;
real mu2;
vector<lower=0>[2] sigma;
real<lower=0, upper=1> p;
}
data {
int<lower = 0> N;
vector[N] y;
vector[N] index;
}
Model
model {
mu1~normal(50,2);
mu2~normal(120,4);
sigma[1] ~ inv_gamma(0.01, 0.01);
sigma[2] ~ inv_gamma(0.01, 0.01);
p ~ beta(1, 1);
for (n in 1:N)
target += log_mix(p,
fun1_log(y[n] ,index[n]| mu1, sigma[1]),
fun2_log(y[n], index[n]| mu2, sigma[2]));
}
But when I compile the stan code, some ERROR appear:
PARSER EXPECTED: whitespace to end of file.
FOUND AT line 18:
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model '95bdbfeb015fa8187a5778b30cd86959' due to the above error.