# [R] Sum up the values of a function

From: Stefan Wagner <Stef.Wagner_at_students.uni-mannheim.de>
Date: Fri 10 Jun 2005 - 21:08:53 EST

I have to do a maximum-likelihood estimation and have now a problem concerning how to sum up my function values in a smart way. I don't know how to explain it easyly, so I give you the code and show you where my problem is.
I have shorten the code a little bit, so that you only get the necessary facts:

```ws12 <- function (z, i) (1/(1+exp(z*(z-x1[i]- z*(m1[i]/n1[i]-0.5)))))
ws37 <- function (z, i) (1/(1+exp(z*(z-x2[i]- z*(m2[i]/n2[i]-0.5)))))
wsAttack12 <- function (z,i) (ws12(z,i)*dec1[i]+(1-ws12(z,i))*(1-dec1[i]))
wsAttack37 <- function (z,i) (ws37(z,i)*dec2[i]+(1-ws37(z,i))*(1-dec2[i]))
```
logwsAttack12 <- function (z,i) (log(wsAttack12(z,i))) logwsAttack37 <- function (z,i) (log(wsAttack37(z,i))) ws12sum <- function (z)
(logwsAttack12(z,i=1)+logwsAttack12(z,i=2)+logwsAttack12(z,i=3)+logwsAttack12(z,i=4)+logwsAttack12(z,i=5)+logwsAttack12(z,i=6))
ws37sum <- function (z)
(logwsAttack37(z,i=1)+logwsAttack37(z,i=2)+logwsAttack37(z,i=3)+logwsAttack37(z,i=4)+logwsAttack37(z,i=5)+logwsAttack37(z,i=6)+logwsAttack37(z,i=7)+logwsAttack37(z,i=8))
wsLOG <- function (z) (ws12sum(z) + ws37sum(z)) LogSum <- function (z) (-sum(wsLOG(z)))
SP <- c(0.16, 0.10, 44, 0.80, 46)
out <- nlm (LogSum, p=SP)
out

Best regards,

Stefan Wagner

R-help@stat.math.ethz.ch mailing list