[R] expm() within the Matrix package

2007-03-15 Thread Laura Hill
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

__
R-help@stat.math.ethz.ch 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.


[R] multiplying matrix by vector of times

2007-03-13 Thread Laura Hill



On 8/3/07 22:12, Gad Abraham [EMAIL PROTECTED] wrote:

 Laura Hill wrote:
 
 
 On 7/3/07 00:15, Gad Abraham [EMAIL PROTECTED] wrote:
 

 On 6 Mar 2007, at 08:54, Laura Hill wrote:
 
 Hi,
 
 My name is Laura. I'm a PhD student at Queen's University Belfast
 and have
 just started learning R. I was wondering if somebody could help me
 to see
 where I am going wrong in my code for estimating the parameters
 [mu1, mu2,
 lambda1] of a 2-phase Coxian Distribution.
 
 cox2.lik-function(theta, y){
 mu1-theta[1]
 
 mu2-theta[2]
 
 lambda1-theta[3]
 
 p-Matrix(c(1, 0), nrow=1, ncol=2)
 
 Q-Matrix(c(-(lambda1 + mu1), 0, lambda1, -mu2), nrow=2, ncol=2)
 
 q-Matrix(c(mu1, mu2), nrow=2, ncol=1)
 
 for (i in 1:length(y)){
 loglik-log(p %*% expm(Q * y(i)) %*% q)
 return(loglik)}
 
 sumloglik-sum(loglik)
 
 return(-sumloglik)
 }

 
 
 
 Hi Gad,
 
 Yes that's exactly hat I am trying to do. If I gave you a simple example,
 could you perhaps tell me how I could create a vector of log likelihoods.
 
 
 
 Lets say I have 1x1 matrices:
 
 p=[1]
 Q=[0.05]  i.e. [mu1]
 q=[-0.05] i.e. [-mu1]
 
 Where mu1 is the parameter that I would like to estimate and I have chosen
 the initial value mu1=0.05
 
 
 Loglik-p %*% expm(Q*y) %*% q
 
 Where y=(5 10)
 
 I want to sum the log likelihoods that I get for y=5 and y=10 using
 
 Sumloglik-sum(allloglik)
 
 Where allloglik = vector of log likelihoods
 
 
 Any help would be greatly appreciated.
 
 Thanks in advance
 Laura
 
 
 Hi Laura,
 
 Make an empty vector of required length, then assign the loglik to each
 of its cells, and don't return() anything:
 
 loglik - rep(0, length(y))
 for(i in 1:length(y)){
 loglik[i] - log(p %*% expm(Q * y[i]) %*% q)
 }
 
 Then you can sum(loglik) like you did before.
 
 Cheers,
 Gad


Hi Gad,

Thanks for the tip about the empty vector, I ever knew you could do that. I
just have one problem,

Lets say Q is a 2x2 matrix
 p is a 1x2 matrix
 q is a 2x1 matrix
 y is vector of times, say y = c(5, 10)

How do I multiply Q by each time y[i]?

I would like to get the answer to the equation

loglik[i] - log(p %*% expm(Q * y[i]) %*% q)

Where first y=5 and then y=10 so that the answers to loglik for each i are
put into the empty vector.

I'm sure that I am missing something fairly obvious here but can't put my
finger on it.

Thanks in advance
Laura

__
R-help@stat.math.ethz.ch 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.


[R] multiplying matrix by vector of times

2007-03-13 Thread Laura Hill



