[R] Excel can do what R can't?????

Spencer Graves spencer.graves at pdf.com
Wed Jul 16 15:29:22 CEST 2003


The phrase:

    f <- 1000000000*(((((Wt-Wtmod)^2)/Wt) + (((Hgt-Hgtmod)^2)/Hgt))2) ; f

is an immediate computation, not a function.  If you want a function, 
try something like the following:

    f <- function(x){
	  Wt <- x[1]
	  Wtmod <- x[2]
	  Hgt <- x[3]
	  Hgtmod <- x[4]
      1000000000*(((((Wt-Wtmod)^2)/Wt) + (((Hgt-Hgtmod)^2)/Hgt))2)
    }

OR

    f <- function(x, X){
	  Wt <- X[,1]
	  Hgt <- X[,2]
	  Wtmod <- x[1]
	  Hgtmod <- x[2]
	1000000000*(((((Wt-Wtmod)^2)/Wt) + (((Hgt-Hgtmod)^2)/Hgt))2)
    }

"par" in "optim" is the starting values for "x".  Pass "X" to "f" via 
"..." in the call to "optim".

	  If you can't make this work, please submit a toy example with the 
code and error messages.  Please limit your example to 3 observations, 
preferably whole numbers so someone else can read your question in 
seconds.  If it is any longer than that, it should be ignored.

hope this helps.
Spencer Graves

M.Kondrin wrote:
>  >?optim
> 
> optim(par, fn, gr = NULL,
>            method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"),
>            lower = -Inf, upper = Inf,
>            control = list(), hessian = FALSE, ...)
> 
> .....
>       fn: A function to be minimized (or maximized), with first
>           argument the vector of parameters over which minimization is
>           to take place. It should return a scalar result.
> 
> Your fn defined as:
> f <- 1000000000*(((((Wt-Wtmod)^2)/Wt) + (((Hgt-Hgtmod)^2)/Hgt))2) ; f
> What is its first argument I wonder?
> I think you have just an ill-defined R function (although for Excel it 
> may be OK - do not know) and optim just chokes on it.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list