r - 计算 R 中大矩阵的零空间

标签 r matrix r-bigmemory large-data

我找不到任何函数或包来计算 bigmatrix 的零空间或(QR 分解) (来自 library(bigmemory) )在 R 中。例如:

library(bigmemory)

a <- big.matrix(1000000, 1000, type='double', init=0)

我尝试了以下操作,但显示了错误。如何找到 bigmemory 的零空间目的?
a.qr <- Matrix::qr(a)
# Error in as.vector(data) : 
#   no method for coercing this S4 class to a vector
q.null <- MASS::Null(a)
# Error in as.vector(data) : 
#   no method for coercing this S4 class to a vector

最佳答案

如果要计算矩阵的完整 SVD,可以使用包 bigstatsr 按块执行计算。一个 FBM代表文件支持的大矩阵,是一个类似于文件支持的对象 big.matrix包裹对象大内存 .

library(bigstatsr)
options(bigstatsr.block.sizeGB = 0.5)

# Initialize FBM with random numbers
a <- FBM(1e6, 1e3)
big_apply(a, a.FUN = function(X, ind) {
  X[, ind] <- rnorm(nrow(X) * length(ind))
  NULL
}, a.combine = 'c')

# Compute t(a) * a
K <- big_crossprodSelf(a, big_scale(center = FALSE, scale = FALSE))

# Get v and d where a = u * d * t(v) the SVD of a
eig <- eigen(K[])
v <- eig$vectors
d <- sqrt(eig$values)

# Get u if you need it. It will be of the same size of u
# so that I store it as a FBM.
u <- FBM(nrow(a), ncol(a))
big_apply(u, a.FUN = function(X, ind, a, v, d) {
  X[ind, ] <- sweep(a[ind, ] %*% v, 2, d, "/")
  NULL
}, a.combine = 'c', block.size = 50e3, ind = rows_along(u),
a = a, v = v, d = d)

# Verification
ind <- sample(nrow(a), 1000)
all.equal(a[ind, ], tcrossprod(sweep(u[ind, ], 2, d, "*"), v))

这在我的电脑上大约需要 10 分钟。

关于r - 计算 R 中大矩阵的零空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46253537/

相关文章:

arrays - Julia:我可以在 for 循环的迭代中更新和存储相同的数组吗?

c - 分配结构体矩阵

r - R 中的 big.matrix 作为 data.frame

r - 自定义类继承 `data.frame`及替换方法

在调用 mutate() 时引用上一列/下一列

c++ - 如何增加 Visual Studio C++ 中的堆栈大小?

r - 大矩阵和内存问题

r - 对 big.matrix 进行子集化的正确方法

r - 如何使用 GDB 在 Emacs 中调试 R 包(使用 C 代码)?

使用 `ctree` 包运行 `party`,列作为因子而不是字符