Re: [R] rbind for a list

2009-09-29 Thread Carlos Hernandez
On Tue, Sep 29, 2009 at 8:05 PM, David Winsemius 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)
{
  

  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,]13
> [2,]24
>
> [[2]]
> [,1] [,2]
> [1,]57
> [2,]68
>
> > gg3 <- list(list(matrix(1:4, 2)), list(matrix(5:8,2)))
> > gg3
> [[1]]
> [[1]][[1]]
> [,1] [,2]
> [1,]13
> [2,]24
>
>
> [[2]]
> [[2]][[1]]
> [,1] [,2]
> [1,]57
> [2,]68
>
> This does work,  but it is not "intuitive:
>
> > rbind2 <- function (x) Reduce("rbind", x)
> > rbind2(lapply(gg3, "[[", 1))
> [,1] [,2]
> [1,]13
> [2,]24
> [3,]57
> [4,]68
>
> --
>
> 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]]
>> 
>>
>> [[2]]
>> [[2]][[1]]
>> 
>> .
>> .
>> .
>> [[48]]
>> [[48]][[1]]
>> 
>>
>> 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]]

__
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.


Re: [R] rbind for a list

2009-09-29 Thread David Winsemius


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, but aside from that what problems are you experiencing that  
make you want different approaches? You have  shot your self in the  
foot for using simple methods by creating a more complex than needed  
list structure:


> gg3 <- list(matrix(1:4, 2), matrix(5:8,2))
> gg3
[[1]]
 [,1] [,2]
[1,]13
[2,]24

[[2]]
 [,1] [,2]
[1,]57
[2,]68

> gg3 <- list(list(matrix(1:4, 2)), list(matrix(5:8,2)))
> gg3
[[1]]
[[1]][[1]]
 [,1] [,2]
[1,]13
[2,]24


[[2]]
[[2]][[1]]
 [,1] [,2]
[1,]57
[2,]68

This does work,  but it is not "intuitive:

> rbind2 <- function (x) Reduce("rbind", x)
> rbind2(lapply(gg3, "[[", 1))
 [,1] [,2]
[1,]13
[2,]24
[3,]57
[4,]68

--

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]]


[[2]]
[[2]][[1]]

.
.
.
[[48]]
[[48]][[1]]


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,


__
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.


Re: [R] rbind for a list

2009-09-29 Thread Carlos Hernandez
Thank you for your quick reply! It works perfectly!

On Tue, Sep 29, 2009 at 7:51 PM, Henrique Dallazuanna wrote:

> Try this;
>
> do.call(rbind, sapply(gg2, '[', 1))
>
> On Tue, Sep 29, 2009 at 2: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]]) }
> >
> > 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]]
> > 
> >
> > [[2]]
> > [[2]][[1]]
> > 
> > .
> > .
> > .
> > [[48]]
> > [[48]][[1]]
> > 
> >
> > 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,
> > Carlos
> >
> >[[alternative HTML version deleted]]
> >
> >
> > __
> > 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.
> >
> >
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>

[[alternative HTML version deleted]]

__
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.


Re: [R] rbind for a list

2009-09-29 Thread Henrique Dallazuanna
Try this;

do.call(rbind, sapply(gg2, '[', 1))

On Tue, Sep 29, 2009 at 2: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]]) }
>
> 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]]
> 
>
> [[2]]
> [[2]][[1]]
> 
> .
> .
> .
> [[48]]
> [[48]][[1]]
> 
>
> 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,
> Carlos
>
>        [[alternative HTML version deleted]]
>
>
> __
> 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.
>
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

__
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.