Poisson GLM
koenvandenberge opened this issue · comments
Hi,
Suppose that I'd like to fit a constrained Poisson GLM using CVXR
, much like the logistic regression example in the documentation.
The program does not seem to optimize the function using either maximum likelihood formulation
## MWE
library(CVXR)
y <- rpois(n=100, lambda=50)
X <- matrix(rnorm(100), ncol=1)
betaHat <- Variable(1)
obj <- sum(y* log(exp(X %*% betaHat)) - exp(X %*% betaHat))
prob <- Problem(Maximize(obj))
result <- CVXR::solve(prob)
or a Poisson loss formulation
obj <- mean(exp(X %*% betaHat) - y * log(exp(X %*% betaHat)))
prob <- Problem(Minimize(obj))
result <- CVXR::solve(prob)
returning the error Error in construct_intermediate_chain(object, candidate_solvers, gp = gp) : Problem does not follow DCP rules. However, the problem does follow DGP rules. Consider calling this function with gp = TRUE
. Setting gp=TRUE
gives an opposite error.
Am I overlooking something?
Not sure what you are trying to do with the logs/exp in the objective. See below. Both ways will yield the same result.
> y <- rpois(n=100, lambda=50)
> X <- matrix(rnorm(100), ncol=1)
> betaHat <- Variable(1)
> obj <- sum(y* X %*% betaHat - exp(X %*% betaHat))
> prob <- Problem(Maximize(obj))
> result <- CVXR::solve(prob)
> result$getValue(betaHat)
[1] 1.496695
> obj <- mean(exp(X %*% betaHat) - y * X %*% betaHat)
> prob <- Problem(Minimize(obj))
> result <- CVXR::solve(prob)
> result$getValue(betaHat)
[1] 1.496695
I see, that's useful. Many thanks!