Skip to content

Pasting a list of parameters into a function

8 messages · bsm2, S Ellison, hp wan +2 more

#
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: http://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.
#
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
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}}
#
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 =
"Nelder-Mead",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 re-run 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 =
"Nelder-Mead",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 =
"Nelder-Mead",control = list(maxit = 50000))

####

I tried the previously suggested solutions and couldn't get them to work.  I
apologize if I'm being dense!



--
View this message in context: http://r.789695.n4.nabble.com/Pasting-a-list-of-parameters-into-a-function-tp4656445p4656521.html
Sent from the R help mailing list archive at Nabble.com.
#
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 log-likelihood function at all.  

S Ellison


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped: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:

            
#
On 24-01-2013, at 18:56, hp wan <huaping.wan at gmail.com> wrote:

            
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 R-forge 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.