[R] optim "a log-likelihood function"
Zhen Pang
nusbj at hotmail.com
Thu Sep 30 03:00:48 CEST 2004
>From: Sundar Dorai-Raj <sundar.dorai-raj at pdf.com>
>Reply-To: sundar.dorai-raj at pdf.com
>To: Christian Schulz <ozric at web.de>
>CC: r-help at stat.math.ethz.ch
>Subject: Re: [R] optim "a log-likelihood function"
>Date: Wed, 29 Sep 2004 10:41:45 -0700
>
>
>
>Christian Schulz wrote:
>
>>Hello,
>>
>>i know that i have to use optim, but i'm confused how its
>>possible maximize the sum over all l[i] and get the optimized
>>max(LL), r and alpha?
>>
>>LL <- function(trans,time){
>> for(i in 1:length(trans){
>> l[i] <- log(lgamma(r+trans[i] -
>>gamma(r+1)*(alpha/alpha+t[i]))**r)*(t[i]/alpha+t[i]))**trans[i]
>>}
>>return(sum(l))
>> }
>>
>>i'm confused how i have to set r and alpha and i found no related help in
>>archives?
>>
>>...in Excel it works with solver but only for ~65.000 rows :-)
>>
>>#This notation is 1 for trans and 1 for time instead the Startvalues for
>>r and alpha?
>>
>
>I'm not sure what the above statement means, so I may have misinterpretted
>what you are trying to accomplish.
>
>>optim(c(1,1),-LL)
>>
>>many thanks for an easy example or hint regards,christian
>>
>
>Did you look at the first example in ?optim? There also numerous errors in
>LL: missing parans, time is not used, t is undefined in the function.
>
>LL <- function(x, trans, time) {
> r <- x[1]
> alpha <- x[2]
> ...
> sum(l)
>}
>
>optim(c(1, 1), LL, control = list(fnscale = -1),
> trans = trans, time = time)
>
>Some style issues:
>1. Break up lines that run too long, especially if you expect others to
>read your code.
>2. You don't need an explicit "return" at the end of a function.
>3. You should remove the "for" loop in LL and vectorise "l", which should
>be easy.
>
I also use optim, however, for my case, can you show some light on avoiding
the loop?
There are around 200 sets of (i,j,k) where i<=j<=k. 3 situations exist
whether "=" hold, I list one for example,
l<-i:(k-j+i)
s<-rep(0,k)
s[l]<-choose(j,i)*choose((k-j),(l-i))/choose(k,l)
ss<-sum(s*x0)
then sum all the log(ss) is my log-liklihood function.
One loop from 1 to 200 is inevitable. I have tried to use vector, however, I
only can simply to this situation. Thanks.
Regards,
Zhen
>Hope this is helpful,
>
>--sundar
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide!
>http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list