Description Usage Arguments Examples
vpoptim uses vpack to let you call optim on cost functions involving multiple arguments. See examples for usage.
1 |
guess |
list of initial parameters |
cfun |
a cost function (the names of the arguments should be the *same ones* as in the "guess" argument) |
... |
passed on to optim |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #A cost function in two arguments:
cost <- function(a,b)
{
(3*a-b+2)^2
}
res <- vpoptim(list(a=1,b=0),cost)
res$par
##Won't work: the names in the list and the cost function need to be the same
try(res <- vpoptim(list(a=1,v=0),cost))
##Use optim for low-rank + diagonal approximation
X <- cov(USArrests)
cfun <- function(A,sigma2)
{
lrank <- tcrossprod(A,A) + sigma2*diag(nrow(A))
sum((X-lrank)^2)
}
guess <- list(A=matrix(0,4,2),sigma2=1)
opt <- vpoptim(guess,cfun)
opt$par
#Extra parameters are passed on to optim:
opt <- vpoptim(guess,cfun,method="BFGS")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.