On 8/3/07 22:12, Gad Abraham [EMAIL PROTECTED] wrote:

 Laura Hill wrote:
 
 
 On 7/3/07 00:15, Gad Abraham [EMAIL PROTECTED] wrote:
 

 On 6 Mar 2007, at 08:54, Laura Hill wrote:
 
 Hi,
 
 My name is Laura. I'm a PhD student at Queen's University Belfast
 and have
 just started learning R. I was wondering if somebody could help me
 to see
 where I am going wrong in my code for estimating the parameters
 [mu1, mu2,
 lambda1] of a 2-phase Coxian Distribution.
 
 cox2.lik-function(theta, y){
 mu1-theta[1]
 
 mu2-theta[2]
 
 lambda1-theta[3]
 
 p-Matrix(c(1, 0), nrow=1, ncol=2)
 
 Q-Matrix(c(-(lambda1 + mu1), 0, lambda1, -mu2), nrow=2, ncol=2)
 
 q-Matrix(c(mu1, mu2), nrow=2, ncol=1)
 
 for (i in 1:length(y)){
 loglik-log(p %*% expm(Q * y(i)) %*% q)
 return(loglik)}
 
 sumloglik-sum(loglik)
 
 return(-sumloglik)
 }

 
 
 
 Hi Gad,
 
 Yes that's exactly hat I am trying to do. If I gave you a simple example,
 could you perhaps tell me how I could create a vector of log likelihoods.
 
 
 
 Lets say I have 1x1 matrices:
 
 p=[1]
 Q=[0.05]  i.e. [mu1]
 q=[-0.05] i.e. [-mu1]
 
 Where mu1 is the parameter that I would like to estimate and I have chosen
 the initial value mu1=0.05
 
 
 Loglik-p %*% expm(Q*y) %*% q
 
 Where y=(5 10)
 
 I want to sum the log likelihoods that I get for y=5 and y=10 using
 
 Sumloglik-sum(allloglik)
 
 Where allloglik = vector of log likelihoods
 
 
 Any help would be greatly appreciated.
 
 Thanks in advance
 Laura
 
 
 Hi Laura,
 
 Make an empty vector of required length, then assign the loglik to each
 of its cells, and don't return() anything:
 
 loglik - rep(0, length(y))
 for(i in 1:length(y)){
 loglik[i] - log(p %*% expm(Q * y[i]) %*% q)
 }
 
 Then you can sum(loglik) like you did before.
 
 Cheers,
 Gad


Hi Gad,

Thanks for the tip about the empty vector, I ever knew you could do that. I
just have one problem,

Lets say Q is a 2x2 matrix
 p is a 1x2 matrix
 q is a 2x1 matrix
 y is vector of times, say y = c(5, 10)

How do I multiply Q by each time y[i]?

I would like to get the answer to the equation

loglik[i] - log(p %*% expm(Q * y[i]) %*% q)

Where first y=5 and then y=10 so that the answers to loglik for each i are
put into the empty vector.

I'm sure that I am missing something fairly obvious here but can't put my
finger on it.

Thanks in advance
Laura

__
R-help@stat.math.ethz.ch 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.


Re: [R] Estimating parameters of 2 phase Coxian using optim

2007-03-08 Thread Laura Hill



On 7/3/07 00:15, Gad Abraham [EMAIL PROTECTED] wrote:

 Andy Fugard wrote:
 Hi There,
 
 Perhaps the problem is the line
 
  loglik-log(p %*% expm(Q * y(i)) %*% q)
 
 You mention that y is a vector but here you're treating it as a
 function.  Maybe try
 
  loglik-log(p %*% expm(Q * y[i]) %*% q)
 
 ?
 
 Don't have a clue about the correctness of the contents of cox2.lik...
 
 Andy
 
 
 On 6 Mar 2007, at 08:54, Laura Hill wrote:
 
 Hi,
 
 My name is Laura. I'm a PhD student at Queen's University Belfast
 and have
 just started learning R. I was wondering if somebody could help me
 to see
 where I am going wrong in my code for estimating the parameters
 [mu1, mu2,
 lambda1] of a 2-phase Coxian Distribution.
 
 cox2.lik-function(theta, y){
 mu1-theta[1]
 
 mu2-theta[2]
 
 lambda1-theta[3]
 
 p-Matrix(c(1, 0), nrow=1, ncol=2)
 
 Q-Matrix(c(-(lambda1 + mu1), 0, lambda1, -mu2), nrow=2, ncol=2)
 
 q-Matrix(c(mu1, mu2), nrow=2, ncol=1)
 
 for (i in 1:length(y)){
 loglik-log(p %*% expm(Q * y(i)) %*% q)
 return(loglik)}
 
 sumloglik-sum(loglik)
 
 return(-sumloglik)
 }
 
 Just to add my 2 AU cents regarding the for loop:
 
 You're trying to create a vector of log likelihoods to sum up later, but
 that's not what's happening there. Instead, assign an empty vector of
 same length as y, then assign the loglik from each iteration to a
 different cell.
 
 Lastly, there's no need to return anything from a for loop, it's not a
 function.
 
 HTH,
 Gad



Hi Gad,

Yes that's exactly hat I am trying to do. If I gave you a simple example,
could you perhaps tell me how I could create a vector of log likelihoods.



Lets say I have 1x1 matrices:

