# Re: [R] Sum up the values of a function

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Fri 10 Jun 2005 - 21:36:34 EST

Hi

?aggregate
or
?by

BTW, do you have some trouble with your space bar?

HTH Petr

On 10 Jun 2005 at 13:08, Stefan Wagner wrote:

> Dear R-Users,
>
> 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)+logwsA
> ttack12(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)+logwsA
> ttack37(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
>
> For explanation: x1[i], x2[i], m1[i], m2[i], n1[i], n2[i] are given
> data and z[1:5] are my estimates. My problem is that I have more than
> one session with diffent number of datas so that I am searching for a
> general way of summing up my logwsAttack12 and logwsAttack37. The
> program should recognize how many data are in my table concerning ws12
> and how many concerning ws37 and should, in dependency of z, sum them
> up. I hope you understand my problem and hopefully someone is able to
> solve it. Many thanks for the moment.
>
> Best regards,
>
> Stefan Wagner
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help