I need to repeat a function many times, with differing parameters held constant across iterations. To accomplish this, I would like to create a list (or vector) of parameters, and then insert that list into the function. For example: q<-("l,a,b,s") genericfunction<-function(q){ } ###### The equivalent code would of course be genericfunction<-function(l,a,b,s){ } Any help or suggestions would be much appreciated. -- View this message in context: r.789695.n4.nabble.com/Pasting-a-list-of-parameters-into-a-function-tp4656445.html Sent from the R help mailing list archive at Nabble.com.
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of bsm2 > Sent: 23 January 2013 20:00 > To: r-help at r-project.org > Subject: [R] Pasting a list of parameters into a function > > I need to repeat a function many times, with differing > parameters held constant across iterations. To accomplish > this, I would like to create a list (or vector) of > parameters, and then insert that list into the function. > > For example: > > q<-("l,a,b,s")This isn't a ist, it's a single quoted string. A list would be q <- list(l=<value>,a=<value>, b=<value>, s=<value> ) (with common sense replacement of <value> with the values you want to pass> genericfunction<-function(q){ > }That'll work, assuming your function dereferences q, for example using q$l or, perhaps, using with() Other things might work too though. For example, assuming each item is single-valued numeric, set up a matrix with columns l, a, b, s or or data frame (say d) with variables l, a, b, s, then use apply(d, 1, genericfunction) #iterate over rows, coercing each row to vector with g using either the numeric indices (like q[1], q[2]) or, for the data frame version, the names (q['a'] etc) because in the data frame version q will be a named vector. You could also construct a list of lists, and use lapply or sapply to do the iteration over the whole list; that would work whatever type l, a, b, s are. Example: param.sets <- list( list(l=1,a='a', b=2.3, s=TRUE ), list(l=5,a='p', b=7.5, s=FALSE ), list(l=3,a='r', b=2.5, s=TRUE ) ) a.function <- function(q) with(q, paste(l, a, b, s) ) sapply(param.sets, a.function) You could also use mapply and supply l, a etc as individual vectors, factors etc, but that would need a different function definition: d <- data.frame(l=1:6, a=gl(3,2, labels=letters[1:3]), b=6:1, s=rnorm(6)) #to package them neatly mfun <- function(l, a, b, s) paste(l, a, b, s) #Then with(d, mapply(mfun, l, a, b, s)) ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Prof J C Nash (U30A)
2013-Jan-25 13:57 UTC
[R] Pasting a list of parameters into a function
Quite a long time ago, there was a thread about generalized eigenvalues, which ended inconclusively. tolstoy.newcastle.edu.au/R/help/05/06/6832.html For students, a good proposal for the Google Summer of Code (gsoc-r) would be a nice interface to things like the QZ algorithm and similar extended numerical linear algebra. I'm willing to mentor that, and am sure there would be someone else to back me up on it. But it needs a GOOD proposal. JN On 13-01-25 06:00 AM, r-help-request at r-project.org wrote:> Message: 39 > Date: Thu, 24 Jan 2013 10:37:07 -0800 > From: Jeff Newmiller<jdnewmil at dcn.davis.CA.us> > To: hp wan<huaping.wan at gmail.com>,r-help at r-project.org > Subject: Re: [R] Pasting a list of parameters into a function > Message-ID:<3ad1a172-d00c-43a5-b949-20873debd898 at email.android.com> > Content-Type: text/plain; charset=UTF-8 > > The eigenvalue problem is not unique to R. This is an R mailing list. What is your question about R? > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > hp wan<huaping.wan at gmail.com> wrote: > >> >Hi mailing listers, >> > >> >Sorry, I made a little mistake in the previous mail. B^{1} should be >> >B^{-1}. >> > >> >Is there certain function in R deal with how to compute generalized >> >eigenvalues, that is the problem: A*x* = ?B*x *? When I use >> >eigen(B^{-1}*A), error happened. It displays there are many Inf >> >elements in >> >B^{-1}. >> > >> >Thanks >> > >> >Huaping Wan >> > >> > >> > >> >2013/1/25 hp wan<huaping.wan at gmail
> -----Original Message----- > I'd love to write a code that would allow me replace the example code: > > fit1F <- mle2(LL, fixed=list(xhalf=6)) > > with something like: > > var<-xhalf > val<-6 > > fit1F <- mle2(LL, fixed=list(var=val)) > > or > > var<-c("xhalf","=") > val<-6 > > fit1F <- mle2(LL, fixed=list(var,val)) > > Replacing the value in question is easy enough, but I can't > figure out exactly how to replace the identity of the > variable that will be held constant.I'm not sure exactly _where_ in the code you need to replace things. If it's at the time you call your code (presumably a function) manually, the answer's kind of obvious; write a function that takes a 'fixed' argument and pass that directly to mle2. If you want to cycle through a set of fixed values, though, you could probably do that by starting with the complete list and then cyclyng through it. A very simple example might look like this: show.fixed <- function(i, l, a, b, s, fixed) { #A simple function that takes a 'fixed' parameter in the same format as mle2's 'fixed'; cat(paste(i, ":", l, a, b, s, "with", names(fixed), "fixed at", fixed[[1]], "\n")) #This function just prints out the values provided #followed by the name and fixed value of the 'fixed' element } #To pass different things to such a function: #i) Set up a complete list of parameters with their fixed values fixed.scope <- list(l=1, a=3, b=5, s=23) #This could be your starting parameter set if that's convenient #ii) reference elements of the list inside a loop # I've out such a loop inside the following function: f <- function(l=0, a=1, b=2, s=3, fs) { #fs is our complete set of possible fixed values for(i in 1:4) { show.fixed(i, l, a, b, s, fixed=fs[i]) #you would have mle2 called here instead of show.fixed # fs[i] sends the i'th element of fs to the function as a one-element list with the right name } } # iii) call the function with the 'fixed scope' list f(fs=fixed.scope) #shows that setting fixed=fs[i] provides the function with the (named) i'th element of the list of fixed parameters Clearly* you could update the values actually held in fs at each cycle if what you wanted to do was optimise three parameters for different values of each fixed element, or (somewhat dodgy, but I've had to resort to such things with ill-behaved optimisations) do 'three at a time' optimisation to sneak up on a maximum. You could also build fs inside your function instead of supplying it from outside. Steve Ellison *'clearly' in the usual mathematical sense of "after several years of study and considerable thought one might see that..." ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}