# NOT RUN {
x <- rnbinom(100, mu = 10, size = 10)
hdev <- function(par)
-sum(dnbinom(x, mu = par[1], size = par[2], log = TRUE))
nlminb(c(9, 12), hdev)
nlminb(c(20, 20), hdev, lower = 0, upper = Inf)
nlminb(c(20, 20), hdev, lower = 0.001, upper = Inf)
## slightly modified from the S-PLUS help page for nlminb
# this example minimizes a sum of squares with known solution y
sumsq <- function( x, y) {sum((x-y)^2)}
y <- rep(1,5)
x0 <- rnorm(length(y))
nlminb(start = x0, sumsq, y = y)
# now use bounds with a y that has some components outside the bounds
y <- c( 0, 2, 0, -2, 0)
nlminb(start = x0, sumsq, lower = -1, upper = 1, y = y)
# try using the gradient
sumsq.g <- function(x, y) 2*(x-y)
nlminb(start = x0, sumsq, sumsq.g,
lower = -1, upper = 1, y = y)
# now use the hessian, too
sumsq.h <- function(x, y) diag(2, nrow = length(x))
nlminb(start = x0, sumsq, sumsq.g, sumsq.h,
lower = -1, upper = 1, y = y)
## Rest lifted from optim help page
fr <- function(x) { ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
grr <- function(x) { ## Gradient of 'fr'
x1 <- x[1]
x2 <- x[2]
c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
200 * (x2 - x1 * x1))
}
nlminb(c(-1.2,1), fr)
nlminb(c(-1.2,1), fr, grr)
flb <- function(x)
{ p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) }
## 25-dimensional box constrained
## par[24] is *not* at boundary
nlminb(rep(3, 25), flb, lower = rep(2, 25), upper = rep(4, 25))
## trying to use a too small tolerance:
r <- nlminb(rep(3, 25), flb, control = list(rel.tol = 1e-16))
stopifnot(grepl("rel.tol", r$message))
# }
Run the code above in your browser using DataLab
为什么下巴经常长痘痘hcv7jop9ns5r.cn | 西瓜和什么相克hcv7jop7ns4r.cn | 做噩梦被吓醒预示什么creativexi.com | 心血虚吃什么中成药hcv9jop3ns5r.cn | 青少年梦遗有什么危害hcv9jop3ns2r.cn |
眼睛痒什么原因520myf.com | 痛风挂什么科就医xinmaowt.com | 胃肠炎吃什么药好hcv9jop4ns2r.cn | 翅膀车标是什么车hcv9jop6ns3r.cn | 胃功能三项检查是什么hcv9jop0ns9r.cn |
左心房扩大是什么意思hcv9jop2ns4r.cn | 经常放屁什么原因hcv8jop5ns0r.cn | 什么钱最值钱hcv9jop1ns2r.cn | 三伏天吃什么水果好hcv9jop5ns9r.cn | 上不来气是什么原因hcv9jop5ns5r.cn |
硬卧是什么样子的luyiluode.com | 恐龙灭绝的原因是什么hcv9jop1ns0r.cn | 双鱼座是什么星象hcv7jop6ns4r.cn | 中性粒细胞百分比偏低是什么意思hcv9jop6ns6r.cn | 修复子宫内膜吃什么药hcv8jop6ns1r.cn |