On Tue, Sep 29, 2009 at 8:05 PM, David Winsemius <[email protected]>wrote:
>
> On Sep 29, 2009, at 1:43 PM, Carlos Hernandez wrote:
>
> Dear All,
>> I´m using the following code:
>>
>> all1<-gg2[[1]][[1]]; for(i in 1:48){ all1 <- rbind(all1,gg2[[i]][[1]]) }
>>
>
> Looks to me that you would be getting a duplicate copy of the first matrix,
>
yes, you are right. the duplicate should not be there!
> but aside from that what problems are you experiencing that make you want
> different approaches?
I have a function (ff1) that returns a list and one of the objects is the
gg2 list. Out of that list of matrices i'm computing a jacobian in a
likelihood function (that i'm computing several times). The code of Henrique
is slightly faster and results in the following performance:
$by.total
total.time total.pct self.time self.pct
"f.loglike" 0.38 100.0 0.00 0.0
"tcrossprod" 0.18 47.4 0.18 47.4
"FUN" 0.10 26.3 0.02 5.3
"apply" 0.10 26.3 0.00 0.0
>>"ff1" 0.10 26.3 0.00 0.0
"diag<-" 0.08 21.1 0.08 21.1
"%*%" 0.06 15.8 0.06 15.8
"f.draw.vi.chol" 0.04 10.5 0.02 5.3
"determinant.matrix" 0.02 5.3 0.02 5.3
"determinant" 0.02 5.3 0.00 0.0
the "ff1" function is extremely fast but what is slowing down my computation
is the following code:
mat1<-tcrossprod(all1) ## all1 is the matrix i get from do.call(rbind,
sapply(gg2, '[', 1)) ##
diag(mat1)<-diag1[is.na(diag1)==FALSE] ## these are diagonal elements i get
from somewhere else
jacl <- rep(0,48)
for(i in 1:48){ jacl[i] <-
as.numeric(determinant(mat1[idx[i]:idx2[i],idx[i]:idx2[i]],logarithm=TRUE)[1])
}
the idx and idx2 are indices that get the matrix blocks of the matrix "mat1"
for which i need to compute the determinant.
You have shot your self in the foot for using simple methods by creating a
> more complex than needed list structure:
>
>
The complex structure (where i probably shot my foot) comes from this code:
f.sub1 <- function (array1,Lmat,Emat,H=50)
{
<some code here>
out1 <- list(vec2,mat2) ##mat2 contains a matrix, vec2 is a vector with
the same number of rows that mat2
return(out1)
}
ff1 <- function (array1,Lmat,Emat)
{
## array1 contains matrices
out1 <- apply(array1,3,f.sub1,Lmat=Lmat,Emat=Emat)
out1
}
Is there something I could do to improve this last part? or the previous
one?
thank you for your reply and time!!
> > gg3 <- list(matrix(1:4, 2), matrix(5:8,2))
> > gg3
> [[1]]
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
> [[2]]
> [,1] [,2]
> [1,] 5 7
> [2,] 6 8
>
> > gg3 <- list(list(matrix(1:4, 2)), list(matrix(5:8,2)))
> > gg3
> [[1]]
> [[1]][[1]]
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
>
> [[2]]
> [[2]][[1]]
> [,1] [,2]
> [1,] 5 7
> [2,] 6 8
>
> This does work, but it is not "intuitive:
>
> > rbind2 <- function (x) Reduce("rbind", x)
> > rbind2(lapply(gg3, "[[", 1))
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
> [3,] 5 7
> [4,] 6 8
>
> --
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>
>
>> to create a new matrix that contains all the matrices in a list called
>> gg2.
>> gg2 is a list that looks like
>>
>> gg2
>>>>
>>> [[1]]
>> [[1]][[1]]
>> <matrix one>
>>
>> [[2]]
>> [[2]][[1]]
>> <matrix two>
>> .
>> .
>> .
>> [[48]]
>> [[48]][[1]]
>> <matrix 48>
>>
>> Is there a faster way to do the rbind?
>>
>> i've tried do.call("rbind",gg2) but does not work.
>>
>> Thank you for any hints or advice!
>>
>> Best regards,
>>
>
>
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.