[R] how to use mle?

ronggui ronggui.huang at gmail.com
Thu Feb 2 19:04:35 CET 2006


>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




More information about the R-help mailing list