[R] how to use mle?

From: ronggui <ronggui.huang_at_gmail.com>
Date: Fri 03 Feb 2006 - 05:04:35 EST


>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
Received on Fri Feb 03 05:12:17 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:18 EST