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

[Ft] give cox baseline #10

Open
rimhajal opened this issue Aug 19, 2024 · 7 comments
Open

[Ft] give cox baseline #10

rimhajal opened this issue Aug 19, 2024 · 7 comments

Comments

@rimhajal
Copy link
Member

rimhajal commented Aug 19, 2024

it would be nice to have a function that adds the baseline H_0 function. this is a rough draft of the breslow estimator for that end:

function _breslow_est(model::SurvivalModels.CoxModel, t)
    r = 0.0
    exp_pred = exp.(model.des * model.par)
    for ti in model.times
        if ti < t
            num = 0.0
            den = 0.0
            for j in eachindex(model.times)
                num += model.status[j] * (model.times[j]=ti)
                den += exp_pred[j] * (model.times[j]>=ti)
            end
            r += num / den
        end
    end
    return r
end

survival_curve(model::SurvivalModels.CoxModel, t, X) = exp(-_breslow_est(model, t) * exp(X' * model.par))
@lrnv
Copy link
Member

lrnv commented Aug 20, 2024

Thisone looks like it is doing the same thing but a bit faster:

function _breslow_est(model::SurvivalModels.CoxModel, t)
    r = 0.0
    us = unique(model.times)
    max_iu = searchsortedfirst(us, t)
    contribs = 1 ./ reverse(cumsum(reverse(exp.(model.des * model.par))))
    for i_u in 1:(max_iu-1)
        u = us[i_u]
        iiu = searchsortedfirst(model.times, u)
        count = sum(model.status[1:(iiu-1)])
        r += count * contribs[iiu]
    end
    return r
end

@rimhajal
Copy link
Member Author

It's still the wrong values no?

@lrnv
Copy link
Member

lrnv commented Aug 20, 2024

It is the same value, I do not know if it is right or wrong for the moment...

@lrnv
Copy link
Member

lrnv commented Aug 22, 2024

@rimhajal i do not remember if we had a correctly working verison at the end ? if so, can you copy it here ?

@rimhajal
Copy link
Member Author

the results were a lot better (when comparing the Cox survival curve to Kaplan Meier) but they didn't match the values in R i think.

function _breslow_est(model::SurvivalModels.CoxModel)
    t = unique(model.times)
    k = length(t)
    h = zeros(k)
    elp = exp.(model.des * model.par)
    for i in 1:k
        risk = elp .* (model.times .>= t[i])
        h[i] = sum(model.status .* (model.times .== t[i])) / sum(risk)
    end
    return cumsum(h)
end

@lrnv
Copy link
Member

lrnv commented Aug 22, 2024

Ha now i remember, this version is actually OK, TY (even if a bit slow i think). Do you also have the website link we used for future reference ? I do not remember it..

@rimhajal
Copy link
Member Author

you mean this one https://missingdatasolutions.rbind.io/2022/12/cox-baseline-hazard/ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants