R Programming/Maximum Likelihood
From Wikibooks, the open-content textbooks collection
Packages and functions :
- mle() in the "stats4" package
- The "maxLik" library
- fitdistr() in the "MASS" package for univariate problems
- nlm()
- optimize()
- optim()
If you need to program yourself your maximum likelihood estimator (MLE) you have to use one built-in optimizer such as nlm(), optim() or optimize(). optimize() is only available for one dimensional problems.
As an example we can write a simple maximum likelihood estimator for a Cauchy distribution using the nlm() optimizer. We first draw a vector x from a Cauchy distribution. Then we define the log likelihood function and then we optimize using the nlm() function.
Note that nlm() is minimizer and not a maximizer.
> n <- 100
> x <- rcauchy(n)
> mlog.1 <- function(mu, x) {
+ sum(-dcauchy(x, location = mu, log = TRUE))
+ }
> mu.start <- median(x)
> out <- nlm(mlog.1, mu.start, x = x)
> out
$minimum
[1] 241.5354
$estimate
[1] -0.08364306
$gradient
[1] 6.423306e-06
$code
[1] 1
$iterations
[1] 2
Contents |
[edit] Tests
[edit] Likelihood Ratio Test
- lrtest in the lmtest package[1].
[edit] Resources
- Charles Geyer : Maximum Likelihood in R (pdf, 9 pages)
- Marco Steenbergen Maximum Likelihood Programming in R (pdf, 7 pages)
- Michael Ward : Maximum Likelihood
[edit] References
- ↑ Achim Zeileis, Torsten Hothorn (2002). Diagnostic Checking in Regression Relationships. R News 2(3), 7-10. URL http://CRAN.R-project.org/doc/Rnews/