[R] Logit regression using MLE

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Jan 29 23:17:32 CET 2006


"Martin" <martin.chlond at btinternet.com> writes:

> I have used the following code to obtain a max likelihood estimator for
> a logit regression. The final command invokes ‘optim’ to obtain the
> parameter estimates. The code works OK but I want to use the ‘mle’
> function in the ‘stats4’ package instead of directly calling ‘optim’.
> Can someone please figure out the command to do this?
> 

You need to make sure that the loglikelihood you pass to mle is a
function of the parameters only, and you have to string out the
parameter vector:

ll <- eval(function(beta0=0,beta1=0)
             log.lo.like (c(beta0,beta1),Y,X),
           list(X=X,Y=Y))


summary(mle(ll))

glm(Y~X-1,family=binomial)

  
> 
> Thank you in advance.
> 
>  
> 
> Martin 
> 
>  
> 
> # mlelo.r - maximum likelihood estimation for logit regression
> 
>  
> 
> # log-likelihood function (returns negative to minimise)
> 
> log.lo.like <- function(beta,Y,X) {
> 
>   Fbetax   <- 1/(1+exp(-beta%*%t(X)))
> 
>   loglbeta <- -log(prod(Fbetax^Y*(1-Fbetax)^(1-Y)))
> 
> }
> 
>  
> 
> # data
> 
> Y <-  c(0,0,1,0,0,1,1,0,0,0,0,1,1,0,1,1,0,1,1,0,1)
> 
> X <-
> cbind(matrix(1,21,1),matrix(c(-48.5,24.4,82.8,-24.6,-31.6,91.0,52.1,-87.7,
> -17.0,-51.5,-90.7,65.5,-44.0,-7.0,51.6,32.4,-61.8,34.0,27.9,-72.9,49.9),
> 21,1))
> 
>  
> 
> # maximum likelihood estimator
> 
> mle.res <- optim(c(0,0),log.lo.like,Y=Y, X=X)
> 

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list