p=[1]
Q=[0.05]  i.e. [mu1]
q=[-0.05] i.e. [-mu1]

Where mu1 is the parameter that I would like to estimate and I have chosen
the initial value mu1=0.05


Loglik-p %*% expm(Q*y) %*% q

Where y=(5 10)

I want to sum the log likelihoods that I get for y=5 and y=10 using

Sumloglik-sum(allloglik)

Where allloglik = vector of log likelihoods


Any help would be greatly appreciated.

Thanks in advance
Laura

__
R-help@stat.math.ethz.ch 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.


[R] Estimating parameters of 2 phase Coxian using optim

2007-03-06 Thread Laura Hill


Hi,

My name is Laura. I'm a PhD student at Queen's University Belfast and have
just started learning R. I was wondering if somebody could help me to see
where I am going wrong in my code for estimating the parameters [mu1, mu2,
lambda1] of a 2-phase Coxian Distribution.

cox2.lik-function(theta, y){
mu1-theta[1] 

mu2-theta[2]

lambda1-theta[3]

p-Matrix(c(1, 0), nrow=1, ncol=2)

Q-Matrix(c(-(lambda1 + mu1), 0, lambda1, -mu2), nrow=2, ncol=2)

q-Matrix(c(mu1, mu2), nrow=2, ncol=1)

for (i in 1:length(y)){
loglik-log(p %*% expm(Q * y(i)) %*% q)
return(loglik)}

sumloglik-sum(loglik)

return(-sumloglik)
}

I have installed the Matrix package. y is a vector of 240 survival times. In
the R console I typed

 source(/private/var/automount/users/lhill07/Desktop/cox2.lik.R)

 optim(c(0.5, 0.5, 0.5), cox2.lik, y=y, method=BFGS)

Error: could not find function y

Error in expm(Q * y(i)) : error in evaluating the argument 'x' in selecting
a method for function 'expm'

Error in log(p %*% expm(Q * y(i)) %*% q) :
error in evaluating the argument 'x' in selecting a method for function
'log'


I'm sorry if I have missed something really obvious and I would appreciate
any help that is offered. Hopefully I have given enough information.

Many thanks in advance,

Laura

__
R-help@stat.math.ethz.ch 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.


[R] Estimating parameters of 2 phase Coxian using optim

2007-03-06 Thread Laura Hill
Hi,

My name is Laura. I'm a PhD student at Queen's University Belfast and have
just started learning R. I was wondering if somebody could help me to see
where I am going wrong in my code for estimating the parameters [mu1, mu2,
lambda1] of a 2-phase Coxian Distribution.

cox2.lik-function(theta, y){
mu1-theta[1] 

mu2-theta[2]

lambda1-theta[3]

p-Matrix(c(1, 0), nrow=1, ncol=2)

Q-Matrix(c(-(lambda1 + mu1), 0, lambda1, -mu2), nrow=2, ncol=2)

q-Matrix(c(mu1, mu2), nrow=2, ncol=1)

for (i in 1:length(y)){
loglik-log(p %*% expm(Q * y(i)) %*% q)
return(loglik)}

sumloglik-sum(loglik)

return(-sumloglik)
}

I have installed the Matrix package. y is a vector of 240 survival times. In
the R console I typed

 source(/private/var/automount/users/lhill07/Desktop/cox2.lik.R)

 optim(c(0.5, 0.5, 0.5), cox2.lik, y=y, method=BFGS)

Error: could not find function y

Error in expm(Q * y(i)) : error in evaluating the argument 'x' in selecting
a method for function 'expm'

Error in log(p %*% expm(Q * y(i)) %*% q) :
error in evaluating the argument 'x' in selecting a method for function
'log'


I'm sorry if I have missed something really obvious and I would appreciate
any help that is offered. Hopefully I have given enough information.

Many thanks in advance,

Laura

__
R-help@stat.math.ethz.ch 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.


[R] expm()

2007-03-06 Thread Laura Hill
Can the expm function be used to calculate the exponential of a matrix where
the matrix is multiplied by a vector in a data frame?

For example


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

expmN-expm(Q*y[i])

Q-Matrix(c(1, 2, 3, 4), 2, 2)

return(expmN)

}

Sorry I am new to R and I have been finding it difficult to get information
on calculating Matrix exponentials in R.

Many thanks in advance

Laura

__
R-help@stat.math.ethz.ch 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.