JuMP.jl 中的 Cox 比例风险
Cox Proportional Hazard in JuMP.jl
我第一次在 Julia 中尝试 JuMP.jl,似乎无法解决错误。这是我的设置。
使用 DataFrames、DataFramesMeta、JuMP、Ipopt
#time to event
times = [143,164,188,189,190,192,206,209,213,216,220,227,230,234,246,265,304,216,244,
142,156,163,198,205,232,232,233,233,233,233,239,240,261,280,280,296,296,232,204,344];
#make censored data
is_censored = zeros(Int32, 40);
is_censored[18]=1
is_censored[19]=1
is_censored[39]=1
is_censored[40]=1
#treatment vs control
x1=ones(Int32,19)
x2=zeros(Int32,21)
x=append!(x1,x2)
#build DataFrame
using DataFrames
df = DataFrame();
df[:times]=times;
df[:is_censored]= is_censored;
df[:x]=x;
df
#sort df
df_sorted = sort!(df, cols = [order(:times)])
#make df_risk and df_uncensored
df_uncensored = @where(df_sorted, :is_censored .== 0)
df_risk = df_sorted
#cox partial likelihood
#use JuMP
##convert df to array
uncensored = convert(Array,df_uncensored[:x])
risk_set = convert(Array,df_risk[:x])
m = Model(solver=IpoptSolver(print_level=0))
@variable(m, β, start = 0.0)
@NLobjective(m, Max, sum(uncensored[i]*β-log*sum(exp(risk_set[j]*β) for j=1:length(risk_set)) for i=1:length(uncensored)))
最后一行是我所有的问题所在
@NLobjective(m, Max, sum(uncensored[i]*β-log*sum(exp(risk_set[j]*β) for j=1:length(risk_set)) for i=1:length(uncensored)))
我收到错误
ERROR: MethodError: no method matching parseNLExpr_runtime(::JuMP.Model, ::Base.#log, ::Array{ReverseDiffSparse.NodeData,1}, ::Int64, ::Array{Float64,1})
Closest candidates are:
parseNLExpr_runtime(::JuMP.Model, ::Number, ::Any, ::Any, ::Any) at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:196
parseNLExpr_runtime(::JuMP.Model, ::JuMP.Variable, ::Any, ::Any, ::Any) at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:202
parseNLExpr_runtime(::JuMP.Model, ::JuMP.NonlinearExpression, ::Any, ::Any, ::Any) at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:208
...
Stacktrace:
[1] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/parseExpr_staged.jl:489 [inlined]
[2] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:226 [inlined]
[3] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/macros.jl:1086 [inlined]
[4] anonymous at ./<missing>:?
我一直在尝试使用的示例 maximum likelihood example and the optimal control
你可能打算写
log(sum(exp(risk_set[j]*β) for j=1:length(risk_set)))
而不是
log*sum(exp(risk_set[j]*β) for j=1:length(risk_set))
我第一次在 Julia 中尝试 JuMP.jl,似乎无法解决错误。这是我的设置。
使用 DataFrames、DataFramesMeta、JuMP、Ipopt
#time to event
times = [143,164,188,189,190,192,206,209,213,216,220,227,230,234,246,265,304,216,244,
142,156,163,198,205,232,232,233,233,233,233,239,240,261,280,280,296,296,232,204,344];
#make censored data
is_censored = zeros(Int32, 40);
is_censored[18]=1
is_censored[19]=1
is_censored[39]=1
is_censored[40]=1
#treatment vs control
x1=ones(Int32,19)
x2=zeros(Int32,21)
x=append!(x1,x2)
#build DataFrame
using DataFrames
df = DataFrame();
df[:times]=times;
df[:is_censored]= is_censored;
df[:x]=x;
df
#sort df
df_sorted = sort!(df, cols = [order(:times)])
#make df_risk and df_uncensored
df_uncensored = @where(df_sorted, :is_censored .== 0)
df_risk = df_sorted
#cox partial likelihood
#use JuMP
##convert df to array
uncensored = convert(Array,df_uncensored[:x])
risk_set = convert(Array,df_risk[:x])
m = Model(solver=IpoptSolver(print_level=0))
@variable(m, β, start = 0.0)
@NLobjective(m, Max, sum(uncensored[i]*β-log*sum(exp(risk_set[j]*β) for j=1:length(risk_set)) for i=1:length(uncensored)))
最后一行是我所有的问题所在
@NLobjective(m, Max, sum(uncensored[i]*β-log*sum(exp(risk_set[j]*β) for j=1:length(risk_set)) for i=1:length(uncensored)))
我收到错误
ERROR: MethodError: no method matching parseNLExpr_runtime(::JuMP.Model, ::Base.#log, ::Array{ReverseDiffSparse.NodeData,1}, ::Int64, ::Array{Float64,1})
Closest candidates are:
parseNLExpr_runtime(::JuMP.Model, ::Number, ::Any, ::Any, ::Any) at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:196
parseNLExpr_runtime(::JuMP.Model, ::JuMP.Variable, ::Any, ::Any, ::Any) at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:202
parseNLExpr_runtime(::JuMP.Model, ::JuMP.NonlinearExpression, ::Any, ::Any, ::Any) at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:208
...
Stacktrace:
[1] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/parseExpr_staged.jl:489 [inlined]
[2] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:226 [inlined]
[3] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/macros.jl:1086 [inlined]
[4] anonymous at ./<missing>:?
我一直在尝试使用的示例 maximum likelihood example and the optimal control
你可能打算写
log(sum(exp(risk_set[j]*β) for j=1:length(risk_set)))
而不是
log*sum(exp(risk_set[j]*β) for j=1:length(risk_set))