Re: [R] Pasting a list of parameters into a function

2013-01-25 Thread bsm2
Thanks for taking to time to help me out with this, I really appreciate it!



Quoting "S Ellison-2 [via R]" :

>
>
>> -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}}
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
> ___
> If you reply to this email, your message will be added to the  
> discussion below:
> http://r.789695.n4.nabble.com/Pasting-a-list-of-parameters-into-a-function-tp4656445p4656633.html
>
> To unsubscribe from Pasting a list of parameters into a function,  
> visit  
> http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4656445&code=YnNtMkByaWNlLmVkdXw0NjU2NDQ1fC0xMzgzNTc2NDc=
>
> !DSPAM:3889,5102a1e8165101603916553!
>







--
View this message in context: 
http://r.789695.n4.nabble.com/Pasting-a-list-of-parameters-into-a-function-tp4656445p4656660.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Pasting a list of parameters into a function

2013-01-24 Thread bsm2
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 = 5))


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 = 5))

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 = 5))



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.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Pasting a list of parameters into a function

2013-01-23 Thread bsm2
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.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.