Stu @ AGS
2009-Jun-17 20:17 UTC
[R] Curve fit a nonlinear equation with box constraints: success, many thanks!
Dear R Helper Community! Thank you for all your help and suggestions. For your reference and any future person searching the archives, here is the solution that did what I wanted it to do. As background, my goal was to find the coefficients for the following equation form: y ~ c1 * x1 * x2 ^ -c2 *x3 ^ c3 where y, x1, x2, and x3 are observed data, and c1, c2 and c3 are regression coefficients subject to the following constraints: 0.0 <= c2 <= 1.0 0.0 <= c3 <= 1.0 I ran the following code as a script from the R Gui on a windows machine. The command for this was: source("C:\\Users\\me\\R281\\work\\optimcall.R") # **** Script file: optimcall.R *********** # function definition rss <- function(par) { y <- c(0.111111111111111,0.0921052631578947,0.0564516129032258,0.0608108108108108 ,0.0128205128205128,0.0136078431372549) x1 <- c(0,0.978723404255319,0.087378640776699,0.549295774647887,0.0596026490066225 ,0.61578947368421) x2 <- c(1,3,4,5,6,7) x3 <- c(3600,169200,185400,255600,271800,342000) par1 <- par[1] par2 <- par[2] par3 <- par[3] ressq <- (y - par1 * (x1 + 1) * x2^(-par2) * x3^par3)^2 sum(ressq) } #call to optimizer opti <- optim(c(0.5, 0.5, 0.1), rss, gr = NULL, method = "L-BFGS-B", lower c(-Inf, 0, 0), upper = c(Inf, 1, 1) ) # ***** End script file ********* [[alternative HTML version deleted]]