Re: [R] expm() within the Matrix package
Gad == Gad Abraham [EMAIL PROTECTED] on Mon, 19 Mar 2007 09:36:15 +1100 writes: Gad If you convert to numeric, you can then assign it to Loglik: Loglik[1] - as.numeric(log(p %*% expm(Q * y[i]) %*% q)) Loglik[1] Gad [1] 134.5565 Hmm, I don't think that's Laura's problem (and actually I don't know what her problem is) : Assignment of a 1 x 1 matrix to a vector is not a problem, and hence the as.numeric(.) above really has no effect : ll - 1:2 (m - matrix(pi, 1,1)) [,1] [1,] 3.141593 ll[1] - m ll [1] 3.141593 2.00 Gad Ah but you're using 'matrix' while she's using 'Matrix' Gad (AFAIK there is no expm for matrix): Yes, of course, you are absolutely right (and I'm pretty embarrassed). Martin library(Matrix) Gad Loading required package: lattice m - Matrix(1, nrow=1, ncol=1) m Gad 1 x 1 diagonal matrix of class ddiMatrix Gad [,1] Gad [1,]1 Gad Warning message: Gad Ambiguous method selection for diag, target ddiMatrix (the first of Gad the signatures shown will be used) Gad diagonalMatrix Gad ddenseMatrix Gad in: .findInheritedMethods(classes, fdef, mtable) a - vector(numeric, 0) a[1] - m Gad Error in a[1] - m : incompatible types (from S4 to double) in Gad subassignment type fix a[1] - as.numeric(m) a Gad [1] 1 [.] __ 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] expm() within the Matrix package
Gad If you convert to numeric, you can then assign it to Loglik: Loglik[1] - as.numeric(log(p %*% expm(Q * y[i]) %*% q)) Loglik[1] Gad [1] 134.5565 Hmm, I don't think that's Laura's problem (and actually I don't know what her problem is) : Assignment of a 1 x 1 matrix to a vector is not a problem, and hence the as.numeric(.) above really has no effect : ll - 1:2 (m - matrix(pi, 1,1)) [,1] [1,] 3.141593 ll[1] - m ll [1] 3.141593 2.00 Ah but you're using 'matrix' while she's using 'Matrix' (AFAIK there is no expm for matrix): library(Matrix) Loading required package: lattice m - Matrix(1, nrow=1, ncol=1) m 1 x 1 diagonal matrix of class ddiMatrix [,1] [1,]1 Warning message: Ambiguous method selection for diag, target ddiMatrix (the first of the signatures shown will be used) diagonalMatrix ddenseMatrix in: .findInheritedMethods(classes, fdef, mtable) a - vector(numeric, 0) a[1] - m Error in a[1] - m : incompatible types (from S4 to double) in subassignment type fix a[1] - as.numeric(m) a [1] 1 sessionInfo() R version 2.4.1 (2006-12-18) i486-pc-linux-gnu locale: LC_CTYPE=en_AU.UTF-8;LC_NUMERIC=C;LC_TIME=en_AU.UTF-8;LC_COLLATE=en_AU.UTF-8;LC_MONETARY=en_AU.UTF-8;LC_MESSAGES=en_AU.UTF-8;LC_PAPER=en_AU.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_AU.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods [7] base other attached packages: Matrix lattice 0.9975-11 0.14-16 -- Gad Abraham Department of Mathematics and Statistics The University of Melbourne Parkville 3010, Victoria, Australia email: [EMAIL PROTECTED] web: http://www.ms.unimelb.edu.au/~gabraham __ 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] expm() within the Matrix package
Gad == Gad Abraham [EMAIL PROTECTED] on Fri, 16 Mar 2007 09:48:52 +1100 writes: Gad Laura Hill wrote: 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() Gad You need to do a type conversion here, because you get the loglik as a Gad 1x1 Matrix, instead of a scalar: Gad (after running your code) log(p %*% expm(Q * y[i]) %*% q) Gad 1 x 1 Matrix of class dgeMatrix Gad [,1] Gad [1,] 134.5565 Gad If you convert to numeric, you can then assign it to Loglik: Loglik[1] - as.numeric(log(p %*% expm(Q * y[i]) %*% q)) Loglik[1] Gad [1] 134.5565 Hmm, I don't think that's Laura's problem (and actually I don't know what her problem is) : Assignment of a 1 x 1 matrix to a vector is not a problem, and hence the as.numeric(.) above really has no effect : ll - 1:2 (m - matrix(pi, 1,1)) [,1] [1,] 3.141593 ll[1] - m ll [1] 3.141593 2.00 Martin Maechler, ETH Zurich __ 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] expm() within the Matrix package
Gad == Gad Abraham [EMAIL PROTECTED] on Fri, 16 Mar 2007 09:48:52 +1100 writes: Gad Laura Hill wrote: 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() Gad You need to do a type conversion here, because you get the loglik as a Gad 1x1 Matrix, instead of a scalar: Gad (after running your code) log(p %*% expm(Q * y[i]) %*% q) Gad 1 x 1 Matrix of class dgeMatrix Gad [,1] Gad [1,] 134.5565 Gad If you convert to numeric, you can then assign it to Loglik: Loglik[1] - as.numeric(log(p %*% expm(Q * y[i]) %*% q)) Loglik[1] Gad [1] 134.5565 Gad -- Gad Gad Abraham Gad Department of Mathematics and Statistics Gad The University of Melbourne Gad Parkville 3010, Victoria, Australia Gad email: [EMAIL PROTECTED] Gad web: http://www.ms.unimelb.edu.au/~gabraham Gad __ Gad R-help@stat.math.ethz.ch mailing list Gad https://stat.ethz.ch/mailman/listinfo/r-help Gad PLEASE do read the posting guide http://www.R-project.org/posting-guide.html Gad and provide commented, minimal, self-contained, reproducible code. __ 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() within the Matrix package
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.
Re: [R] expm() within the Matrix package
Laura Hill wrote: 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() You need to do a type conversion here, because you get the loglik as a 1x1 Matrix, instead of a scalar: (after running your code) log(p %*% expm(Q * y[i]) %*% q) 1 x 1 Matrix of class dgeMatrix [,1] [1,] 134.5565 If you convert to numeric, you can then assign it to Loglik: Loglik[1] - as.numeric(log(p %*% expm(Q * y[i]) %*% q)) Loglik[1] [1] 134.5565 -- Gad Abraham Department of Mathematics and Statistics The University of Melbourne Parkville 3010, Victoria, Australia email: [EMAIL PROTECTED] web: http://www.ms.unimelb.edu.au/~gabraham __ 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()
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.
Re: [R] expm()
On 3/6/07, Laura Hill [EMAIL PROTECTED] wrote: 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? I don't quite understand the question. The exponential of a matrix is only defined for square matrices. For example for (i in 1:length(y)){ expmN-expm(Q*y[i]) Q-Matrix(c(1, 2, 3, 4), 2, 2) return(expmN) } I think what you are trying to do here is to calculate the exponential of Q first and then multiply it by the columns of y (although I don't understand why the loop runs to length(y) and you use y[i] - if y is a numeric vector then y[i] is a scalar). library(Matrix) Loading required package: lattice Q - Matrix(c(1,2,3,4), 2, 2) Q 2 x 2 Matrix of class dgeMatrix [,1] [,2] [1,]13 [2,]24 expm(Q) 2 x 2 Matrix of class dgeMatrix [,1] [,2] [1,] 51.96896 112.1048 [2,] 74.73656 164.0738 If you then want to multiply expm(Q) by the columns of a 2 by k matrix Y you can use expm(Q) %*% Y 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. __ 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.