[R] how to compute this summation...

kathie kathryn.lord2000 at gmail.com
Thu Aug 20 02:36:38 CEST 2009


Dear R users, 

I try to compute this summation,

http://www.nabble.com/file/p25054272/dd.jpg 

where

f(y|x) = Negative Binomial(y, mu=exp(x' beta), size=1/alp)

http://www.nabble.com/file/p25054272/aa.jpg 

http://www.nabble.com/file/p25054272/cc.jpg 


In fact, I tried to use "do.call" function to compute each u(y,x) before the
summation, but I got an error, "Error in X[i, ] * t(beta) : non-conformable
arrays".


Here is my code.

#----------------------------------------------------------------------------------



# data ------------------------------------------------------------
yy <- c(2,2,10,3,6,7,9,9,14,6)
x1 <- c(0,0,0,0,0,1,0,0,0,0)
x2 <- c(1,1,0,1,0,0,0,0,0,0)
x3 <- c(0,0,1,0,0,0,0,0,0,0)
x4 <- c(0,0,0,0,0,0,0,0,0,0)
x5 <- c(0,0,0,0,0,0,0,0,0,1)
x6 <- c(0,0,0,0,1,0,0,0,0,0)

n <- length(yy)
x <- cbind(x1,x2,x3,x4,x5,x6)
X <- cbind(1,x)
p <- 7   ####  p:#beta 

library(numDeriv)

# u function -------------------------------------------------------

obj <- function(theta,i,j)
{
	beta <- theta[1:p]
	alp <- theta[p+1]

	log(dnbinom(yy[j], mu=exp(rowSums(X[i,] * t(beta))), size=1/alp))
} 



list <- expand.grid(j=seq(1,n),i=seq(1,n))

func <- function(i,j) { grad(func=obj,i=i,j=j, 
x=c(1.50, 0.02, 0.09, 0.22,  0.17, -0.08, -0.13, 0.04)) }

do.call( func, list )


#------------------------------------------------------------------------------------


Any suggestion will be greatly appreciated.

Regards,

Kathryn Lord 
-- 
View this message in context: http://www.nabble.com/how-to-compute-this-summation...-tp25054272p25054272.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list