Hi! Very new to Julia and having a great experience with it so far. I was messing around with censored models in Turing today and used the following approach, which seemed to work well for my toy example:
function sim_expon_censored(alpha, beta, censor_duration, N)
X = rand(Normal(0, 1), N)
θ = exp.(alpha .+ beta .* X)
lifetimes = rand.(Exponential.(θ))
is_censored = [lt < censor_duration ? false : true for lt in lifetimes]
censored_lifetimes = [ic==true ? censor_duration : lt for (ic, lt) in zip(is_censored, lifetimes)]
return DataFrame(x=X, y=censored_lifetimes, is_censored=is_censored)
end
@model censored_reg(y, X, is_censored) = begin
alpha ~ Normal(0, 10)
beta ~ Normal(0, 10)
N = length(y)
for i in 1:N
θ = exp(alpha + beta * X[i])
dist = Exponential(θ)
if is_censored[i] == true
pcensor = 1 - cdf(dist, y[i])
1 ~ Bernoulli(pcensor)
else
y[i] ~ dist
end
end
end
df = sim_expon_censored(2, 0.5, 4, 500)
model = censored_reg(df.y, df.x, df.is_censored)
chain = sample(model, NUTS(), 1000)