I have adapted this excellent post by @aaowens to fit a right-censored AFT regression model, but for some reason the likelihood is not compatible with forwarddiff.
using Distributions,Optim
function AFT(pvec,data)
function l(theta)
α = exp(theta[1])
θ = exp(theta[2])
wdist = Weibull(α,θ)
-sum(logpdf(wdist,theta[3:end]' * d[3:end] - d[2])^(d[1]) + logccdf(wdist,theta[3:end]' * d[3:end] - d[2])^(1-d[1]) for d in data)
end
l(pvec)
optimize(l,pvec,BFGS(),autodiff = :forward)
end
a = 2.0
b = 2.0
n = 100
beta = [0.1,0.3,.8]
wb = Weibull(a,b)
X = rand(n,3)
delta = rand([0,1],n) # delta[i] = 1 iff Y[i] censored
Y = X * beta
for i in 1:n
if delta[i] == 1
Y[i] = Y[i] - Y[i]*rand()
end
end
paramInit = fill(0.1,5)
resp = AFT(paramInit, collect(eachrow([delta Y X])))
julia> resp = AFT(paramInit, collect(eachrow([delta Y X])))
* Status: failure (line search failed)
* Candidate solution
Final objective value: Inf
* Found with
Algorithm: BFGS
* Convergence measures
|x - x'| = 0.00e+00 ≤ 0.0e+00
|x - x'|/|x'| = 0.00e+00 ≤ 0.0e+00
|f(x) - f(x')| = NaN ≰ 0.0e+00
|f(x) - f(x')|/|f(x')| = NaN ≰ 0.0e+00
|g(x)| = 1.15e+02 ≰ 1.0e-08
* Work counters
Seconds run: 0 (vs limit Inf)
Iterations: 1
f(x) calls: 1
∇f(x) calls: 1