3

I'm new in the big.matrix and related packages, I try to reproduce the following example ```

Loading required package: stats
> Sys.setenv(LANG = "en")
> library(bigmemory)
Loading required package: bigmemory.sri

bigmemory >= 4.0 is a major revision since 3.1.2; please see package
biganalytics and http://www.bigmemory.org for more information.

> x <- big.matrix(5, 2, type="integer", init=0, dimnames=list(NULL, c("alpha", "beta")))
> x[,] <- round(rnorm(10))
Assignment will down cast from double to integer
Hint: To remove this warning type:  options(bigmemory.typecast.warning=FALSE)
Mensajes de aviso perdidos
In SetAll.bm(x, value) : 
> x
An object of class "big.matrix"
Slot "address":
<pointer: 0x22a1620>

> x[,]
     alpha beta
[1,]    -2    0
[2,]    -1    0
[3,]     0   -1
[4,]     2    1
[5,]     0    0
> apply(x, 1, mean)
Error en as.vector(data) : 
  ningún método para coaccionar a esta clase S4 a un vector

from the documentation, but the last line gives me the following error:

Error en as.vector(data) : 
  ningún método para coaccionar a esta clase S4 a un vector

The last line says something like "there is no method for transform this S4 class to a vector"

Could you give me a pointer?

My R version is

R.version
               _                            
platform       x86_64-unknown-linux-gnu     
arch           x86_64                       
os             linux-gnu                    
system         x86_64, linux-gnu            
status                                      
major          2                            
minor          15.1                         
year           2012                         
month          06                           
day            22                           
svn rev        59600                        
language       R                            
version.string R version 2.15.1 (2012-06-22)
nickname       Roasted Marshmallows    
cdeterman
  • 19,630
  • 7
  • 76
  • 100
nanounanue
  • 7,942
  • 7
  • 41
  • 73
  • I think it is better to set (temporary ) your environment language to English , e.g Sys.setenv(LANG = "en"), to get more help. – agstudy Dec 24 '12 at 17:03
  • Thanks for yous suggestion, but the error still appears in spanish, see the edited question above... – nanounanue Dec 24 '12 at 17:12
  • hum..are you sure? see this question http://stackoverflow.com/questions/13575180/how-to-change-the-language-of-errors-in-r for more details – agstudy Dec 24 '12 at 17:14
  • Thanks for the link, let me find what's wrong with my env... – nanounanue Dec 24 '12 at 17:30

3 Answers3

2

You try to call apply in a bigmemory object. The latter don't have an implicit method to convert to a matrix(argument needed by apply)

apply(x, 1, mean)
Error in as.vector(data) : 
  no method for coercing this S4 class to a vector

Forcing the conversion to a matrix, correct the problem

apply(as.matrix(x), 1, mean)
[1] -1.5 -0.5  1.0 -0.5 -0.5

Edit after OP answer:

The package biganalytics extends the bigmemory package with various analytics. Functions bigkmeans and binit may also be used with native R objects. But be careful:

apply for big.matrix objects. Note that the performance may be degraded (compared to apply with regular R matrices) because of S4 overhead associated with extracting data from big.matrix objects. This sort of limitation is unavoidable and would be the case (or even worse) with other "custom" data structures. Of course, this would only be partically significant if you are applying over lengthy rows or columns.

For tapply-like functions, the bigtabulate package may also be helpful. The idea with this package is to do the job in 2 steps.

We have found that bigsplit followed by lapply or sapply can be particularly effective, when the subsets produced by the split are of reasonable size.

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • You are right, but I think that transform the `big.matrix` to `matrix` defeats all the purpose of the `big.memory` package... – nanounanue Dec 24 '12 at 17:15
  • since you put me in the right direction... Do you want to change your answer to incorporate the `biganalytics`package (see my answer below) , so I can close this question as answered by you? – nanounanue Dec 24 '12 at 17:21
  • No. it is answered by you. It is encouraged in SO. you can check your answer and close the topic. – agstudy Dec 24 '12 at 17:24
  • I agree that when in doubt it's nice to give credit to the lower-rep person. On the other hand, this answer actually answers the original question. Since agstudy doesn't mind, I would suggest editing the original question slightly so that it doesn't just ask "what's wrong?", but rather "what's wrong ... and how do I achieve the goal of calculating row means?" – Ben Bolker Dec 24 '12 at 17:26
  • PS: I would also like to see a pointer/explanation of why you thought `apply` should work/whether it is **explicitly** suggested in the docs ... I don't see it? (There is supposedly an `apply` method, but I don't see an explicit example using it ...) – Ben Bolker Dec 24 '12 at 17:33
  • @BenBolker you're right. I think it is better to change the question, to something like "how to call t-appaly function to a bigmemory object?" – agstudy Dec 24 '12 at 17:40
2

Well, I discover the error thanks to the previous answer (#agstudy, I give you a +1)... The apply method was from the basepackage, If I load the biganalyticspackages, everything works as charm...

> library(biganalytics)
> apply(x, 1, mean)
[1]  0.0  1.5  0.5 -1.0  0.5

Thank you again!

nanounanue
  • 7,942
  • 7
  • 41
  • 73
0

This answer is slightly off-topic from the original question, not "why doesn't apply(...) work?", which @agstudy answered above, but "how do I get row means for a bigmemory object?" I googled "r bigmemory rowmeans" and ended up here: http://www.stat.yale.edu/~jay/HPCwR/examples/bioinfo/bioinfo3.txt

Reproducing an interesting snippet:

# Get the row means, three different ways.

system.time({
  a <- rep(0, nrow(z))
  for (i in 1:nrow(z)) {
    a[i] <- mean(z[i,])
  }
}) # Will definitely work on both matrix and big.matrix
   # matrix timing: about 30 seconds
   # big.matrix timing: about 270 seconds
   #    The price for using bigmemory with lots of very small
   #    operations is the overhead of S3/S4 dispatch.

system.time({
  a <- apply(z, 1, mean)
}) # Works on a matrix only; interesting that it is slower.
   # matrix timing: 45 seconds 

system.time({
  myfunc <- function(i) return(mean(z[i,]))
  a <- sapply(1:nrow(z), myfunc)
}) # Will definitely work on both matrix and big.matrix
   # matrix timing:     About 40 seconds
   # big.matrix timing: About 306 seconds

The example goes on to show how to use parallel methods (doMC etc.) to compute row means.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453