Thanks again. Now I want to complicate the model. Supose that Y is a vector with 30 data.
Y = c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0) Then the r = sum(Y*log(comb)) Is it possible? I want to discover the variables values (alpha12,w_g12,w_u12) for each Y that optimize the function r. The new code is: Model = function(alpha12,w_g12,w_u12) { Y = c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0) u = 0.5 g = -1 comb = alpha12 + g*w_g12 + u*w_u12 r = sum(Y*log(comb)) cat(alpha12,w_g12,w_u12,comb,log(comb),r,"\n") ## debug r } res = mle(minuslog=Model,start=list(alpha12=0.1,w_u12=0.1,w_g12=0.1), method = "BFGS") model2 <- function(p) { do.call("Model",as.list(p)) } constrOptim(theta=c(0.1,0.1,0.1),f=model2, grad=NULL, ui = c(1,-1,0.5), ci = rep(0,3)) ERROR: Error in optim(start, f, method = method, hessian = TRUE, ...) : objective function in optim evaluates to length 30 not 1 Best regards, LFRC Ben Bolker wrote: > > LFRC <feliperiehs <at> yahoo.com.br> writes: > >> >> >> Dear Ben Bolker, >> >> Thanks a lot for your help. >> >> I have two more questions: >> >> 1) My goal is maximize the function (> r = Y*log(comb)) but the >> parameters found, minimized the function (r = Y*log(comb)). >> > > Oh. Oops. Just change the sign ( r = -Y*log(comb)) or > set fnscale=-1 (see ?optim) > >> 2) What this function do? >> > model2 <- function(p) { >> > do.call("Model",as.list(p)) >> > } >> > > I got a little carried away, this is R Magic. > mle() expects the function to be written out with > a list of parameters ( func(param1,param2,param3) ), > while optim and constrOptim want the function to > have a single vector argument ( func(paramvec) ). > model2() is a wrapper that converts a vector p > into a list and passes it to the Model function. > > Ben Bolker > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > -- View this message in context: http://www.nabble.com/Help-MLE-tp19904724p19927405.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.