# R Optimisation Tips using Optim and Maximum Likelihood

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Jeromy Anglim's Blog: Psychology and Statistics**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post summarises some R modelling tips I picked up atAMPC2011.

I got some tips from a tutorial on parameter estimationput on by Scott Brownfrom the Newcastle Cognition Lab.The R code used in the tutorial is available directly hereor from the conference website.

The main tips I took from the tutorial were:

- Consider using BICas a model comparison criterion.
- When modelling reaction times, consider modelling data as a mixture model oftwo processes. One process is the main process of experimental interest andanother is a secondary process that otherwise contributes noise.The secondary process is used to capture what would otherwise be outliers thatflow, particularly, from very slow reaction times observed when participantsget distracted.Probability assigned to the two processescan be specified a priori based on knowledge of the experimental phenomena.In the specific example that Scott showed, the outlier process was given aprobability of 0.03 and this was treated as a uniform distribution between 0and the trial time-out time.
- Consider transformations model parameters for the purpose of estimation andthen converting the transformed parameters back to their original scale.This can facilitate estimation and also assist in enforcing psychologicallymeaningful constraints on parameter values (e.g., ensuring that asymptoticreaction time is greater than zero).
- The combination of the R function
`optim`

and a custom created objectivefunction, such as a minus log-likelihood function provides a powerful tool forparameter estimation of custom models.- Scott Brown’s tutorial includes an example ofthis.
- Ajay Shah has an exampleof writing a likelihood function and then getting a maximum likelihoodestimate using
`optim`

. - Benjamin Bolker has great material available on the web from his bookEcological Models and Data in R.PDFs, Rnw, and R code for early versions of the chapters are provided onthe website.Chapter 6 (likelihood and all that), 7 (the gory details of model fitting),and 8 (worked likelihood estimation examples).
- Brian Ripley has a set of slides on simulation and optimisation in R. In particular it provides a useful discussion of the various optimisationalgorithms available using
`optim`

.

- I also got the impression that it will soon be time to dive into WinBUGS.

To

**leave a comment**for the author, please follow the link and comment on their blog:**Jeromy Anglim's Blog: Psychology and Statistics**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.