diff --git a/src/cox.jl b/src/cox.jl index 5573044..65c0e12 100644 --- a/src/cox.jl +++ b/src/cox.jl @@ -100,8 +100,12 @@ function StatsBase.coeftable(obj::CoxModel) ["Estimate", "Std.Error", "z value", "Pr(>|z|)"], ["x$i" for i in 1:length(β)], 4) end -function Base.show(io::IO, obj::CoxModel) - println(io, "$(typeof(obj)):\n\nCoefficients:\n", coeftable(obj)) +function Base.show(io::IO, model::CoxModel) + ct = coeftable(model) + println(io, "$(typeof(model))") + println(io) + println(io,"Coefficients:") + show(io, ct) end StatsBase.coef(obj::CoxModel) = obj.β diff --git a/test/rossi.csv b/test/data/rossi.csv similarity index 100% rename from test/rossi.csv rename to test/data/rossi.csv diff --git a/test/runtests.jl b/test/runtests.jl index f723b7f..eb0ecd9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -146,13 +146,32 @@ end end @testset "Cox" begin - filepath = joinpath(@__DIR__, "rossi.csv") + filepath = joinpath(@__DIR__, "data", "rossi.csv") rossi = CSV.read(filepath) rossi[:event] = EventTime.(rossi[:week],rossi[:arrest] .== 1) outcome = coxph(@formula(event ~ fin+age+race+wexp+mar+paro+prio), rossi; tol = 1e-8) outcome_coefmat = coeftable(outcome) + regressor_matrix = Array(rossi[[:fin, :age, :race, :wexp, :mar, :paro, :prio]]) + event_vector = rossi[:event] + + outcome_without_formula = coxph(regressor_matrix, event_vector) + + @test sprint(show, outcome_without_formula) == """ + Survival.CoxModel{Float64} + + Coefficients: + Estimate Std.Error z value Pr(>|z|) + x1 -0.379416 0.191379 -1.98253 0.0474 + x2 -0.0574299 0.0219988 -2.61059 0.0090 + x3 0.31392 0.307995 1.01924 0.3081 + x4 -0.14981 0.212226 -0.705898 0.4803 + x5 -0.433724 0.38187 -1.13579 0.2560 + x6 -0.0848615 0.195756 -0.433505 0.6646 + x7 0.091521 0.0286469 3.1948 0.0014 + """ + coef_matrix = ModelMatrix(ModelFrame(@formula(event ~ 0+fin+age+race+wexp+mar+paro+prio), rossi)).m outcome_from_matrix = coxph(coef_matrix, rossi[:event]; tol = 1e-8, l2_cost = 0) outcome_from_matrix32 = coxph(Float32.(coef_matrix), rossi[:event]; tol = 1e-5) @@ -220,4 +239,4 @@ end end @test_throws ErrorException Survival.newton_raphson(wrong_fgh!, [2.2]) -end \ No newline at end of file +end