Re: [R] how to use mle?

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Fri 03 Feb 2006 - 05:55:43 EST

ronggui <ronggui.huang@gmail.com> writes:

> >Y
> [,1] [,2] [,3]
> [1,] 0 1 0
> [2,] 0 1 0
> [3,] 0 0 1
> [4,] 1 0 0
> [5,] 0 0 1
> [6,] 0 0 1
> [7,] 1 0 0
> [8,] 1 0 0
> [9,] 0 0 1
> [10,] 1 0 0
>
> >X
> pri82 pan82
> 1 0 0
> 2 0 0
> 3 1 0
> 4 1 0
> 5 0 1
> 6 0 0
> 7 1 0
> 8 1 0
> 9 0 0
> 10 0 0
>
> >K=2
> >J=3
>
>
> LL <- function(b=rep(0,(J-1)*K)){
> B=matrix(c(b,rep(0,K)),ncol=J,nrow=K)
> fit <- X%*%B
> p<-exp(fit)/rowSums(exp(fit))
> sum(Y*log(p))
> }
>
> grad<- function(b=rep(0,(J-1)*K)){
> B=matrix(c(b,rep(0,K)),ncol=J,nrow=K)
> fit <- X%*%B
> p<-exp(fit)/rowSums(exp(fit))
> Yp <- Y-p
> Yp<-matrix(rep(t(Yp),each=K),ncol=K*J,by=T)
> X <- matrix(rep(X,J) ,ncol=K*J)
> apply(Yp*X,2,sum)
> }
>
> library(stats4)
> mle(LL)
> Error in validObject(.Object) : invalid class "mle" object: invalid
> object for slot "fullcoef" in class "mle": got class "list", should be
> or extend class "numeric"

It is a bit strange that it gets dicovered so late (when the mle object is printed), but the issue is that the negative log likelihood has to be a function of a number of *scalar* parameters.

Vector parameters are not allowed (yet? this is not the first case where people have expected or desired that they were allowed).  

> mle(LL,gr=grad)
> Error in optim(start, f, method = method, hessian = TRUE, ...) :
> gradient in optim evaluated to length 6 not 0
>
> what is wrong with my code?I try to fix it myself but fails,anyone
> helps me ?Thank you!
>
> --
> Deparment of Sociology
> Fudan University
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
   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@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Feb 03 06:11:32 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Feb 2006 - 15:12:40 EST