[R] expm() within the Matrix package

Laura Hill lhill07 at qub.ac.uk
Thu Mar 15 13:19:28 CET 2007


Hi

Could anybody give me a bit of advice on some code I'm having trouble with?

I've been trying to calculate the loglikelihood of a function iterated over
a data of time values and I seem to be experiencing difficulty when I use
the function expm(). Here's an example of what I am trying to do


y<-c(5,10)      #vector of 2 survival times

p<-Matrix(c(1,0),1,2)   #1x2 matrix

Q<-Matrix(c(1,2,3,4),2,2)   #2x2 matrix

q<-Matrix(c(1,2),2,1)       #2x1 matrix

Loglik<-rep(0,length(y))    #creating empty vector of same length as y

    for(i in 1:length(y)){

        Loglik[i]<-log((p %*% (expm(Q*y[i]))) %*% q)  #calculating
                   
                                        #  Loglikelihood for each y[i]
        }

The code works perfectly if I use exp(Q*y[i]) but not for expm()


If anyone has any advice they could give that would be great.
I would like to thank Gad Abraham also for helping me get this far.

Thanks in advance

Laura



More information about the R-help mailing list