CrishaD
CrishaD

Reputation: 51

Double summation in R

I have to implement this summation in R:

enter image description here

I have tried to implement the first exponential in the following way:

f <- rep(0,48)
for(i in 1:48) {
f[i] <- exp(-sum(lambda[which(lambda<lambda[i])])-(i-1))}

where 48 is the length of lambda. I think this expression looks ok, but my problem now is..How can I add the second summation? I don't have any idea because of the double indeces. Do you have any suggestion? Thank you.

Upvotes: 1

Views: 705

Answers (1)

YCR
YCR

Reputation: 4002

I am not sure about a few parenthesis in your formula, but something like that should do the trick:

exp(-sum(lambda[20:j]) + (j-20)) * sum(unlist(lapply(20:j, function(i) lambda[i] * exp(sum(lambda[20:i]) + i-20) )))

In R, it is easier to use vectorisation than loop.

Upvotes: 1

Related Questions