我试图在R
中使用Newton-Raphson算法来最小化我为一个非常具体的问题编写的对数似然函数。我会诚实地说,估计方法在我的头脑中,但我知道我的领域中的许多人(心理测量学)使用NR算法进行估计,所以我试图使用这种方法,至少开始时。我有一系列嵌套函数,它们返回一个标量作为特定数据向量的对数似然估计值:
log.likelihoodSL <- function(x,sxdat1,item) {
theta <- x[1]
rho <- x[2]
log.lik <- 0
for (it in 1:length(sxdat1)) {
val <- as.numeric(sxdat1[it])
apars <- item[it,1:3]
cpars <- item[it,4:6]
log.lik <- log.lik + as.numeric(log.pSL(theta,rho,apars,cpars,val))
}
return(log.lik)
}
log.pSL <- function(theta,rho,apars,cpars,val) {
p <- (rho * e.aSL(theta,apars,cpars,val)) + ((1-rho) * e.nrm(theta,apars,cpars,val))
log.p <- log(p)
return(log.p)
}
e.aSL <- function(theta,apars,cpars,val) {
if (val==1) {
aprob <- e.nrm(theta,apars,cpars,val)
} else if (val==2) {
aprob <- 1 - e.nrm(theta,apars,cpars,val)
} else
aprob <- 0
return(aprob)
}
e.nrm <- function(theta,apars,cpars,val) {
nprob <- exp(apars*theta + cpars)/sum(exp((apars*theta) + cpars))
nprob <- nprob[val]
return(nprob)
}
这些功能都按照显示的顺序依次互相呼叫。对最高功能的调用如下:
max1 <- maxNR(log.likelihoodSL,grad=NULL,hess=NULL,start=x,print.level=1,sxdat1=sxdat1,item=item)
以下是输入数据的示例(在本例中我称之为sxdat1
):
> sxdat1
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18
2 1 3 1 3 3 2 2 3 2 2 2 2 2 3 2 3 2
V19 V20
2 2
这是变量item
:
> item
V1 V2 V3 V4 V5 V6
[1,] 0.2494625 0.3785529 -0.6280155 -0.096817808 -0.7549263 0.8517441
[2,] 0.2023690 0.4582290 -0.6605980 -0.191895013 -0.8391203 1.0310153
[3,] 0.2044005 0.3019147 -0.5063152 -0.073135691 -0.6061725 0.6793082
[4,] 0.2233619 0.4371988 -0.6605607 -0.160377714 -0.8233197 0.9836974
[5,] 0.2257933 0.2851198 -0.5109131 -0.044494872 -0.5970246 0.6415195
[6,] 0.2047308 0.3438725 -0.5486033 -0.104356236 -0.6693569 0.7737131
[7,] 0.3402220 0.2724951 -0.6127172 0.050795183 -0.6639092 0.6131140
[8,] 0.2513672 0.3263046 -0.5776718 -0.056203015 -0.6779823 0.7341853
[9,] 0.2008285 0.3389165 -0.5397450 -0.103565987 -0.6589961 0.7625621
[10,] 0.2890680 0.2700661 -0.5591341 0.014251386 -0.6219001 0.6076488
[11,] 0.3127214 0.2572715 -0.5699929 0.041587479 -0.6204483 0.5788608
[12,] 0.2697048 0.2965255 -0.5662303 -0.020115553 -0.6470669 0.6671825
[13,] 0.2799978 0.3219374 -0.6019352 -0.031454750 -0.6929045 0.7243592
[14,] 0.2773233 0.2822723 -0.5595956 -0.003711768 -0.6314010 0.6351127
[15,] 0.2433519 0.2632824 -0.5066342 -0.014947878 -0.5774375 0.5923853
[16,] 0.2947281 0.3605812 -0.6553092 -0.049389825 -0.7619178 0.8113076
[17,] 0.2290081 0.3114185 -0.5404266 -0.061807853 -0.6388839 0.7006917
[18,] 0.3824588 0.2543871 -0.6368459 0.096053788 -0.6684247 0.5723709
[19,] 0.2405821 0.3903595 -0.6309416 -0.112333048 -0.7659758 0.8783089
[20,] 0.2424331 0.3028480 -0.5452811 -0.045311136 -0.6360968 0.6814080
我希望最小化函数log.likelihood()
的两个参数是theta和rho,我想将theta限制在介于-3和3之间,并且rho介于0和1之间,但我不知道我不知道如何用当前的设置做到这一点。有人可以帮帮我吗?我是否需要使用Newton-Raphson方法中的其他估算方法,或者是否有一种方法可以使用函数maxNR
来实现此方法,该函数来自我目前正在使用的包maxLik
?谢谢!
编辑:包含参数theta和rho的起始值的向量x
只是c(0,0)
,因为这是这些参数的“平均”或“默认”假设(就术语而言)他们的实质性解释)。
答案 0 :(得分:8)
数据更方便:
sxdat1 <- c(2,1,3,1,3,3,2,2,3,2,2,2,2,2,3,2,3,2,2,2)
item <- matrix(c(
0.2494625,0.3785529,-0.6280155,-0.096817808,-0.7549263,0.8517441,
0.2023690,0.4582290,-0.6605980,-0.191895013,-0.8391203,1.0310153,
0.2044005,0.3019147,-0.5063152,-0.073135691,-0.6061725,0.6793082,
0.2233619,0.4371988,-0.6605607,-0.160377714,-0.8233197,0.9836974,
0.2257933,0.2851198,-0.5109131,-0.044494872,-0.5970246,0.6415195,
0.2047308,0.3438725,-0.5486033,-0.104356236,-0.6693569,0.7737131,
0.3402220,0.2724951,-0.6127172,0.050795183,-0.6639092,0.6131140,
0.2513672,0.3263046,-0.5776718,-0.056203015,-0.6779823,0.7341853,
0.2008285,0.3389165,-0.5397450,-0.103565987,-0.6589961,0.7625621,
0.2890680,0.2700661,-0.5591341,0.014251386,-0.6219001,0.6076488,
0.3127214,0.2572715,-0.5699929,0.041587479,-0.6204483,0.5788608,
0.2697048,0.2965255,-0.5662303,-0.020115553,-0.6470669,0.6671825,
0.2799978,0.3219374,-0.6019352,-0.031454750,-0.6929045,0.7243592,
0.2773233,0.2822723,-0.5595956,-0.003711768,-0.6314010,0.6351127,
0.2433519,0.2632824,-0.5066342,-0.014947878,-0.5774375,0.5923853,
0.2947281,0.3605812,-0.6553092,-0.049389825,-0.7619178,0.8113076,
0.2290081,0.3114185,-0.5404266,-0.061807853,-0.6388839,0.7006917,
0.3824588,0.2543871,-0.6368459,0.096053788,-0.6684247,0.5723709,
0.2405821,0.3903595,-0.6309416,-0.112333048,-0.7659758,0.8783089,
0.2424331,0.3028480,-0.5452811,-0.045311136,-0.6360968,0.6814080),
byrow=TRUE,ncol=6)
使用maxNR
:
library(maxLik)
x <- c(0,0)
max1 <- maxNR(log.likelihoodSL,grad=NULL,hess=NULL,start=x,
print.level=1,sxdat1=sxdat1,item=item)
注意rho
消极时发生的警告。但是,maxNR
可以恢复
从这里得到一个估计(theta = -1,rho = 0.63)在内部
可行集。 L-BFGS-B
无法处理非有限的中间结果,而是处理边界
保持算法远离那些有问题的区域。
我选择使用bbmle
而不是optim
执行此操作:bbmle
是optim
(以及其他优化工具)的包装,它提供了一些特定于似然估计(分析,置信区间,模型之间的似然比检验等)。
library(bbmle)
## mle2() wants a NEGATIVE log-likelihood
NLL <- function(x,sxdat1,item) {
-log.likelihoodSL(x,sxdat1,item)
}
编辑:在早期版本中,我使用control=list(fnscale=-1)
告诉优化器我正在传递一个应该最大化而不是最小化的对数似然函数;这得到了正确的答案,但随后尝试使用结果可能会变得非常混乱,因为包不能解释这种可能性(例如,报告的对数似然的符号是错误的)。这可以在包装中修复,但我不确定它是否值得。
## needed when objective function takes a vector of args rather than
## separate named arguments:
parnames(NLL) <- c("theta","rho")
(m1 <- mle2(NLL,start=c(theta=0,rho=0.5),method="L-BFGS-B",
lower=c(theta=-3,rho=2e-3),upper=c(theta=3,rho=1-2e-3),
data=list(sxdat1=sxdat1,item=item)))
这里有几点:
rho=0.5
而不是边界开始rho
边界(L-BFGS-B
在计算导数的有限差分近似值时并不总是完全尊重边界)data
参数在这种情况下,我得到与maxNR
相同的结果。
## Call:
## mle2(minuslogl = NLL, start = c(theta = 0, rho = 0.5),
## method = "L-BFGS-B", data = list(sxdat1 = sxdat1, item = item),
## lower = c(theta = -3, rho = 0.002), upper = c(theta = 3,
## rho = 1 - 0.002), control = list(fnscale = -1))
##
## Coefficients:
## theta rho
## -1.0038531 0.6352782
##
## Log-likelihood: -18.11
除非你真的需要用Newton-Raphson做这个,而不是用基于渐变的“准牛顿”方法,我猜这已经足够了。 (这听起来像你有很强的技术理由这样做,除了“这是其他人在我的领域做的事情” - 一个很好的理由,所有其他事情都是平等的,但在这种情况下不足以让我挖当类似的方法很容易获得并且工作正常时,可以实施NR。)