[R] optim "a log-likelihood function"
Christian Schulz
ozric at web.de
Thu Sep 30 11:09:20 CEST 2004
dear optim experts,
optim(c(1,1),LL,control=list(fnscale=-1),trans=trans,times=times)
Error in optim(c(1, 1), LL, control = list(fnscale = -1), trans = trans, :
Function cannot be evaluated at initial parameters
Finally i would constraint r and alpha > than a small positive number?
optim(c(1,1),LL,lower=c(0.0001,0.001),upper=Inf,method="L-BFGS-B",control=list(fnscale=-1),trans=trans,times=times)
Error in optim(c(1, 1), LL, lower = c(0.001, 0.001), upper = Inf, method =
"L-BFGS-B", :
L-BFGS-B needs finite values of fn
where are my mistakes?
Many thanks, christian
########## The data and function
dm <- subset(dm09,select=c(ID,TRANS,TIMES))
trans <- dm$TRANS
> length(trans)
[1] 269732
times <- dm$TIMES
> length(times)
[1] 269732
LL <- function(x,trans,times){
r <- x[1]
alpha <- x[2]
l0 <-
exp(lgamma(r+trans)-lgamma(r))/gamma(trans+1)*(alpha/(alpha+times))**r*(times/(alpha+times))**trans
l <- log(l0)
sum(l)
}
Am Mittwoch, 29. September 2004 19:41 schrieb Sundar Dorai-Raj:
> 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.
>
> 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