Logit regression using MLE

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Logit regression using MLE

Martin-79
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?

 

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)


--
Internal Virus Database is out-of-date.
Checked by AVG Free Edition.

20/01/2006
 

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Logit regression using MLE

Peter Dalgaard
"Martin" <[hidden email]> 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
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html