Dear All,
I am trying to implement Bayesian Survival Analysis with Cox (proportional hazards) regression method in Julia based on the PyMC3 code written by Austin Rochford https://austinrochford.com/posts/2015-10-05-bayes-survival.html.
Here are my codes:
using RDatasets,DataFrames,Plots,StatsPlots,Turing,Distributions,FillArrays
HSAUR = dataset("HSAUR","mastectomy");
HSAUR[!,:CMetastized]=levelcode.(HSAUR[!,:Metastized]).-1;
interval_length = 3
interval_bounds =0:interval_length:maximum(HSAUR[!,:Time])+interval_length+1
n_intervals =length(interval_bounds)-1
n_patients=nrow(HSAUR)
patients=1:n_intervals
death =zeros(Int64, n_patients, n_intervals)
exposure =zeros(Int64, n_patients, n_intervals)
last_period =floor.(Int64,HSAUR[!,:Time] ./ interval_length);
I don’t know if Julia has a similar function like greater_equal.outer in NumPy, so I just simply write the following code to covert:
for i in 1:n_patients
death[i,last_period[i]]=HSAUR[i,:Event]
for j in 1:n_intervals
if HSAUR[i,:Time] >= interval_bounds[j]
exposure[i,j]=1*interval_length
else
exposure[i,j]=0
end
end
exposure[i,last_period[i]]=HSAUR[i,:Time]-interval_bounds[last_period[i]]
end
Turing model and sampling code:
@model function cox_regression(death,exposure,x)
λ0 ~Gamma(0.01, 0.01)
# σ ~Uniform(0.0, 10.0)
# τ = σ^2
# μβ ~ Normal(0.0, 100)
# β ~ Normal(μβ,τ)
β ~ Normal(0.0,100)
λ = exp.(β .* x).* fill(λ0,n_intervals)'
μ = exposure .*λ
for i in 1:length(death)
death[i] ~ Poisson(μ[i])
end
end
chain = sample(cox_regression(death,exposure,HSAUR[!,:CMetastized]), NUTS(0.65),1000)
I am new to Julia and Turing, I am not sure what is wrong that causes very long sampling time (seems never stop)… Could experts here advise me to solve the issue?
Thanks,
Ryan