# Re: Efficient way to code using optim() Classic List Threaded 3 messages Open this post in threaded view
|

## Re: Efficient way to code using optim()

 Hi all,   I am trying to estimate a simple logit model. By using MLE, I am maximizing the log likelihood, with optim(). The thing is, each observation has different set of choice options, so I need a loop inside the objective function, which I think slows down the optimization process.   The data is constructed so that each row represent the characteristics for one alternative, and CS is a variable that represents choice situations. (say, 1 ~ Number of observations) cum_count is the ¡°cumulative¡± count of each choice situations, i.e. number of available alternatives in each CS. So I am maximizing the sum of [exp(U(chosen)) / sum(exp(U(all alternatives)))]   When I have 6,7 predictors, the running time is about 10 minutes, and it slows down exponentially as I have more predictors. (More theta¡¯s to estimate) I want to know if there is a way I can improve the running time. Below is my code..   simple_logit = function(theta){                 realized_prob = rep(0, max(data\$CS))                 theta_multiple = as.matrix(data[,4:35]) %*% as.matrix(theta)                 realized_prob = exp(theta_multiple) / sum(exp(theta_multiple[1:cum_count]))                 for (i in 2:length(realized_prob)){                                 realized_prob[i] = exp(theta_multiple[cum_count[(i-1)]+1]) / sum(exp(theta_multiple[((cum_count[(i-1)]+1):cum_count[i])]))                                 }                 -sum(log(realized_prob)) }   initial = rep(0,32) out33 = optim(initial, simple_logit, method="BFGS", hessian=TRUE)       Many thanks in advance!!!     _________________________________________________________________         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.