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

Store only the covariance matrix in CoxModel, not the information #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 22 additions & 6 deletions src/cox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ end

# Structure of Cox regression output

struct CoxModel{T<:Real} <: RegressionModel
struct CoxModel{T<:Real,C<:Union{Cholesky{T},CholeskyPivoted{T}}} <: RegressionModel
aux::CoxAux{T}
β::Vector{T}
loglik::T
score::Vector{T}
fischer_info::Matrix{T}
vcov::Matrix{T}
chol::C
end

function StatsAPI.coeftable(obj::CoxModel)
Expand Down Expand Up @@ -119,7 +118,7 @@ StatsAPI.dof(obj::CoxModel) = length(coef(obj))

StatsAPI.dof_residual(obj::CoxModel) = nobs(obj) - dof(obj)

StatsAPI.vcov(obj::CoxModel) = obj.vcov
StatsAPI.vcov(obj::CoxModel) = inv(obj.chol)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point is that this one will error in the rank deficient case since

julia> inv(cholesky([0 0; 0 1], Val(true), check=false))
ERROR: SingularException(2)
Stacktrace:
 [1] chknonsingular
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/lapack.jl:49 [inlined]
 [2] potri!(uplo::Char, A::Matrix{Float64})
   @ LinearAlgebra.LAPACK /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/lapack.jl:3118
 [3] inv(C::CholeskyPivoted{Float64, Matrix{Float64}})
   @ LinearAlgebra /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:688
 [4] top-level scope
   @ REPL[150]:1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh okay yeah, I see. And in that case then there's no point in using a pivoted factorization since we primarily care about the inverse. 👍

Could still be worth checking for collinearity ahead of time though.


StatsAPI.stderror(obj::CoxModel) = sqrt.(diag(vcov(obj)))

Expand Down Expand Up @@ -193,6 +192,19 @@ function _cox_fgh!(β, grad, hes, c::CoxAux{T}) where T
return y
end

function _chol_me_maybe(A)
C = cholesky(A; check=false)
if issuccess(C)
return C
else
@static if VERSION >= v"1.8.0-DEV.1139"
return cholesky(A, RowMaximum())
else
return cholesky(A, Val(true))
end
end
end

_coxph(X::AbstractArray{<:Integer}, s::AbstractVector; tol, l2_cost) = _coxph(float(X), s; tol=tol, l2_cost=l2_cost)

function _coxph(X::AbstractArray{T}, s::AbstractVector; l2_cost, tol) where T
Expand All @@ -201,8 +213,12 @@ function _coxph(X::AbstractArray{T}, s::AbstractVector; l2_cost, tol) where T
β₀ = zeros(R, size(X, 2))
fgh! = TwiceDifferentiable(Optim.only_fgh!((f, G, H, x)->_cox_fgh!(x, G, H, c)), β₀)
res = optimize(fgh!, β₀, NewtonTrustRegion(), Optim.Options(g_tol = tol))
β, neg_ll, grad, hes = Optim.minimizer(res), Optim.minimum(res), Optim.gradient(fgh!), Optim.hessian(fgh!)
return CoxModel{R}(c, β, -neg_ll, -grad, hes, pinv(hes))
β = Optim.minimizer(res)
neg_ll = minimum(res)
grad = Optim.gradient(fgh!)
hes = Symmetric(Optim.hessian(fgh!))
chol = _chol_me_maybe(hes)
return CoxModel{R,typeof(chol)}(c, β, -neg_ll, -grad, chol)
end

StatsModels.drop_intercept(::Type{CoxModel}) = true
Expand Down
7 changes: 4 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ end
@test modelmatrix(outcome) == modelmatrix(outcome_without_formula)

@test sprint(show, outcome_without_formula) == """
CoxModel{Float64}
CoxModel{Float64, Cholesky{Float64, Matrix{Float64}}}

Coefficients:
──────────────────────────────────────────────
Expand Down Expand Up @@ -241,8 +241,9 @@ x7 0.0914971 0.0286485 3.19378 0.0014
@test dof(outcome) == 7
@test dof_residual(outcome) == 425
@test loglikelihood(outcome) > nullloglikelihood(outcome)
@test all(x->x > 0, eigen(outcome.model.fischer_info).values)
@test outcome.model.fischer_info * vcov(outcome) ≈ I atol=1e-10
fisher_info = Matrix(outcome.model.chol)
@test all(x->x > 0, eigen(fisher_info).values)
@test fisher_info * vcov(outcome) ≈ I atol=1e-10
@test norm(outcome.model.score) < 1e-5
@test hcat(outcome_coefmat.cols[1:3]...) ≈ expected_coefs[:,1:3] atol=1e-5

Expand Down