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

Huntsinger, Reid reid_huntsinger at merck.com
Wed Jul 16 20:09:58 CEST 2003


R is good at automating specific kinds of complex loops, namely those that
can be vectorized, or that can be written to draw on otherwise built-in
facilities. It's usually reasonable for other kinds of loops but not
spectacularly fast. You can write this part in C, though, quite easily, and
R provides very convenient utilities for this.

As for your code: You seem to have a system of equations that relates W and
Hg to their one-period-ago values. It might clarify things if you coded this
as a function: input time t values and q, output time t + 1 values. (You
wouldn't need any arrays.) Then f would just iterate this function and
calculate the criterion.

Does the trajectory of (W, Hg) for given q in R seem correct? Does it agree
with Excel? What does the criterion function look like? You could plot it in
R and perhaps see if the surface is complicated, in which case a simple grid
search might work for you.

Reid Huntsinger





-----Original Message-----
From: Michael Rennie [mailto:mrennie at utm.utoronto.ca] 
Sent: Wednesday, July 16, 2003 11:18 AM
To: Spencer Graves
Cc: R-Help; M.Kondrin
Subject: Re: [R] Excel can do what R can't?????



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
> 
> 


-- 
Michael Rennie
M.Sc. Candidate
University of Toronto at Mississauga
3359 Mississauga Rd. N.
Mississauga ON  L5L 1C6
Ph: 905-828-5452  Fax: 905-828-3792

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, ...{{dropped}}




More information about the R-help mailing list