Search code examples
rsum

Sum and product over interval in R


I am trying to implement the following simple formulas in R:

  • Formula 1:

enter image description here

I have no idea how to implement in R the product operator when the limits of the interval are very large (e.g. value of the upper limit = 10,000 instead of 5)

  • Formula 2

enter image description here

Example input for second formula (in reality, the dimension of the interval S is much much bigger)

 S = list(c(1,0,0), c(0,1,0), c(0,0,1))
 X = c(1,2,3)

Solution

  • For the first formula it has been mentioned that it is better to do this on the log scale, if your true values of x are near 0 then the log1p function may be of help.

    In general for these types of problems you can use lapply or sapply to compute the pieces that need to be multiplied or summed (or whatever), then use sum or prod to sum, multiply. If you want to collapse/combine the values with an operator that does not have a nice function like sum or prod then use the Reduce function.