[R] Fitting GLM with BFGS algorithm

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Oct 26 13:51:55 CEST 2010


for instance, for logistic regression you can do something like this:

# simulate some data
x <- cbind(1, runif(100, -3, 3), rbinom(100, 1, 0.5))
y <- rbinom(100, 1, plogis(c( x%*% c(-2, 1, 0.3))))

# BFGS from optim()
fn <- function (betas, y, x) {
   -sum(dbinom(y, 1, plogis(c(x %*% betas)), log = TRUE))
}
optim(rep(0, ncol(x)), fn, x = x, y = y, method = "BFGS")

# IWLS from glm()
glm(y ~ x[, -1], family = "binomial")

You can also improve it by providing the minus score vector as a third 
argument to optim().


I hope it helps.

Best,
Dimitris


On 10/26/2010 1:38 PM, justin bem wrote:
> Dear all,
>
> By default the glm function in the stats package use IWLS. How can I fit a glm
> model using BFGS algorithm ?
>   Justin BEM
> BP 1917 Yaoundé
> Tél (237) 76043774
>
>
>
>
> 	[[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list