Zhen Pang
2004-Sep-30 02:57 UTC
Vectorising and loop (was Re: [R] optim "a log-likelihood function")
>From: Sundar Dorai-Raj <sundar.dorai-raj at PDF.COM> >Reply-To: sundar.dorai-raj at PDF.COM >To: Zhen Pang <nusbj at hotmail.com> >CC: r-help at stat.math.ethz.ch >Subject: Vectorising and loop (was Re: [R] optim "a log-likelihood >function") >Date: Wed, 29 Sep 2004 18:21:17 -0700 > > > >Zhen Pang wrote: > >> >>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 >> > >Zhen, > Your question doesn't really have much to do with optim, so I changed >the subject line. > >It's difficult to see what you're trying to accomplish without a complete >example. Could you post one? Also, for loops are necessarily bad. > >One thing to note is that you're better off using lchoose in the above >code. I.e. > >log.s <- lchoose(j, i) + lchoose(k - j, l - i) - lchoose(k, l) >s[l] <- exp(log.s) > >--sundar > >I have a matrix of 200 by 2, namely z, the columns are i and j. K is the maximum of j. x0 is a k dimension vector. ss<-rep(0,200) for (m in 1:200) {i<-z[m,1] j<-z[m,2] l<-i:(k-j+i) s<-rep(0,k) s[l]<-choose(j,i)*choose((k-j),(l-i))/choose(k,l) # we only define some of s to be non-zero, since dim(l) might be smaller than dim(s) ss[m]<-sum(s*x0) # ss[m] is a weighted sum of x0 } sum(log(ss)) How to avoid the loop of m? I tried to define i<-z[,1] and j<-z[,2], but failed. Thanks. Regards, Zhen
Gabor Grothendieck
2004-Sep-30 04:18 UTC
Vectorising and loop (was Re: [R] optim "a log-likelihood function")
Zhen Pang <nusbj <at> hotmail.com> writes:
:
: >From: Sundar Dorai-Raj <sundar.dorai-raj <at> PDF.COM>
: >Reply-To: sundar.dorai-raj <at> PDF.COM
: >To: Zhen Pang <nusbj <at> hotmail.com>
: >CC: r-help <at> stat.math.ethz.ch
: >Subject: Vectorising and loop (was Re: [R] optim "a log-likelihood
: >function")
: >Date: Wed, 29 Sep 2004 18:21:17 -0700
: >
: >
: >
: >Zhen Pang wrote:
: >
: >>
: >>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
: >>
: >
: >Zhen,
: > Your question doesn't really have much to do with optim, so I
changed
: >the subject line.
: >
: >It's difficult to see what you're trying to accomplish without a
complete
: >example. Could you post one? Also, for loops are necessarily bad.
: >
: >One thing to note is that you're better off using lchoose in the above
: >code. I.e.
: >
: >log.s <- lchoose(j, i) + lchoose(k - j, l - i) - lchoose(k, l)
: >s[l] <- exp(log.s)
: >
: >--sundar
: >
: >
: I have a matrix of 200 by 2, namely z, the columns are i and j. K is the
: maximum of j.
:
: x0 is a k dimension vector.
:
: ss<-rep(0,200)
: for (m in 1:200)
: {i<-z[m,1]
: j<-z[m,2]
: l<-i:(k-j+i)
: s<-rep(0,k)
: s[l]<-choose(j,i)*choose((k-j),(l-i))/choose(k,l) # we only define some
: of s to be non-zero, since dim(l) might be smaller than dim(s)
: ss[m]<-sum(s*x0) # ss[m] is a weighted sum of x0
: }
: sum(log(ss))
:
: How to avoid the loop of m? I tried to define i<-z[,1] and j<-z[,2], but
: failed. Thanks.
When Sundar was referring to a *complete* example he was referring to
both code and data -- something that someone else could simply copy
from your post, paste it into an R session and get the answer as per
the posting guide.
At any rate, if f(x) is the function which takes z[m,] as its single
argument and returns ss[m] then the above is equivalent to:
sum(log(apply(z, 1, f)))
Not sure if that really qualifies as getting rid of the loop but it
does get rid of the for and the initialization of ss.