[R] About Passing Arguments to Function

markleeds at verizon.net markleeds at verizon.net
Fri May 23 03:54:01 CEST 2008


  to add to what Haris said: I think you are trying to find the mle's of 
the gamma, given some data,  and either what you're doing is not the 
right way to do it  or maybe it's an alternative way to go about it that 
i am unfamilar with. ?

i don't spend any  time maximizing likelihoods but roger peng has a very 
nice example of how one can go about it using optim. i suggest that you 
take a look at the attached document and then write out the likelihood 
for the gamma similar to how i wrote it out for the poisson the other 
day in the private email that i sent you.  in fact, the likelihood for 
the is probably in any decent math stat book like casella and berger 
etc. if you have johnson and kotz, it's definitely in there. if i had it 
here, i would just tell you what it was.







On Thu, May 22, 2008 at  9:36 PM, Charilaos Skiadas wrote:

> On May 22, 2008, at 9:14 PM, Edward Wijaya wrote:
>
>> Hi,
>>
>> Below I have a function mlogl_k,
>> later it's called with "nlm" .
>>
>> __BEGIN__
>> vsamples<- c(14.7, 18.8, 14, 15.9, 9.7, 12.8)
>>
>> mlogl_k <- function( k_func, x_func, theta_func, samp) {
>>      tot_mll <- 0
>>      for (comp in 1:k_func) {
>>        curr_mll <- (- sum(dgamma(samp, shape = x_func,
>> scale=theta_func, log = TRUE)))
>>        tot_mll <- tot_mll + curr_mll
>>      }
>>
>>      tot_mll
>> }
>>
>> # Calling the function above
>> mlogl_out <- nlm(mlogl_k, mean(vsamples), k_func =2, x_func = 1,
>> theta_func = 1, samp=vsamples)
>>
>> __END__
>>
>> I thought under NLM, I already assign
>> the parameter correctly.
>> However it gives me the following error.
>>
>> Error in f(x, ...) : unused argument(s) (14.3166666666667)
>> Calls: nlm -> <Anonymous> -> f
>> Execution halted
>>
>>
>> What's wrong with my code above?
>
> nlm passes the following values over to mlogl_k: mean(vsamples), 
> k_func =2, x_func = 1, theta_func = 1, samp=vsamples
>
> Now, f is called with these arguments, and all its arguments are 
> matched by the named arguments. Then it does not know what to do with 
> the "mean(vsamples)" argument. Hence the error: f is called with one 
> more argument than it can handle.
>
> It is not clear with respect to what variables you want the 
> minimization. Right now all the named variables are treated as 
> constants, and there is no variable left with respect to which mlogl_k 
> is supposed to be maximized. Read the documentation for nlm carefully: 
> Your function should have one more argument (like the alpha in the 
> question you asked yesterday).
>
>> - Edward
>
> Haris Skiadas
> Department of Mathematics and Computer Science
> Hanover College
>
> ______________________________________________
> R-help at 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: r-classes-scope.pdf
Type: application/pdf
Size: 104274 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080522/010faed7/attachment.pdf>


More information about the R-help mailing list