@@ -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
405412end
406413
407414function predict (m:: M ) where {M<: AbstractPH }
408- predict (m, m. P. X)
415+ predict (m, m. P. X, m . R . enter, m . R . exit )
409416end
410417
411418
0 commit comments