JuMP中Cox比例风险的评分函数

Score Function of Cox proportional hazard in JuMP

我在将方程式转换为代码方面不是很有经验。我一直坚持将部分似然的得分函数转换为 julia 中的代码,以便在 JuMP 中进行评估。

score function,当求解 0 处的 beta 时,它是最大值。

我做了一个简单的小数据集。

Using DataFrames, DataFramesMeta, JuMP, Ipopt
#build DataFrame
times = [6,7,10,15,19,25]
is_censored = [1,0,1,1,0,1]
x= is_control = [1,1,0,1,0,0]

m = Model(solver=IpoptSolver(print_level=0))
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

#use JuMP

##convert df to array

uncensored = convert(Array,df_uncensored[:x])
risk_set = convert(Array,df_risk[:x])
risk_index = convert(Array,find(is_censored .== 0))
x = convert(Array, x)
@variable(m, β, start = 0.0)

# score
@NLobjective(m, Max, sum(uncensored[i] - (([sum(exp(risk_set[j]*β)*x[j]) for j = risk_index[i]:length(risk_set)]) / ([(sum(exp(risk_set[j]*β)*x[j])) for j=risk_index[i]:length(risk_set)])) for i = 1:length(uncensored)))

我收到的错误是

ERROR: exp is not defined for type AffExpr. Are you trying to build a nonlinear problem? Make sure you use @NLconstraint/@NLobjective.
Stacktrace:
 [1] exp(::JuMP.GenericAffExpr{Float64,JuMP.Variable}) at /home/icarus/.julia/v0.6/JuMP/src/operators.jl:630
 [2] collect(::Base.Generator{UnitRange{Int64},##58#60}) at ./array.jl:418
 [3] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/parseExpr_staged.jl:489 [inlined]
 [4] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/parsenlp.jl:226 [inlined]
 [5] macro expansion at /home/icarus/.julia/v0.6/JuMP/src/macros.jl:1086 [inlined]
 [6] anonymous at ./<missing>:?

错误说指数是问题所在,但我之前做了其中有指数的对数似然并且没有错误。

在 R 中,beta = -1.3261

如果上面的代码有效,我希望在 运行

之后得到相同的输出
solve(m)

println("β = ", getvalue(β))

问题中的代码有点绕,下面是尝试提取相关部分的一个工作方法:

using JuMP, Ipopt

times = [6,7,10,15,19,25];
is_censored = 1-[1,0,1,1,0,1];
is_control = 1-[1,1,0,1,0,0];

uncensored = find(is_censored .== 0)

println("times = $times")
println("is_censored = $is_censored")
println("is_control = $is_control")

m = Model(solver=IpoptSolver(print_level=0))
@variable(m, β, start = 0.0)
@NLobjective(m, Max, sum(log(1+(-1)^is_control[uncensored[i]]* 
  sum((-1)^is_control[j]*exp(is_control[j]*β) for j=uncensored[i]:length(times))/
  sum( exp(is_control[j]*β) for j=uncensored[i]:length(times)))
     for i=1:length(uncensored)))

solve(m)
println("β = ", getvalue(β))

这输出:

times = [6,7,10,15,19,25]
is_censored = [0,1,0,0,1,0]
is_control = [0,0,1,0,1,1]

β = -1.3261290591982942

β 与问题中的相同,所以我猜对输入的调整是正确的,公式是对数似然。日志中表达式的开头使用了一个常见的技巧,即根据 0/1 值 bool(-1)^bool.

选择 +1 或 -1 符号