In the documentation for glm, there is a method option where in the documentation it says that
User-supplied fitting functions can be supplied either as a function or a character string naming a function, with a function which takes the same arguments as glm.fit. If specified as a character string it is looked up from within the stats namespace.
I would like to use a binomial non-negative least squares optimizer so that the coefficients are non-negative and sum up to 1. An example of this optimizer being used is in the SuperLearner package with the option method = "method.NNLS". Below is a reproducible example:
library(SuperLearner)
# binary outcome
set.seed(1)
N <- 200
X <- matrix(rnorm(N*10), N, 10)
X <- as.data.frame(X)
Y <- rbinom(N, 1, plogis(.2*X[, 1] + .1*X[, 2] - .2*X[, 3] +
.1*X[, 3]*X[, 4] - .2*abs(X[, 4])))
SL.library <- c("SL.glmnet", "SL.glm", "SL.knn", "SL.mean")
# least squares loss function
test.NNLS <- SuperLearner(Y = Y, X = X, SL.library = SL.library,
verbose = TRUE, method = "method.NNLS", family = binomial())
> test.NNLS
Call:
SuperLearner(Y = Y, X = X, family = binomial(), SL.library = SL.library, method = "method.NNLS",
verbose = TRUE)
Risk Coef
SL.glmnet_All 0.2460486 0.0000000
SL.glm_All 0.2507033 0.2423697
SL.knn_All 0.2508500 0.3493301
SL.mean_All 0.2475494 0.4083002
Notice that the Coefs are non-negative and sum to 1. Is there a way to use the same optimizer with glm? Or a different optimizer that uses the same constraint? I've tried:
glm(Y ~ as.matrix(X), family = "binomial", method = "method.NNLS")
but it did not work.