简体   繁体   English

Optimize/Optimize() 非线性 function 用于序数逻辑回归 Function 在 R

[英]Optimise/Optimize() nonlinear function for Ordinal Logistic Regression Function in R

I want to maximize this nonlinear function, but I cannot figure out what I am doing incorrectly.我想最大化这个非线性 function,但我不知道我做错了什么。 Here is the code:这是代码:

max.fun<-function(y0,y1,y2,pi0,pi1,pi2){y0*log(pi0)+y1*log(pi1)+y2*log(pi2)} 

th = matrix(c(-1,-3,1),nrow=1,ncol = 3)
y0 = matrix(c(26,104),nrow=1,ncol = 2)
y1 = matrix(c(33,72),nrow=1,ncol = 2)
y2 = matrix(c(22,31),nrow=1,ncol = 2)
race = matrix(c(1,0),nrow=1,ncol = 2)

alpha1<-th[1]
alpha2<-th[2]
beta<-th[3]

ex1<-exp(alpha1+beta*race)
ex2<-exp(alpha2+beta*race)

ee1<-ex1/(1+ex1)
ee2<-ex2/(1+ex2)

pi0<-1/(1+ex1)
pi1<-ee1-ee2
pi2<-ee2


optimize(max.fun,
         c(0,1),
         y0=y0,
         y1=y1,
         y2=y2,
         pi0=pi0,
         pi1=pi1,
         pi2=pi2,
         tol = 0.0001)

When I run the optimize function, I get this error message:当我运行优化 function 时,我收到以下错误消息:

Error in f(arg, ...) : unused argument (arg)

Any help is greatly appreciated.任何帮助是极大的赞赏。

What are you trying to optimize?你想优化什么? If x then it needs to be on LHS & other values must be provided- such as if you want to maximize y0:如果 x 则它需要在 LHS 上,并且必须提供其他值 - 例如,如果您想最大化 y0:

max.fun<-function(y0,y1,y2,pi0,pi1,pi2){y0*log(pi0)+y1*log(pi1)+y2*log(pi2)} 

th = matrix(c(-1,-3,1),nrow=1,ncol = 3)
y0 = matrix(c(26,104),nrow=1,ncol = 2)
y1 = matrix(c(33,72),nrow=1,ncol = 2)
y2 = matrix(c(22,31),nrow=1,ncol = 2)
race = matrix(c(1,0),nrow=1,ncol = 2)

alpha1<-th[1]
alpha2<-th[2]
beta<-th[3]

ex1<-exp(alpha1+beta*race)
ex2<-exp(alpha2+beta*race)

ee1<-ex1/(1+ex1)
ee2<-ex2/(1+ex2)

pi0<-1/(1+ex1)
pi1<-ee1-ee2
pi2<-ee2

# Using purely illustrative values
y0m <- optimize(max.fun,
         c(0,1),
         y1=2,
         y2=3,
         pi0=4,
         pi1=5,
         pi2=6,
         tol = 0.0001,
         maximum = T)
y0m

> y0m
$maximum
[1] 0.9999339

$objective
[1] 9.980357

If this/any other answer provided to you is useful to you please accept the answer /vote+ !如果提供给您的这个/任何其他答案对您有用,请接受答案 /vote+ !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM