我在将方程转换为代码方面不是很有经验。我坚持将部分可能性的得分函数转换为julia中的代码,以便在JuMP中进行评估。
score function,当在0处解决β时,是最大值。
我制作了一个简单的小数据集。
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中β= -1.3261
如果上面的代码正常工作,我会在运行
之后得到相同的输出solve(m)
println("β = ", getvalue(β))
答案 0 :(得分:3)
问题中的代码有点复杂,但以下是尝试提取工作方法的相关部分:
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
β
与问题相同,所以我猜输入的调整是正确的,公式是对数似然。日志中表达式的开头使用了根据带有bool
的0/1值(-1)^bool
选择+1或-1符号的常用技巧。