[R] matrices call a function element-wise

2011-01-03 Thread zhaoxing731
Hello

I have 4 1000*1000 matrix A,B,C,D. I want to use the corresponding element of 
the 4 matrices. Using the for loop as follow:

E-o
for (i in 1:1000)
{for (j in 1:1000)
 {
 E-fisher.test(matrix(c(A[i][j],B[i][j],C[i][j],D[i][j]),2))#call 
fisher.test for every element
 }
}

It is so time-consuming
Need vectorization

Yours sincerely




ZhaoXing
Department of Health Statistics
West China School of Public Health
Sichuan University
No.17 Section 3, South Renmin Road
Chengdu, Sichuan 610041
P.R.China

[[alternative HTML version deleted]]


__
8O?lW2aQE;3,4sH]A?Cb7QSJOd?

__
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] matrices call a function element-wise

2011-01-03 Thread Jonathan P Daily
IIRC, R is perfectly able to call matrices as vectors, so you might be 
able to do this:

FT - function(i) fisher.test(matrix(c(A[i],B[i],C[i],D[i]),2))
E - sapply(1:100, FT)

Though I don't know how much time you will save.
--
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it.
 - Jubal Early, Firefly

r-help-boun...@r-project.org wrote on 01/03/2011 09:57:14 AM:

 [image removed] 
 
 [R] matrices call a function element-wise
 
 zhaoxing731 
 
 to:
 
 R-help
 
 01/03/2011 01:46 PM
 
 Sent by:
 
 r-help-boun...@r-project.org
 
 Hello
 
 I have 4 1000*1000 matrix A,B,C,D. I want to use the corresponding 
 element of the 4 matrices. Using the for loop as follow:
 
 E-o
 for (i in 1:1000)
 {for (j in 1:1000)
  {
  E-fisher.test(matrix(c(A[i][j],B[i][j],C[i][j],D[i][j]),
 2))#call fisher.test for every element
  }
 }
 
 It is so time-consuming
 Need vectorization
 
 Yours sincerely
 
 
 
 
 ZhaoXing
 Department of Health Statistics
 West China School of Public Health
 Sichuan University
 No.17 Section 3, South Renmin Road
 Chengdu, Sichuan 610041
 P.R.China
 
[[alternative HTML version deleted]]
 
 
 __
 8O?lW2aQE;3,4sH]A?Cb7QSJOd?
 
 __
 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] matrices call a function element-wise

2011-01-03 Thread Jonathan Christensen
Hi,

I would recommend reformatting the data as a 2x2x1000 array and using apply.

Jonathan


On Mon, Jan 3, 2011 at 7:57 AM, zhaoxing731 zhaoxing...@yahoo.com.cn wrote:
 Hello

 I have 4 1000*1000 matrix A,B,C,D. I want to use the corresponding element of 
 the 4 matrices. Using the for loop as follow:

 E-o
 for (i in 1:1000)
{for (j in 1:1000)
  {
 E-fisher.test(matrix(c(A[i][j],B[i][j],C[i][j],D[i][j]),2))#call 
 fisher.test for every element
  }
}

 It is so time-consuming
 Need vectorization

 Yours sincerely




 ZhaoXing
 Department of Health Statistics
 West China School of Public Health
 Sichuan University
 No.17 Section 3, South Renmin Road
 Chengdu, Sichuan 610041
 P.R.China

[[alternative HTML version deleted]]


 __
 8O?lW2aQE;3,4sH]A?Cb7QSJOd?

 __
 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] matrices call a function element-wise

2011-01-03 Thread Dennis Murphy
Hi:

The idea is as follows:
* string your four matrices into vectors, cbinding them so that the
columns correspond to what you want as the (1,1), (1, 2), (2, 1) and (2, 2)
elements, respectively, of the matrix/table to be used for the Fisher test;
   *  Operate row-wise on the constructed matrix, running fisher.test() in
each row, producing a list of model objects;
  * use the ldply() function in package plyr to pick off pieces of output
(or alternatively, do.call(rbind, function applied to list))

Before I give an example, note that an object returned by fisher.test() has
the following components, each of which can be extracted individually; w is
the output object of a single run of fisher.test():
names(w)
[1] p.value conf.intestimatenull.value  alternative
[6] method  data.name

