Skip to content

Commit ae307c2

Browse files
committed
Bug in predict
1 parent 765104d commit ae307c2

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LSurvival"
22
uuid = "edbbf5c1-874b-40a5-8c9f-d8fc918aacaa"
33
authors = ["Alex Keil"]
4-
version = "1.6.0"
4+
version = "1.6.1"
55

66
[deps]
77
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

src/coxmodel.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ coxph(f::FormulaTerm, data; kwargs...) = fit(PHModel, f, data; kwargs...)
377377

378378
# x = m.P.X
379379
"""
380+
Predicted probability of an outcome within a discrete window of time (absent competing risks)
381+
382+
using LSurvival
380383
dat1clust= (
381384
id = [1,2,3,3,4,4,5,5,6,6],
382385
enter = [0,0,0,1,0,1,0,1,0,1],
@@ -387,25 +390,29 @@ coxph(f::FormulaTerm, data; kwargs...) = fit(PHModel, f, data; kwargs...)
387390
388391
# use the `id` parameter with the ID struct
389392
ft2 = coxph(@formula(Surv(enter, exit, status) ~ x),dat1clust, id=ID.(dat1clust.id))
390-
hcat(dat1clust.id, dat1clust.enter, dat1clust.exit, dat1clust.x, predict(ft2))
393+
predict(ft2)
394+
predict(ft2, [1,1,1,1,0,0][:,:], [0,5,0,0,0,1], [5,8,8,9,9,9])
395+
396+
391397
"""
392-
function predict(m::M, x::X) where {M<:AbstractPH, X<:AbstractArray}
398+
function predict(m::M, x::X, entertime::V, exittime::V2) where {M<:AbstractPH, X<:AbstractArray, V<:AbstractVector, V2<:AbstractVector}
393399
basehaz = m.bh[:,1]
394400
bhtime = m.bh[:,4]
395401
lnhrs = coef(m)
396-
ex = hcat(m.R.enter, m.R.exit)
402+
#ex = hcat(m.R.enter, m.R.exit)
403+
ex = hcat(entertime, exittime)
397404
nobs= size(ex,1)
398405
p = zeros(nobs)
399406
for obs in 1:nobs
400407
bhidx = findall(ex[obs,1] .< bhtime .<= ex[obs,2])
401-
hazobs = basehaz[bhidx] .* exp.(x[bhidx,:] * lnhrs)
408+
hazobs = basehaz[bhidx] .* exp.(x[obs:obs,:] * lnhrs)
402409
p[obs] = 1-exp(-sum(hazobs))
403410
end
404411
p
405412
end
406413

407414
function predict(m::M) where {M<:AbstractPH}
408-
predict(m, m.P.X)
415+
predict(m, m.P.X, m.R.enter, m.R.exit)
409416
end
410417

411418

0 commit comments

Comments
 (0)