

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.


> Original Message
> From: [hidden email]
> [mailto: [hidden email]] On Behalf Of bsm2
> Sent: 23 January 2013 20:00
> To: [hidden email]
> 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 singlevalued 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}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


I'm still having trouble, I think that I may have poorly explained the problem I'm running into.
The code I'm working with looks like this:
mle.nb<function(l,a,b,s){
t1pred=(data$T*l)/(1+a*data$T)^b
sum(dnbinom(x=data$T.1, mu=t1pred, size=s,log=TRUE))
}
fit.mle.nb=mle2(mle.nb, start=list(l=2, a=1, b=1, s=1), method = "NelderMead",control = list(maxit = 50000))
I'm trying to iterate model fits while holding one variable (l,a,b or s) constant. In order to do this, each time I need to rerun the mle2 fit with one variable held constant, for example:
l=1
mle.nb<function(a,b,s){
t1pred=(data$T*l)/(1+a*data$T)^b
sum(dnbinom(x=data$T.1, mu=t1pred, size=s,log=TRUE))
}
fit.mle.nb=mle2(mle.nb, start=list(a=1, b=1, s=1), method = "NelderMead",control = list(maxit = 50000))
I would like to be able to rewrite this code as something like:
q<("l,a,b,s") #or something like this, I'm not sure if this should be formatted as a data frame or something else
mle.nb<function(q){
t1pred=(data$T*l)/(1+a*data$T)^b
sum(dnbinom(x=data$T.1, mu=t1pred, size=s,log=TRUE))
}
fit.mle.nb=mle2(mle.nb, start=list(a=1, b=1, s=1), method = "NelderMead",control = list(maxit = 50000))
####
I tried the previously suggested solutions and couldn't get them to work. I apologize if I'm being dense!


> I'm trying to iterate model fits while holding one variable
> (l,a,b or s) constant. In order to do this, each time I need
> to rerun the mle2 fit with one variable held constant,
Have you read the help page closely enough, I wonder?
If I look at the mle2 help page, I see an argument called 'fixed' described as "Named list. Parameter values to keep fixed during optimization."
Looking at the first example, i see an example of that in the line
fit1F < mle2(LL, fixed=list(xhalf=6))
If I run that, I see mle2 use xhalf as a fixed parameter, iterating only over the remaining parameter, y
That looks awfully like what you seem to want, with no change to your negative loglikelihood function at all.
S Ellison
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Hi mailing listers,
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
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 < [hidden email]>
> Hi mailing listers,
>
> 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
>
>
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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:< [hidden email]> 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 < [hidden email]> 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 < [hidden email]>
>
>> Hi mailing listers,
>>
>> 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
>>
>>
>>
>
> [[alternative HTML version deleted]]
>
>
>
>
>
>______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp>PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html>and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On 24012013, at 18:56, hp wan < [hidden email]> 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}.
You should not reply to a message and change the subject.
Create a new thread with you subject.
Did you literally do B^{1}? That doesn't calculate the inverse of B.
In addition Matrix * Matrix does elementwise multiplication. You should have used %*%.
You could try eigen(solve(B,A)). Not the best way but it might be sufficient for your purpose.
There appears to be package imad on Rforge that has an interface to Lapack's dggev.
Berend
Please do not send html mails. And don't boldify things. In plain text 8 will be inserted rendering your text possibly incomprehensible.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Quite a long time ago, there was a thread about generalized eigenvalues,
which ended inconclusively.
http://tolstoy.newcastle.edu.au/R/help/05/06/6832.htmlFor students, a good proposal for the Google Summer of Code (gsocr)
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 130125 06:00 AM, [hidden email] wrote:
> Message: 39
> Date: Thu, 24 Jan 2013 10:37:07 0800
> From: Jeff Newmiller< [hidden email]>
> To: hp wan< [hidden email]>, [hidden email]
> Subject: Re: [R] Pasting a list of parameters into a function
> MessageID:< [hidden email]>
> ContentType: text/plain; charset=UTF8
>
> 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:< [hidden email]> 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< [hidden email]> 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@gmail
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


> 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 oneelement 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 illbehaved 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}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Thanks for taking to time to help me out with this, I really appreciate it!
Quoting "S Ellison2 [via R]" < [hidden email]>:
>
>
>> 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
> oneelement 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 illbehaved 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}}
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the
> discussion below:
> http://r.789695.n4.nabble.com/Pastingalistofparametersintoafunctiontp4656445p4656633.html>
> To unsubscribe from Pasting a list of parameters into a function,
> visit
>
> !DSPAM:3889,5102a1e8165101603916553!
>