#-
library(plyr)

# generate some matrices:
m1 - matrix(rpois(100, 7), nrow = 10)
m2 - matrix(rpois(100, 7), nrow = 10)
m3 - matrix(rpois(100, 7), nrow = 10)
m4 - matrix(rpois(100, 7), nrow = 10)

# I want m1 to represent the (1, 1) elements, m2 the (1, 2) elements,
# m3 the (2, 1) elements and m4 the (2, 2) elements of the table used as
# input to fisher.test. The idea is to string each of the matrices into
vectors
# and then to recombine them with cbind() into a 4-column matrix = each
# row is then ready to be reshaped into a 2 x 2 matrix for input into
fisher.test()

v - cbind(as.vector(m1), as.vector(m2), as.vector(m3), as.vector(m4))

# Each row of v is reshaped into a 2 x 2 matrix and fisher.test() is
# applied. The result is a list of model objects from fisher.test(), one
component
# for each row of v. [In this example, 100 model objects are generated.]

ll - vector('list', nrow(v))
for(i in seq_along(nrow(v))) ll[[i]] - fisher.test(matrix(v[i, ], nrow =
2))

# Now ll can be used to extract individual pieces of output for each run;
e.g.,
# to get all the p-values and confidence intervals,

pvals - ldply(ll, function(x) x$p.value)
confints - ldply(ll, function(x) x$conf.int)

The time it takes to generate the data and produce the 100 model objects
is:
 system.time( {
+   m1 - matrix(rpois(100, 7), nrow = 1000)
+   m2 - matrix(rpois(100, 7), nrow = 1000)
+   m3 - matrix(rpois(100, 7), nrow = 1000)
+   m4 - matrix(rpois(100, 7), nrow = 1000)
+   v - cbind(as.vector(m1), as.vector(m2), as.vector(m3), as.vector(m4))
+   ll - vector('list', nrow(v))
+   for(i in seq_along(nrow(v))) ll[[i]] - fisher.test(matrix(v[i, ], nrow
= 2))
+
+   } )
   user  system elapsed
   0.430.000.43
 length(ll)
[1] 100

A second run in a fresh R session took 0.73 seconds elapsed.

When running a lot of tests like this, it's important to try out different
alternatives. My initial approach to this problem was to use apply() on the
rows of v. Testing it on my system when the input matrices were 100 x 100,
it took 11.23 seconds, so for the 1000 x 1000 input matrices you're using,
use of apply() would have taken around 20 minutes. In this particular case,
use of a loop to fill in the components of a pre-allocated list object with
the output of fisher.test() was much more efficient; in fact, the actual
data generation and reshaping took up a significant amount of the time!

Data generation and reshaping:
 system.time( {
+   m1 - matrix(rpois(100, 7), nrow = 1000)
+   m2 - matrix(rpois(100, 7), nrow = 1000)
+   m3 - matrix(rpois(100, 7), nrow = 1000)
+   m4 - matrix(rpois(100, 7), nrow = 1000)
+
+   v - cbind(as.vector(m1), as.vector(m2), as.vector(m3), as.vector(m4))
+   })
   user  system elapsed
   0.270.000.27

HTH,
Dennis


On Mon, Jan 3, 2011 at 6:57 AM, zhaoxing731 zhaoxing...@yahoo.com.cnwrote:

 Hello

 I have 4 1000*1000 matrix A,B,C,D. I want to use the corresponding element
 of the 4 matrices. Using the for loop as follow:

 E-o
 for (i in 1:1000)
{for (j in 1:1000)
  {
 E-fisher.test(matrix(c(A[i][j],B[i][j],C[i][j],D[i][j]),2))#call
 fisher.test for every element
  }
}

 It is so time-consuming
 Need vectorization

 Yours sincerely




 ZhaoXing
 Department of Health Statistics
 West China School of Public Health
 Sichuan University
 No.17 Section 3, South Renmin Road
 Chengdu, Sichuan 610041
 P.R.China

[[alternative HTML version deleted]]


 __
 8O?lW2aQE;3,4sH]A?Cb7QSJOd?

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