楼主: ZJ19900420
2564 4

[问答] optim函数 [推广有奖]

  • 1关注
  • 1粉丝

初中生

61%

还不是VIP/贵宾

-

威望
0
论坛币
4 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
85 点
帖子
10
精华
0
在线时间
12 小时
注册时间
2012-10-5
最后登录
2014-1-4

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
请问有没有哪位大神对optim函数比较熟悉的啊?跪求指导呀!万分感谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Optim OPT Tim 请问有没有 跪求指导

沙发
nuomin 发表于 2013-8-1 11:00:27 |只看作者 |坛友微信交流群
90后?

使用道具

藤椅
ZJ19900420 发表于 2013-8-2 15:53:52 |只看作者 |坛友微信交流群
nuomin 发表于 2013-8-1 11:00
90后?
是滴,请问你对这个函数熟悉吗?

使用道具

板凳
nuomin 发表于 2013-8-3 15:46:12 |只看作者 |坛友微信交流群
ZJ19900420 发表于 2013-8-2 15:53
是滴,请问你对这个函数熟悉吗?
谷歌搜 optim in r,有很好的例子

使用道具

报纸
ryusukekenji 发表于 2013-8-3 17:41:08 |只看作者 |坛友微信交流群
http://www.r-bloggers.com/how-to-use-optim-in-r/

How to use optim in R
March 12, 2013
By Markus Gesmann

(This article was first published on mages' blog, and kindly contributed to R-bloggers)
A friend of mine asked me the other day how she could use the function optim in R to fit data. Of course there are functions for fitting data in R and I wrote about this earlier. However, she wanted to understand how to do this from scratch using optim.

The function optim provides algorithms for general purpose optimisations and the documentation is perfectly reasonable, but I remember that it took me a little while to get my head around how to pass data and parameters to optim. Thus, here are two simple examples.

I start with a linear regression by minimising the residual sum of square and discuss how to carry out a maximum likelihood estimation in the second example.

Minimise residual sum of squares
I start with an x-y data set, which I believe has a linear relationship and therefore I'd like to fit y against x by minimising the residual sum of squares.
dat=data.frame(x=c(1,2,3,4,5,6),
               y=c(1,3,5,6,8,12))
Next, I create a function that calculates the residual sum of square of my data against a linear model with two parameter. Think of y = par[1] + par[2] * x.
min.RSS <- function(data, par) {
              with(data, sum((par[1] + par[2] * x - y)^2))
             }
Optim minimises a function by varying its parameters. The first argument of optim are the parameters I'd like to vary, par in this case; the second argument is the function to be minimised, min.RSS. The tricky bit is to understand how to apply optim to your data. The solution is the ... argument in optim, which allows me to pass other arguments through to min.RSS, here my data. Therefore I can use the following statement:
result <- optim(par = c(0, 1), min.RSS, data = dat)
# I find the optimised parameters in result$par
# the minimised RSS is stored in result$value
result
## $par
## [1] -1.267  2.029
##
## $value
## [1] 2.819
##
## $counts
## function gradient
##       89       NA
##
## $convergence
## [1] 0
##
## $message
## NULL
Let me plot the result:
plot(y ~ x, data = dat)
abline(a = result$par[1], b = result$par[2], col = "red")

Read more »

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-22 06:25