On Thu, 20 Aug 2009 02:36:38 +0200, kathie <kathryn.lord2...@gmail.com> wrote:


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 )

I could not get what you were trying to do here, but here are a couple of suggestions shat might help:

--Look at function ?outer (applies you function to combinations of i and j - so no need of expand.grid)
--Look at ?Reduce - might help in your case.
--grad function does not have i and j variables - so you are doing something wrong there
--do not name your data.frame - "list"
--do not call your function - "obj"

Best,
Vitalie.



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


Any suggestion will be greatly appreciated.

Regards,

Kathryn Lord


--

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to