Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
Cleaning up docstrings

docs

lsurvresp issue

jackknife for KM

jackknife for AJ
  • Loading branch information
alexpkeil1 committed Sep 14, 2023
1 parent 32ee4ea commit cb23a04
Show file tree
Hide file tree
Showing 9 changed files with 1,229 additions and 954 deletions.
1 change: 1 addition & 0 deletions src/LSurvival.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import StatsBase:
#predict, predict!,
response,
score,
var,
vcov,
weights

Expand Down
80 changes: 18 additions & 62 deletions src/bootstrap.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@

"""
```
id, int, outt, data =
LSurvival.dgm(MersenneTwister(1212), 20, 5; afun = LSurvival.int_0)
d, X = data[:, 4], data[:, 1:3]
weights = rand(length(d))
# survival outcome:
R = LSurvivalResp(int, outt, d, ID.(id)) # specification with ID only
```
$DOC_BOOTSTRAP_SURVRESP
"""
function bootstrap(rng::MersenneTwister, R::T) where {T<:LSurvivalResp}
uid = unique(R.id)
Expand All @@ -24,16 +14,9 @@ end
bootstrap(R::T) where {T<:LSurvivalResp} = bootstrap(MersenneTwister(), R::T)



"""
```
z,x,t,d,event,weights =
LSurvival.dgm_comprisk(MersenneTwister(1212), 300)
enter = zeros(length(event))
# survival outcome:
R = LSurvivalCompResp(enter, t, event, weights, ID.(collect(1:length(t)))) # specification with ID only
bootstrap(R) # note that entire observations/clusters identified by id are kept
```
$DOC_BOOTSTRAP_SURVCOMPRESP
"""
function bootstrap(rng::MersenneTwister, R::T) where {T<:LSurvivalCompResp}
uid = unique(R.id)
Expand All @@ -47,27 +30,9 @@ function bootstrap(rng::MersenneTwister, R::T) where {T<:LSurvivalCompResp}
end
bootstrap( R::T) where {T<:LSurvivalCompResp} = bootstrap(MersenneTwister(), R::T)

