Re: [R] how to use mle?

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Wed 08 Feb 2006 - 13:18:14 EST

          I see two problems:

          First, "mle" expects its first argument to return (-log(likelihood));   your "LL" is closer to "+log(likelihood)", but it seems to be missing something beyond that. In particular, "Y" is 10 x 3, but "fit" is only 10 x 2.

          Second, I'm not sure, but it looks to me like "mle" wants to optimize over scalar arguments its first argument, "LL" in your case, and you want it to optimize over a vector. I can't see where the help page for "mle" spells it out, but unless I missed something, the examples all used scalar arguments. Moreover, I walked through your call to "mle" line by line after 'debug(mle)'. The argument "start" that "mle" passes to "optim" was of length 0. Therefore, you need to do something different to convince "mle" that it needs to optimize by varying more than 0 parameters.

          Have you considered changing the sign of the output of "LL" (which you need to do anway) and giving it directly to "optim"? That should work.

          Moreover, it looks to me like you want to do multinomial logistic regression. If that is so, have you considered searching for that in particular? RSiteSearch("multinomial logistic regression") returned 130 hits for me just now.

	  hope this helps.
	  spencer graves

ronggui wrote:

>>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"
>
> 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


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 Wed Feb 08 14:35:06 2006

This archive was generated by hypermail 2.1.8 : Fri 10 Feb 2006 - 19:39:04 EST