R Programming/Optimization
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Intro
- optimize() is devoted to one dimensional optimization problem.
- optim() and nlm() can be used for multidimensional optimization problems.
- nlminb()
- library(quadprog)
[edit] References
- Optimization (Mathematics)
- Optimization (Statistics Wikibook)
- Venables and Ripley, Chapter 16.
- Braun and Murdoch, Chapter 7.
- Updating and improving optim(), Use R 2009 slides
- Cameron and Trivedi, Microeconometrics, chapter 10
[edit] One dimensional problem
The one dimensional problem :
> func <- function(x){
+ return <- (x-2)^2
+ }
> (func(-2))
[1] 16
>
> # plot your function using the 'curve function'
> curve(func,-4,8)
>
> # Here is another way to plot the function
> # using a grid
> grid <- seq(-10,10,by=.1)
> func(grid)
> plot(grid,func(grid))
>
> # you can find the minimum using the optimize function
> optimize(f=func,interval=c(-10,10))
$minimum
[1] 2
$objective
[1] 0
[edit] Newton-Raphson
- "maxLik" package for maximization of a likelihood function. This package includes the Newton Raphson method.
[edit] Nelder Mead
> func <- function(x){
+ out <- (x[1]-2)^2 + (x[2]-1)^2
+ return <- out
+ }
>
> optim(par=c(0,0), fn=func, gr = NULL,
+ method = c("Nelder-Mead"),
+ lower = -Inf, upper = Inf,
+ control = list(), hessian = T)
[edit] BFGS
- The BFGS method
> func <- function(x){
+ out <- (x[1]-2)^2 + (x[2]-1)^2
+ return <- out
+ }>
> optim(par=c(0,0), fn=func, gr = NULL,
+ method = c("BFGS"),
+ lower = -Inf, upper = Inf,
+ control = list(), hessian = T)
> optim(par=c(0,0), fn=func, gr = NULL,
+ method = c("L-BFGS-B"),
+ lower = -Inf, upper = Inf,
+ control = list(), hessian = T)
[edit] Simulated Annealing
> func <- function(x){
+ out <- (x[1]-2)^2 + (x[2]-1)^2
+ return <- out
+ }>
> optim(par=c(0,0), fn=func, gr = NULL,
+ method = c("SANN"),
+ lower = -Inf, upper = Inf,
+ control = list(), hessian = T)
[edit] Trust Region Method
- "trust" package for trust region method
[edit] The simplex algorithm
- The boot package includes a simplex method
[edit] Genetic Algorithm
- "rgenoud" package for genetic algotihm