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

Spencer Graves spencer.graves at pdf.com
Wed Jul 16 17:45:10 CEST 2003


1.  If I have an answer that works, I typically go to something else.

2.  If your Excel solution is still inadequate, have you considered 
trying to modularize your function "f", so "f" is 5-10 lines, several of 
which call other functions, f1, f2, ..., f6, say, and each of these do a 
piece of the computations that can be checked by comparison with 
intermediate results in Excel.

hope this helps.  spencer graves

Michael Rennie wrote:
> Hi, Spencer
> 
> I know I submitted a beastly ammount of code, but I'm not sure how to simplify 
> it much further, and still sucessfully address the problem that i am having.  
> The reason being is that the funciton begins
> 
> f<- function (q) 
> 
> At the top of the iterative loop.  This is what takes q and generates Wtmod, 
> Hgtmod at the end of the iterative loop. the assignment to f occurs at the 
> bottom of the iterative loop. So, yes, the call to f is performing an immediate 
> computation, but based on arguments that are coming out of the iterative loop 
> above it, arguments which depend on q<-(p, ACT).  Maybe this is the problem; 
> I've got too much going on between my function defenition and it's assignment, 
> but I don't know how to get around it.
> 
> So, I'm not sure if your example will work- the output from the iterative 
> process is Wtmod, Hgtmod, and I want to minimize the difference between them 
> and my observed endpoints (Wt, Hgt).  The numbers I am varying to reach this 
> optimization are in the iterative loop (p, ACT), so re-defining these outputs 
> as x's and getting it to vary these doesn't do me much good unless they are 
> directly linked to the output of the iterative loop above it.  
> 
> Last, it's not even that I'm getting error messages anymore- I just can't get 
> the solution that I get from Excel.  If I try to let R find the solution, and 
> give it starting values of c(1,2), it gives me an optimization sulution, but an 
> extremely poor one.  However, if I give it the answer I got from excel, it 
> comes right back with the same answer and solutions I get from excel.  
> 
> Using the 'trace' function, I can see that R gets stuck in a specific region of 
> parameter space in looking for the optimization and just appears to give up.  
> Even when it re-set itself, it keeps going back to this region, and thus 
> doesn't even try a full range of the parameter space I've defined before it 
> stops and gives me the wrong answer.
> 
> I can try cleaning up the code and see if I can re-submit it, but what I am 
> trying to program is so parameter heavy that 90% of it is just defining these 
> at the top of the file.
> 
> Thank you for the suggestions,
> 
> Mike
>   
> 
> Quoting Spencer Graves <spencer.graves at PDF.COM>:
> 
> 
>>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