Matlab的fminunc函数的R等效项是什么? [英] What is the R equivalent of Matlab's fminunc function?

查看:94
本文介绍了Matlab的fminunc函数的R等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了计算最佳θ,例如在逻辑回归中,我必须创建一个costFunction(要最小化的函数),然后将其传递给fminunc以获得最佳theta. 另外,如果可以计算costFunction的梯度,则可以使用

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');

并对costFunction进行编码,以使其返回X的梯度值g作为第二个输出参数. 然后我给

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);

其中X是数据矩阵,y是响应.如何在R中实现上述功能?

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

推荐答案

看看optim函数.它可以使用method = 'L-BFGS-B'进行无约束最小化,并且您还可以指定解析函数来计算梯度

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

编辑.正如Ben正确指出的那样,fminunc进行无约束的优化,也可以使用optim函数选择Nelder-MeadBFGS来实现.此外,我还从fminunc的文档中注意到,它使用trust区域方法进行了大规模优化.我相信有一个R包trust可以做同样的事情.我建议您看看R的optimization 任务视图.

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.

这篇关于Matlab的fminunc函数的R等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