[R] Sum up the values of a function

Stefan Wagner Stef.Wagner at students.uni-mannheim.de
Fri Jun 10 13:08:53 CEST 2005


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[1]*(z[3]-x1[i]- z[4]*(m1[i]/n1[i]-0.5)))))
ws37 <- function (z, i) (1/(1+exp(z[2]*(z[3]-x2[i]- z[5]*(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

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




More information about the R-help mailing list