Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve numerical accuracy of confint(::KaplanMeier) #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/kaplanmeier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ function as a vector of tuples.
function StatsAPI.confint(km::KaplanMeier; level::Real=0.05)
q = quantile(Normal(), 1 - level/2)
return map(km.survival, km.stderr) do srv, se
l = log(-log(srv))
# The direct implementation here would be
# ℓ = log(-log(srv))
# a = q * se / log(srv)
# lower = exp(-exp(ℓ - a))
# upper = exp(-exp(ℓ + a))
# However, this has some issues with numerical accuracy. An approximation to
# this quantity with improved accuracy is implemented here. The approximation
# was obtained via Herbie (https://herbie.uwplse.org/).
a = q * se / log(srv)
exp(-exp(l - a)), exp(-exp(l + a))
return (srv^exp(-a), srv^exp(a))
end
end

Expand Down
5 changes: 3 additions & 2 deletions src/nelsonaalen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ function as a vector of tuples.
"""
function StatsAPI.confint(na::NelsonAalen; level::Real=0.05)
q = quantile(Normal(), 1 - level/2)
return map(na.chaz, na.stderr) do srv, se
srv - q * se, srv + q * se
return map(na.chaz, na.stderr) do ch, se
a = q * se
return (ch - a, ch + a)
end
end

Expand Down
Loading