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