# Logistic Regression

## Introduction

In classification problems, the goal is to predict the class membership based on predictors. Often there are two classes and one of the most popular methods for binary classification is logistic regression (Cox 1958, @Freedman:2009).

Suppose now that $$y_i \in \{0,1\}$$ is a binary class indicator. The conditional response is modeled as $$y|x \sim \mbox{Bernoulli}(g_{\beta}(x))$$, where $$g_{\beta}(x) = \frac{1}{1 + e^{-x^T\beta}}$$ is the logistic function, and maximize the log-likelihood function, yielding the optimization problem

$\begin{array}{ll} \underset{\beta}{\mbox{maximize}} & \sum_{i=1}^m \{ y_i\log(g_{\beta}(x_i)) + (1-y_i)\log(1 - g_{\beta}(x_i)) \}. \end{array}$

CVXR provides the logistic atom as a shortcut for $$f(z) = \log(1 + e^z)$$ to express the optimization problem. One may be tempted to use log(1 + exp(X %*% beta)) as in conventional R syntax. However, this representation of $$f(z)$$ violates the DCP composition rule, so the CVXR parser will reject the problem even though the objective is convex. Users who wish to employ a function that is convex, but not DCP compliant should check the documentation for a custom atom or consider a different formulation.

## Example

The formulation is very similar to OLS, except for the specification of the objective.

In the example below, we demonstrate a key feature of CVXR, that of evaluating various functions of the variables that are solutions to the optimization problem. For instance, the log-odds, $$X\hat{\beta}$$, where $$\hat{\beta}$$ is the logistic regression estimate, is simply specified as X %*% beta below, and the getValue function of the result will compute its value. (Any other function of the estimate can be similarly computed.)

n <- 20
m <- 1000
offset <- 0
sigma <- 45
DENSITY <- 0.2

set.seed(183991)
beta_true <- stats::rnorm(n)
idxs <- sample(n, size = floor((1-DENSITY)*n), replace = FALSE)
beta_true[idxs] <- 0
X <- matrix(stats::rnorm(m*n, 0, 5), nrow = m, ncol = n)
y <- sign(X %*% beta_true + offset + stats::rnorm(m, 0, sigma))

beta <- Variable(n)
obj <- -sum(logistic(-X[y <= 0, ] %*% beta)) - sum(logistic(X[y == 1, ] %*% beta))
prob <- Problem(Maximize(obj))
result <- solve(prob)

log_odds <- result$getValue(X %*% beta) beta_res <- result$getValue(beta)
y_probs <- 1/(1 + exp(-X %*% beta_res))

We can compare with the standard stats::glm estimate.

d <- data.frame(y = as.numeric(y > 0), X = X)
glm <- stats::glm(formula = y ~ 0 + X, family = "binomial", data = d)
est.table <- data.frame("CVXR.est" = beta_res, "GLM.est" = coef(glm))
rownames(est.table) <- paste0("$\\beta_{", 1:n, "}$")
knitr::kable(est.table, format = "html") %>%
kable_styling("striped") %>%
column_spec(1:3, background = "#ececec")
CVXR.est GLM.est
$$\beta_{1}$$ -0.0305494 0.0305494
$$\beta_{2}$$ 0.0023528 -0.0023528
$$\beta_{3}$$ -0.0110080 0.0110080
$$\beta_{4}$$ 0.0163919 -0.0163919
$$\beta_{5}$$ 0.0157186 -0.0157186
$$\beta_{6}$$ 0.0006251 -0.0006251
$$\beta_{7}$$ -0.0157914 0.0157914
$$\beta_{8}$$ -0.0092228 0.0092228
$$\beta_{9}$$ 0.0173823 -0.0173823
$$\beta_{10}$$ 0.0019102 -0.0019102
$$\beta_{11}$$ -0.0100746 0.0100746
$$\beta_{12}$$ -0.0269883 0.0269883
$$\beta_{13}$$ 0.0233625 -0.0233625
$$\beta_{14}$$ 0.0009529 -0.0009529
$$\beta_{15}$$ -0.0016264 0.0016264
$$\beta_{16}$$ 0.0312156 -0.0312156
$$\beta_{17}$$ 0.0038949 -0.0038949
$$\beta_{18}$$ -0.0121105 0.0121105
$$\beta_{19}$$ 0.0246811 -0.0246811
$$\beta_{20}$$ -0.0007025 0.0007025

