Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the R equivalent of Matlab's fminunc function?

In order to compute the optimal theta e.g. in logistic regression, I have to create a costFunction (the function to be minimized) which is then passed to fminunc in order to obtain the optimal theta. Also, if the gradient of costFunction can be computed, I set the 'GradObj' option to 'on' using

options = optimset('GradObj','on');

and code the costFunction so that it returns, as a second output argument, the gradient value g of X. Then I give

[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

where X is the data matrix and y the response. How can I implement the above in R?

like image 314
George Dontas Avatar asked Oct 27 '11 18:10

George Dontas


People also ask

What is Fminunc function?

fminunc finds a minimum of a scalar function of several variables, starting at an initial estimate. This is generally referred to as unconstrained nonlinear optimization. x = fminunc(fun,x0) starts at the point x0 and finds a local minimum x of the function described in fun . x0 can be a scalar, vector, or matrix.

What is Fminunc octave?

fminunc attempts to determine a vector x such that fcn ( x ) is a local minimum. x0 determines a starting guess. The shape of x0 is preserved in all calls to fcn , but otherwise is treated as a column vector.

What is FVAL in Matlab?

fval — Objective function value at solution Objective function value at the solution, returned as a real number. Generally, fval = fun(x) .


2 Answers

Take a look at the optim function. It can do unconstrained minimization using method = 'L-BFGS-B' and you can specify an analytical function to compute the gradient as well

EDIT. As Ben has pointed out correctly, fminunc does unconstrained optimization, which can also be achieved using the optim function choosing Nelder-Mead or BFGS. Moreover, I also noticed from the documentation of fminunc that it does large-scale optimization using trust region methods. There is an R package trust that I believe does the same thing. I would recommend taking a look at the optimization task view of R.

like image 160
Ramnath Avatar answered Sep 28 '22 12:09

Ramnath


In the R, you can use the function nlminb in the R to do constrained optimization!

nlminb(start, objective, gradient = NULL, hessian = NULL, ..., scale = 1, control = list(), lower = -Inf, upper = Inf)

The start is a vector include all initial value of parameters. objective is the cost function or any other function that you want to minimize.

like image 45
Xiaorui Zhu Avatar answered Sep 28 '22 12:09

Xiaorui Zhu