7
$\begingroup$

How do I implement in R the equivalent of

lr = LogisticRegression(penalty='l2')
lr.fit(X, y)

in R?

$\endgroup$

1 Answer 1

12
$\begingroup$

Your question is how to run L2 regularized logistic regression in R.

My another detailed answer can be found here. Regularization methods for logistic regression

For implementation, there are more than one way of doing this.

  • Method 1, use glmnet(data,label,family="binomial", alpha=0, lambda=1), Details can be found in glmnet manual, check page 9.

  • Method 2 Use LiblineaR(data,label, type=0) or LiblineaR(data,label, type=7). Details can be found LiblineaR manual page 4. Both are L2-regularized logistic regression, one primal and one dual.

  • Mehtod 3, manual implementation.

Here are code for the regularized logistic loss and it's gradient. We can use an optimization toolbox (such as BFGS) to optimize.

rm(list=ls())
set.seed(0)
library(mlbench)

d=mlbench.2dnormals(100,2)
x=d$x
y=ifelse(d$classes==1,1,0)
lambda=1


logistic_loss <- function(w){
  p=plogis(x %*% w)
  L=-y*log(p)-(1-y)*log(1-p)
  LwR2=sum(L)+lambda*t(w) %*% w
  return(c(LwR2))
}

logistic_loss_gr <- function(w){
  p=plogis(x %*% w)
  v=t(x) %*% (p - y)
    return(c(v)+2*lambda*w)
}

optim(runif(2),logistic_loss,logistic_loss_gr,method="BFGS")
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.