我正在使用mlogit包进行多项逻辑回归。我想将弹性净罚分(Hastie和Zou,2005)添加到似然函数中。如:https://web.stanford.edu/~hastie/Papers/B67.2%20(2005)%20301-320%20Zou%20&%20Hastie.pdf
我已经尝试过glmnet包,但似乎它不像mlogit那样工作:它似乎不能识别长格式和不同选择集的数据。它以某种方式产生意想不到的结果。 (它在mlogit中工作正常,但可能没有惩罚。)
我正在尝试更改包中的代码,mlogit.lnl.R文件似乎包含似然函数,我试图在mlogit.lnl.R文件中添加一个套索术语“lnl”,但是它产生了相同的结果,好像我没有添加它。
在mlogit.lnl.R文件中:
lnl.slogit <- function(param, X, y, weights = NULL, gradient = FALSE,
hessian = FALSE, opposite, direction = rep(0, length(param)),
initial.value = NULL,stptol = 1E-01){
balanced <- FALSE
step <- 2
repeat{
step <- step / 2
if (step < stptol) break
eXb <- lapply(X, function(x) exp(crossprod(t(x), param + step * direction)))
seXb <- suml(eXb)
P <- lapply(eXb, function(x){v <- x/seXb; v[is.na(v)] <- 0; as.vector(v)})
Pch <- Reduce("+", mapply("*", P, y, SIMPLIFY = FALSE))
lnl <- sum(opposite * weights * log(Pch))
if (is.null(initial.value) || lnl <= initial.value) break
}
if (gradient | hessian) PX <- suml(mapply("*", X, P, SIMPLIFY = FALSE))
if (gradient){
Xch <- suml(mapply("*", X, y, SIMPLIFY = FALSE))
gradi <- opposite * weights * (Xch - PX)
attr(lnl, "gradi") <- gradi
attr(lnl, "gradient") <- if (is.matrix(gradi)) apply(gradi,2,sum) else
sum(gradi)
}
if (hessian){
XmPX <- lapply(X, function(x){g <- x - PX; g[is.na(g)] <- 0; g})
hessian <- - suml( mapply(function(x, y) crossprod(x * y, y),
P, XmPX, SIMPLIFY = FALSE))
attr(lnl, "hessian") <- opposite * hessian
}
if (step < stptol) lnl <- NULL
else{
P <- Reduce("cbind", P)
colnames(P) <- names(y)
attr(lnl, "probabilities") <- P
attr(lnl, "fitted") <- Pch
attr(lnl, "step") <- step
}
lnl + 0.5*sum(abs(param)) #I added the lasso here, the shrinkage factor here is 0.5
#but eventually, i want the elastic net penalty
}
关于如何在mlogit包中添加惩罚词的任何想法?或者还有其他任何可以做我想要的套餐吗?
提前致谢。
PS:包的代码可在此处获取:https://github.com/cran/mlogit/tree/master/R