Re: [R] average matrices across a list

2010-09-14 Thread Ben Bolker
Gregory Ryslik rsaber at comcast.net writes:


 mymats - vector('list', 5)
 set.seed(246)
 
 # Generate a list of five 3 x 3 matrices
 for(i in 1:5) mymats[[i]] - matrix(sample(1:9), nrow = 3)
 
 mymats[[5]][1,1]-NA
 mymats[[4]][2,2]-NA
 mymats
 
 matrixadder-function(u,v){
   na.u-is.na(u)
   na.v-is.na(v)  
   ifelse(na.u  na.v, NA, ifelse(na.u, 0, u)+ ifelse(na.v,0,v))   
 }
 
 Reduce('matrixadder',mymats)


  I was going to suggest that my solution would be faster,
but it turns out to be slower (!) -- 11 seconds vs 6 seconds
for 10,000 replications.

  It's cleverer, but I don't know if that's really a virtue.

library(abind)
apply(do.call(abind,c(mymats,list(along=3))),c(1,2),sum,na.rm=TRUE)

system.time(replicate(1,
  apply(do.call(abind,c(mymats,list(along=3))),
c(1,2),sum,na.rm=TRUE)))

__
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] average matrices across a list

2010-09-14 Thread Henrique Dallazuanna
You can try this also:

Reduce('+', lapply(mymats, function(x)replace(x, is.na(x), 0)))


