Laurence,
The easiest way to do this is to pass the second (or any other) argument
directly to optim, but not as a parameter to be optimized over. A
(hopefully) helpful example:
#
# define function (one parameter mu, 2 data vectors)
fkt <- function(mu,x,y){min((y-x*mu)^2)}
# create data
x <- rnorm(20)
y <- rnorm(20)
# optimize wrt to parameter, pass x and y as data
out <- optim(.5,fkt,x=x,y=y,method="BFGS",control=list(trace=T,REPORT=1))
initial value 0.006378
iter 2 value 0.000183
iter 3 value 0.000000
iter 3 value 0.000000
iter 3 value 0.000000
final value 0.000000
converged
out
$par
[1] 0.444578
Hope this helps. Notice: Since x and y are passed optim recognizes that the
optimization must be wrt mu (the missing argument for which no data is
supplied).
Jens
From: gov2001-l-bounces at
lists.fas.harvard.edu
[mailto:gov2001-l-bounces at
lists.fas.harvard.edu] On Behalf Of Laurence Tai
Sent: Wednesday, March 05, 2008 5:41 PM
To: gov2001-l at
lists.fas.harvard.edu
Subject: [gov2001-l] using optim for functions with multiple arguments
I am curious to know what, if any, the syntax for optim is if I have a
function with multiple arguments and I want to maximize over just the first
argument. My previous attempts have resulted in program errors.
Thanks,
Laurence