Hi, here comes my problem, say I have the following functions (example case) #------------------------------------------------------------ function1 <- function (x, theta) {a <- theta[1] ( 1 - exp(-theta[2]) ) * theta[3] ) b <- x * theta[1] / theta[3]^2 return( list( a = a, b = b )) } #----------------------------------------------------------- function2<-function (x, theta) {P <- function1(x, theta) c <- P$a * x * exp(-theta[2]) d <- P$b * exp(x) q <- theta[1] / theta[3] res <- c + d + q; res} # Function to be optimized function3 <- function(theta1,theta2,theta3) { n <- length(data) -sum( function2(x = data[2:n], theta = c(theta1, theta2, theta3) ))} # 'data' is my input ts class object #-------------------------------------------------------------------------------------- Then I want to maximize function3 with respect to theta(s) (given some starting values) fit<-optim(par=c(theta1=1, theta2=1.2, theta3=.2), fn=function3) I get the following: Error in function1(x, theta) : argument "theta2" is missing, with no default Where I made a mistake? I will appreciate any help ... r -- View this message in context: http://www.nabble.com/optimization-setup-tp16825410p16825410.html Sent from the R help mailing list archive at Nabble.com.
On Wed, 23 Apr 2008, threshold wrote:> Hi, here comes my problem, say I have the following functions (example > case) #------------------------------------------------------------ > function1 <- function (x, theta) > {a <- theta[1] ( 1 - exp(-theta[2]) ) * theta[3] ) > b <- x * theta[1] / theta[3]^2 > return( list( a = a, b = b )) } > #----------------------------------------------------------- > function2<-function (x, theta) > {P <- function1(x, theta) > c <- P$a * x * exp(-theta[2]) > d <- P$b * exp(x) > q <- theta[1] / theta[3] > res <- c + d + q; res} > > # Function to be optimized > function3 <- function(theta1,theta2,theta3) { > n <- length(data) > -sum( function2(x = data[2:n], theta = c(theta1, theta2, theta3) ))} > # 'data' is my input ts class object > #-------------------------------------------------------------------------- >------------ > > Then I want to maximize function3 with respect to theta(s) (given some > starting values) > > fit<-optim(par=c(theta1=1, theta2=1.2, theta3=.2), fn=function3) > > I get the following: > Error in function1(x, theta) : > argument "theta2" is missing, with no default > > Where I made a mistake? I will appreciate any help ... > > rYour function to be optimised must be a function of a single parameter (which may be a vector). So you should have something like: # Function to be optimized function3 <- function(thetas) { n <- length(data) -sum( function2(x = data[2:n], theta = thetas)) } [Not tested, your provided code has typos]. Ray
The error comes from the way you specify the parameters: At least not as elegant, but it works: function4 <- function(x){ theta1 <- x[1] theta2 <- x[2] theta3 <- x[3] function3(theta1,theta2,theta3) } fit<-optim(par=c(1, 1.2, .2), fn=function4) Bart threshold wrote:> > Hi, here comes my problem, say I have the following functions (example > case) > #------------------------------------------------------------ > function1 <- function (x, theta) > {a <- theta[1] ( 1 - exp(-theta[2]) ) * theta[3] ) > b <- x * theta[1] / theta[3]^2 > return( list( a = a, b = b )) } > #----------------------------------------------------------- > function2<-function (x, theta) > {P <- function1(x, theta) > c <- P$a * x * exp(-theta[2]) > d <- P$b * exp(x) > q <- theta[1] / theta[3] > res <- c + d + q; res} > > # Function to be optimized > function3 <- function(theta1,theta2,theta3) { > n <- length(data) > -sum( function2(x = data[2:n], theta = c(theta1, theta2, theta3) ))} > # 'data' is my input ts class object > #-------------------------------------------------------------------------------------- > > Then I want to maximize function3 with respect to theta(s) (given some > starting values) > > fit<-optim(par=c(theta1=1, theta2=1.2, theta3=.2), fn=function3) > > I get the following: > Error in function1(x, theta) : > argument "theta2" is missing, with no default > > Where I made a mistake? I will appreciate any help ... > > r >-- View this message in context: http://www.nabble.com/optimization-setup-tp16825410p16833769.html Sent from the R help mailing list archive at Nabble.com.