The sign difference is due to the coding of $$y$$ as $$(-1, 1)$$ for CVXR rather than $$(0, 1)$$ for stats::glm.

So, for completeness, if we were to code the $$y$$ as $$(0, 1)$$, the objective will have to be modified as below.

obj <- -sum(X[y <= 0, ] %*% beta) - sum(logistic(-X %*% beta))
prob <- Problem(Maximize(obj))
result <- solve(prob)
beta_log <- result$getValue(beta) est.table <- data.frame("CVXR.est" = beta_log, "GLM.est" = coef(glm)) rownames(est.table) <- paste0("$\\beta_{", 1:n, "}\$")
knitr::kable(est.table, format = "html") %>%
kable_styling("striped") %>%
column_spec(1:3, background = "#ececec")
CVXR.est GLM.est
$$\beta_{1}$$ 0.0305494 0.0305494
$$\beta_{2}$$ -0.0023528 -0.0023528
$$\beta_{3}$$ 0.0110080 0.0110080
$$\beta_{4}$$ -0.0163919 -0.0163919
$$\beta_{5}$$ -0.0157186 -0.0157186
$$\beta_{6}$$ -0.0006251 -0.0006251
$$\beta_{7}$$ 0.0157914 0.0157914
$$\beta_{8}$$ 0.0092228 0.0092228
$$\beta_{9}$$ -0.0173823 -0.0173823
$$\beta_{10}$$ -0.0019102 -0.0019102
$$\beta_{11}$$ 0.0100746 0.0100746
$$\beta_{12}$$ 0.0269883 0.0269883
$$\beta_{13}$$ -0.0233625 -0.0233625
$$\beta_{14}$$ -0.0009529 -0.0009529
$$\beta_{15}$$ 0.0016264 0.0016264
$$\beta_{16}$$ -0.0312156 -0.0312156
$$\beta_{17}$$ -0.0038949 -0.0038949
$$\beta_{18}$$ 0.0121105 0.0121105
$$\beta_{19}$$ -0.0246811 -0.0246811
$$\beta_{20}$$ 0.0007025 0.0007025

Now, the results match perfectly.

## Session Info

sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-apple-darwin19.5.0 (64-bit)
## Running under: macOS Catalina 10.15.7
##
## Matrix products: default
## BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.10_1/lib/libopenblasp-r0.3.10.dylib
##
## locale:
##  en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
##  stats     graphics  grDevices datasets  utils     methods   base
##
## other attached packages:
##  kableExtra_1.1.0 CVXR_1.0-9
##
## loaded via a namespace (and not attached):
##   gmp_0.6-0         Rcpp_1.0.5        highr_0.8         compiler_4.0.2
##   pillar_1.4.6      tools_4.0.2       digest_0.6.25     bit_1.1-15.2
##   viridisLite_0.3.0 evaluate_0.14     tibble_3.0.3      lifecycle_0.2.0
##  lattice_0.20-41   pkgconfig_2.0.3   rlang_0.4.7       Matrix_1.2-18
##  gurobi_9.0.3.1    rstudioapi_0.11   Rglpk_0.6-4       yaml_2.2.1
##  blogdown_0.19     xfun_0.15         cccp_0.2-4        ECOSolveR_0.5.3
##  xml2_1.3.2        httr_1.4.2        Rmpfr_0.8-1       stringr_1.4.0
##  knitr_1.28        vctrs_0.3.2       hms_0.5.3         webshot_0.5.2
##  bit64_0.9-7       grid_4.0.2        glue_1.4.1        R6_2.4.1
##  rmarkdown_2.3     bookdown_0.19     readr_1.3.1       magrittr_1.5
##  codetools_0.2-16  rcbc_0.1.0.9001   scales_1.1.1      htmltools_0.5.0
##  ellipsis_0.3.1    assertthat_0.2.1  rvest_0.3.5       colorspace_1.4-1
##  Rcplex_0.3-3      stringi_1.4.6     Rmosek_9.2.3      munsell_0.5.0
##  slam_0.1-47       crayon_1.3.4

R Markdown