On Sun, Sep 12, 2010 at 10:36 PM, Gregory Ryslik rsa...@comcast.net wrote:

 Hi Everyone,

 Thanks to everyone for their help. With your suggestions and some poking
 around, the following works for what I need. It basically adds all the
 matrices elementwise, and adds nothing if the element is NA. Thanks again!
 Code below:


 **
 mymats - vector('list', 5)
 set.seed(246)

 # Generate a list of five 3 x 3 matrices
 for(i in 1:5) mymats[[i]] - matrix(sample(1:9), nrow = 3)

 mymats[[5]][1,1]-NA
 mymats[[4]][2,2]-NA
 mymats

 matrixadder-function(u,v){
na.u-is.na(u)
na.v-is.na(v)
ifelse(na.u  na.v, NA, ifelse(na.u, 0, u)+ ifelse(na.v,0,v))
 }

 Reduce('matrixadder',mymats)
 **

 Cheers,
 Greg




 On Sep 12, 2010, at 8:33 PM, Ben Bolker wrote:

  My next suggestion (I don't have time to work out or test an example
  at the moment):
 
  library(abind)
  tmparr - abind(m1,m2,m3,...,along=3)
   OR
  tmparr - do.call(c(matlist,list(along=3)))
  apply(tmparr,c(1,2),mean,na.rm=TRUE)
 
   or something along those lines.
 
  __
  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.


 [[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] average matrices across a list

2010-09-14 Thread Dennis Murphy
Hi:

It's certainly fast (0.97s for 1 reps on my box), but doesn't
replacement by zero affect the denominator of the sum, thereby deflating the
means (assuming the contents of the matrices are nonnegative or NA)?

Dennis

On Tue, Sep 14, 2010 at 5:28 AM, Henrique Dallazuanna www...@gmail.comwrote:

 You can try this also:

 Reduce('+', lapply(mymats, function(x)replace(x, is.na(x), 0)))


 On Sun, Sep 12, 2010 at 10:36 PM, Gregory Ryslik rsa...@comcast.net
 wrote:

  Hi Everyone,
 
  Thanks to everyone for their help. With your suggestions and some poking
  around, the following works for what I need. It basically adds all the
  matrices elementwise, and adds nothing if the element is NA. Thanks
 again!
  Code below:
 
 
  **
  mymats - vector('list', 5)
  set.seed(246)
 
  # Generate a list of five 3 x 3 matrices
  for(i in 1:5) mymats[[i]] - matrix(sample(1:9), nrow = 3)
 
  mymats[[5]][1,1]-NA
  mymats[[4]][2,2]-NA
  mymats
 
  matrixadder-function(u,v){
 na.u-is.na(u)
 na.v-is.na(v)
 ifelse(na.u  na.v, NA, ifelse(na.u, 0, u)+ ifelse(na.v,0,v))
  }
 
  Reduce('matrixadder',mymats)
  **
 
  Cheers,
  Greg
 
 
 
 
  On Sep 12, 2010, at 8:33 PM, Ben Bolker wrote:
 
   My next suggestion (I don't have time to work out or test an example
   at the moment):
  
   library(abind)
   tmparr - abind(m1,m2,m3,...,along=3)
OR
   tmparr - do.call(c(matlist,list(along=3)))
   apply(tmparr,c(1,2),mean,na.rm=TRUE)
  
or something along those lines.
  
   __
   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.
 
 
  [[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.



[[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] average matrices across a list

2010-09-14 Thread Gregory Ryslik
Hi,

The denominator i compute seperately counting how many observations there were 
that were not NA. Thus the I divide each (n,m) cell by the number of counts it 
was not NA.

Thanks,
Greg
On Sep 14, 2010, at 9:23 AM, Dennis Murphy wrote:

 Hi:
 
 It's certainly fast (0.97s for 1 reps on my box), but doesn't replacement 
 by zero affect the denominator of the sum, thereby deflating the means 
 (assuming the contents of the matrices are nonnegative or NA)?
 
 Dennis
 
 On Tue, Sep 14, 2010 at 5:28 AM, Henrique Dallazuanna www...@gmail.com 
 wrote:
 You can try this also:
 
 Reduce('+', lapply(mymats, function(x)replace(x, is.na(x), 0)))
 
 
 On Sun, Sep 12, 2010 at 10:36 PM, Gregory Ryslik rsa...@comcast.net wrote:
 
  Hi Everyone,
 
  Thanks to everyone for their help. With your suggestions and some poking
  around, the following works for what I need. It basically adds all the
  matrices elementwise, and adds nothing if the element is NA. Thanks again!
  Code below:
 
 
  **
  mymats - vector('list', 5)
  set.seed(246)
 
  # Generate a list of five 3 x 3 matrices
  for(i in 1:5) mymats[[i]] - matrix(sample(1:9), nrow = 3)
 
  mymats[[5]][1,1]-NA
  mymats[[4]][2,2]-NA
  mymats
 
  matrixadder-function(u,v){
 na.u-is.na(u)
 na.v-is.na(v)
 ifelse(na.u  na.v, NA, ifelse(na.u, 0, u)+ ifelse(na.v,0,v))
  }
 
  Reduce('matrixadder',mymats)
  **
 
  Cheers,
  Greg
 
 
 
 
  On Sep 12, 2010, at 8:33 PM, Ben Bolker wrote:
 
   My next suggestion (I don't have time to work out or test an example
   at the moment):
  
   library(abind)
   tmparr - abind(m1,m2,m3,...,along=3)
OR
   tmparr - do.call(c(matlist,list(along=3)))
   apply(tmparr,c(1,2),mean,na.rm=TRUE)
  
or something along those lines.
  
   __
   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.
 
 
  [[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.
 
 


[[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] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Sorry, I forgot to add that some of the entries in various matrices have NA in 
them.
On Sep 12, 2010, at 3:40 PM, Gregory Ryslik wrote:

 Hi,
 
 I have a list of several hundred 2 dimensional matrices, where each matrix is 
 n x m. What I need to do is that for each n,m I  need an average over all the 
 lists. This would collapse it down to just one nxm matrix. Any easy ways to 
 do that? As always, I'd like to avoid a for loop to keep computational time 
 low! Thanks again everyone!
 
 Cheers,
 G
 __
 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.

__
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] average matrices across a list

2010-09-12 Thread Phil Spector

Gregory -
   Suppose your list is called mymats.  Then

Reduce(+,mymats)

does what you want.
  - Phil


On Sun, 12 Sep 2010, Gregory Ryslik wrote:


Hi,

I have a list of several hundred 2 dimensional matrices, where each matrix is n 
x m. What I need to do is that for each n,m I  need an average over all the 
lists. This would collapse it down to just one nxm matrix. Any easy ways to do 
that? As always, I'd like to avoid a for loop to keep computational time low! 
Thanks again everyone!

Cheers,
G
__
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.



__
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] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Hi,

Doing that I get the following:

Browse[2] Reduce[+,results]
Error in Reduce[+, results] : 
  object of type 'closure' is not subsettable

Thanks again!

Kind regards,
Greg
On Sep 12, 2010, at 3:49 PM, Phil Spector wrote:

 Gregory -
   Suppose your list is called mymats.  Then
 
 Reduce(+,mymats)
 
 does what you want.
  - Phil
 
 
 On Sun, 12 Sep 2010, Gregory Ryslik wrote:
 
 Hi,
 
 I have a list of several hundred 2 dimensional matrices, where each matrix 
 is n x m. What I need to do is that for each n,m I  need an average over all 
 the lists. This would collapse it down to just one nxm matrix. Any easy ways 
 to do that? As always, I'd like to avoid a for loop to keep computational 
 time low! Thanks again everyone!
 
 Cheers,
 G
 __
 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.
 

__
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] average matrices across a list

2010-09-12 Thread Ben Bolker
Gregory Ryslik rsaber at comcast.net writes:

 Browse[2] Reduce[+,results]
 Error in Reduce[+, results] : 
   object of type 'closure' is not subsettable
 

  You need to use parentheses, not square brackets.

__
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] average matrices across a list

2010-09-12 Thread Phil Spector

Gregory -
   Please provide a reproducible example.
I have no idea what results is.

   - Phil


On Sun, 12 Sep 2010, Gregory Ryslik wrote:


Hi,

Doing that I get the following:

Browse[2] Reduce[+,results]
Error in Reduce[+, results] :
 object of type 'closure' is not subsettable

Thanks again!

Kind regards,
Greg
On Sep 12, 2010, at 3:49 PM, Phil Spector wrote:


Gregory -
  Suppose your list is called mymats.  Then

Reduce(+,mymats)

does what you want.
 - Phil


On Sun, 12 Sep 2010, Gregory Ryslik wrote:


Hi,

I have a list of several hundred 2 dimensional matrices, where each matrix is n 
x m. What I need to do is that for each n,m I  need an average over all the 
lists. This would collapse it down to just one nxm matrix. Any easy ways to do 
that? As always, I'd like to avoid a for loop to keep computational time low! 
Thanks again everyone!

Cheers,
G
__
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.



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



__
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] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Hi,

Thanks, I was using the square brackets instead of (. The ( makes it work.

However, some of my matrices have NA for some values. I need those NA's to 
basically not be counted. And if all the lists have NA for a specific (n,m), I 
want it to remain an (n,m). By using the Reduce('+', mymats), any entries with 
NA in at least one of the lists will give me back an NA for the sum (rather 
than making it NA if it at least on list has an NA for that element).

Thanks again for everyone's help!

Kind regards,
Greg
On Sep 12, 2010, at 4:54 PM, Dennis Murphy wrote:

 Hi:
 
 Here's a more concrete example of Phil's point.
 
 mymats - vector('list', 5)
 set.seed(246)
 
 # Generate a list of five 3 x 3 matrices
 for(i in 1:5) mymats[[i]] - matrix(sample(1:9), nrow = 3)
 # Sum them elementwise
 Reduce('+', mymats)
  [,1] [,2] [,3]
 [1,]   23   33   15
 [2,]   25   36   26
 [3,]   20   24   23
 
 HTH,
 Dennis
 
 On Sun, Sep 12, 2010 at 12:52 PM, Gregory Ryslik rsa...@comcast.net wrote:
 Hi,
 
 Doing that I get the following:
 
 Browse[2] Reduce[+,results]
 Error in Reduce[+, results] :
  object of type 'closure' is not subsettable
 
 You want parentheses there, not brackets; you're asking R to subset a 
 function, Reduce, which is an object of type closure. Hopefully the error 
 message makes more sense now.
  
 
 Thanks again!
 
 Kind regards,
 Greg
 On Sep 12, 2010, at 3:49 PM, Phil Spector wrote:
 
  Gregory -
Suppose your list is called mymats.  Then
 
  Reduce(+,mymats)
 
  does what you want.
   - Phil
 
 
  On Sun, 12 Sep 2010, Gregory Ryslik wrote:
 
  Hi,
 
  I have a list of several hundred 2 dimensional matrices, where each matrix 
  is n x m. What I need to do is that for each n,m I  need an average over 
  all the lists. This would collapse it down to just one nxm matrix. Any 
  easy ways to do that? As always, I'd like to avoid a for loop to keep 
  computational time low! Thanks again everyone!
 
  Cheers,
  G
  __
  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.
 
 
 __
 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.
 


[[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] average matrices across a list

2010-09-12 Thread Ben Bolker
My next suggestion (I don't have time to work out or test an example
at the moment):

library(abind)
tmparr - abind(m1,m2,m3,...,along=3)
  OR
tmparr - do.call(c(matlist,list(along=3)))
apply(tmparr,c(1,2),mean,na.rm=TRUE)

  or something along those lines.

__
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] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Hi Everyone,

Thanks to everyone for their help. With your suggestions and some poking 
around, the following works for what I need. It basically adds all the matrices 
elementwise, and adds nothing if the element is NA. Thanks again! Code below:


**
mymats - vector('list', 5)
set.seed(246)

# Generate a list of five 3 x 3 matrices
for(i in 1:5) mymats[[i]] - matrix(sample(1:9), nrow = 3)

mymats[[5]][1,1]-NA
mymats[[4]][2,2]-NA
mymats

matrixadder-function(u,v){
na.u-is.na(u)
na.v-is.na(v)  
ifelse(na.u  na.v, NA, ifelse(na.u, 0, u)+ ifelse(na.v,0,v))   
}

Reduce('matrixadder',mymats)
**

Cheers,
Greg




On Sep 12, 2010, at 8:33 PM, Ben Bolker wrote:

 My next suggestion (I don't have time to work out or test an example
 at the moment):
 
 library(abind)
 tmparr - abind(m1,m2,m3,...,along=3)
  OR
 tmparr - do.call(c(matlist,list(along=3)))
 apply(tmparr,c(1,2),mean,na.rm=TRUE)
 
  or something along those lines.
 
 __
 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.


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