"""
```
using LSurvival, Random
id, int, outt, data =
LSurvival.dgm(MersenneTwister(1212), 20, 5; afun = LSurvival.int_0)
d, X = data[:, 4], data[:, 1:3]
weights = rand(length(d))
# survival outcome:
R = LSurvivalResp(int, outt, d, ID.(id)) # specification with ID only
P = PHParms(X)
idx, R2 = bootstrap(R)
P2 = bootstrap(idx, P)
Mod = PHModel(R2, P2)
LSurvival._fit!(Mod, start=Mod.P._B)
```

"""
$DOC_BOOTSTRAP_PHPARMS
"""
function bootstrap(idx::Vector{Int}, P::PHParms)
P2 = PHParms(P.X[idx, :])
Expand Down Expand Up @@ -118,6 +83,19 @@ function bootstrap(rng::MersenneTwister, m::M;kwargs...) where{M<:KMSurv}
end
bootstrap(m::M;kwargs...) where{M<:KMSurv} = bootstrap(MersenneTwister(), m;kwargs...)

function bootstrap(rng::MersenneTwister, m::M, iter::Int;kwargs...) where{M<:KMSurv}
if isnothing(m.R)
throw("Model is missing response matrix, use keepy=true for original fit")
end
res = zeros(iter, 1)
@inbounds for i = 1:iter
mb = bootstrap(rng, m)
LSurvival._fit!(mb; kwargs...)
res[i, :] = mb.surv[end:end]
end
res
end
bootstrap(m::M, iter::Int; kwargs...) where{M<:KMSurv} = bootstrap(MersenneTwister(), m, iter::Int;kwargs...)



Expand All @@ -132,10 +110,6 @@ function bootstrap(rng::MersenneTwister, m::M;kwargs...) where{M<:AJSurv}
end
bootstrap(m::M; kwargs...) where{M<:AJSurv} = bootstrap(MersenneTwister(), m;kwargs...)


"""
$DOC_BOOTSTRAP_AJSURV
"""
function bootstrap(rng::MersenneTwister, m::M, iter::Int;kwargs...) where{M<:AJSurv}
if isnothing(m.R)
throw("Model is missing response matrix, use keepy=true for original fit")
Expand All @@ -150,21 +124,3 @@ function bootstrap(rng::MersenneTwister, m::M, iter::Int;kwargs...) where{M<:AJS
end
bootstrap(m::M, iter::Int; kwargs...) where{M<:AJSurv} = bootstrap(MersenneTwister(), m, iter::Int;kwargs...)



"""
$DOC_BOOTSTRAP_KMSURV
"""
function bootstrap(rng::MersenneTwister, m::M, iter::Int;kwargs...) where{M<:KMSurv}
if isnothing(m.R)
throw("Model is missing response matrix, use keepy=true for original fit")
end
res = zeros(iter, 1)
@inbounds for i = 1:iter
mb = bootstrap(rng, m)
LSurvival._fit!(mb; kwargs...)
res[i, :] = mb.surv[end:end]
end
res
end
bootstrap(m::M, iter::Int; kwargs...) where{M<:KMSurv} = bootstrap(MersenneTwister(), m, iter::Int;kwargs...)
28 changes: 2 additions & 26 deletions src/coxmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ mutable struct PHModel{G<:LSurvivalResp,L<:AbstractLSurvivalParms} <: AbstractPH
RL::Union{Nothing,Vector{Matrix{Float64}}} # residual matrix
end

"""
$DOC_PHMODEL
"""
function PHModel(
R::Union{Nothing,G},
P::L,
Expand All @@ -87,9 +84,6 @@ function PHModel(
return PHModel(R, P, formula, ties, fit, bh, nothing)
end

"""
$DOC_PHMODEL
"""
function PHModel(
R::Union{Nothing,G},
P::L,
Expand All @@ -100,9 +94,6 @@ function PHModel(
return PHModel(R, P, formula, ties, fit, zeros(Float64, length(R.eventtimes), 6))
end

"""
$DOC_PHMODEL
"""
function PHModel(
R::Union{Nothing,G},
P::L,
Expand All @@ -112,9 +103,6 @@ function PHModel(
return PHModel(R, P, formula, ties, false)
end

"""
$DOC_PHMODEL
"""
function PHModel(
R::Union{Nothing,G},
P::L,
Expand All @@ -123,9 +111,6 @@ function PHModel(
return PHModel(R, P, nothing, ties, false)
end

"""
$DOC_PHMODEL
"""
function PHModel(
R::Union{Nothing,G},
P::L,
Expand All @@ -134,9 +119,6 @@ function PHModel(
return PHModel(R, P, formula, "efron", false)
end

"""
$DOC_PHMODEL
"""
function PHModel(R::Union{Nothing,G}, P::L) where {G<:LSurvivalResp,L<:AbstractLSurvivalParms}
return PHModel(R, P, "efron")
end
Expand All @@ -155,9 +137,6 @@ mutable struct PHSurv{G<:Array{T} where {T<:PHModel}} <: AbstractNPSurv
fit::Bool
end

"""
$DOC_PHSURV
"""
function PHSurv(fitlist::Array{T}, eventtypes) where {T<:PHModel}
bhlist = [ft.bh for ft in fitlist]
bhlist = [hcat(bh, fill(eventtypes[i], size(bh, 1))) for (i, bh) in enumerate(bhlist)]
Expand All @@ -171,9 +150,6 @@ function PHSurv(fitlist::Array{T}, eventtypes) where {T<:PHModel}
PHSurv(fitlist, eventtypes, times, surv, risk, bh[:, 1], event, false)
end

"""
$DOC_PHSURV
"""
function PHSurv(fitlist::Array{T}) where {T<:PHModel}
eventtypes = collect(eachindex(fitlist))
PHSurv(fitlist, eventtypes)
Expand Down Expand Up @@ -827,7 +803,7 @@ end
"""
$DOC_FIT_PHSURV
"""
function fit(::Type{M}, fitlist::Vector{<:T}, ; fitargs...) where {M<:PHSurv,T<:PHModel}
function fit(::Type{M}, fitlist::Vector{T}, ; fitargs...) where {M<:PHSurv,T<:PHModel}

res = M(fitlist)

Expand All @@ -837,7 +813,7 @@ end
"""
$DOC_FIT_PHSURV
"""
risk_from_coxphmodels(fitlist::Array{T}, args...; kwargs...) where {T<:PHModel} =
risk_from_coxphmodels(fitlist::Vector{T}, args...; kwargs...) where {T<:PHModel} =
fit(PHSurv, fitlist, args...; kwargs...)


Expand Down
8 changes: 4 additions & 4 deletions src/data_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
"""
$DOC_DGM
"""
function dgm(rng, n, maxT; afun = int_0, yfun = yprob, lfun = lprob)
function dgm(rng::MersenneTwister, n::Int, maxT::Int; afun = int_0, yfun = yprob, lfun = lprob)
V = rand(rng, n)
LAY = Array{Float64,2}(undef, n * maxT, 4)
keep = ones(Bool, n * maxT)
Expand All @@ -43,13 +43,13 @@ function dgm(rng, n, maxT; afun = int_0, yfun = yprob, lfun = lprob)
end
id[findall(keep)], time[findall(keep)] .- 1, time[findall(keep)], LAY[findall(keep), :]
end
dgm(n, maxT; kwargs...) = dgm(MersenneTwister(), n, maxT; kwargs...)
dgm(n::Int, maxT::Int; kwargs...) = dgm(MersenneTwister(), n::Int, maxT::Int; kwargs...)


"""
$DOC_DGM_COMPRISK
"""
function dgm_comprisk(rng, n)
function dgm_comprisk(rng::MersenneTwister, n::Int)
z = rand(rng, n) .* 5
x = rand(rng, n) .* 5
dt1 = Weibull.(fill(0.75, n), inv.(exp.(-x .- z)))
Expand All @@ -72,4 +72,4 @@ function dgm_comprisk(rng, n)
event,
round.(weights, digits = 4)
end
dgm_comprisk(n) = dgm_comprisk(MersenneTwister(), n)
dgm_comprisk(n) = dgm_comprisk(MersenneTwister(), n)
Loading

0 comments on commit cb23a04

Please sign in to comment.