r - 大矩阵的线性回归

标签 r r-bigmemory

我想用大矩阵执行线性回归。

这是我迄今为止尝试过的:

library(bigmemory)
library(biganalytics)
library(bigalgebra)

nrows <- 1000000
X <- as.big.matrix( replicate(100, rnorm(nrows)) )
y <- rnorm(nrows)

biglm.big.matrix(y ~ X)
# Error in CreateNextDataFrameGenerator(formula, data, chunksize, fc, getNextChunkFunc,  : 
  argument "data" is missing, with no default

biglm.big.matrix(y ~ X, data = cbind(y, X))
# Error in bigmemory:::mmap(vars, colnames(data)) : 
  Couldn't find a match to one of the arguments.

biglm.big.matrix(y ~ X, data = cbind(y = y, X = X))
# Error in bigmemory:::mmap(vars, colnames(data)) : 
  Couldn't find a match to one of the arguments.

我怎么解决这个问题?

最佳答案

在这里,X是一个有 100 列的(大)矩阵。自 biglm.big.matrix()需要 data=参数,看起来您不能要求该函数在 X 中的所有列上运行线性模型立即使用 lm() .另请注意,当您 cbind()一个带big.matrix ,如 cbind(y, X) ,结果是 list !!.

看来你同时需要 yX成为其中一员 big.matrix ,那么您需要自己手动构建模型公式:

library(bigmemory)
library(biganalytics)
library(bigalgebra)

# Construct an empty big.matrix with the correct number of dimensions and
# with column names
nrows <- 1000000
dat <- big.matrix(nrow=nrows, ncol=101, 
                  dimnames=list(
                    NULL, # no rownames
                    c("y", paste0("X", 1:ncol(X))) # colnames: y, X1, X2, ..., X100
                  ))

# fill with y and X:
dat[,1] <- rnorm(nrows)
dat[,2:101] <- replicate(100, rnorm(nrows)) 

# construct the model formula as a character vector using paste:
# (Or you need to type y ~ X1 + X2 + ... + X100 manually in biglm.big.matrix()!)
f <- paste("y ~", paste(colnames(dat)[-1], collapse=" + "))

# run the model
res <- biglm.big.matrix(as.formula(f), data=dat)
summary(res)

关于r - 大矩阵的线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52272703/

相关文章:

c++ - 通过 C 中的指针从 R 中的 big.matrix 访问一 block 内存

r - 如何连接(合并)数据框(内部、外部、左、右)

r - ggplot2:如何向圆环图添加百分比标签

rmarkdown : kable, Word 文档中的 xtable 或 tab_df 表

读取列中包含逗号的 CSV 文件

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

r - 将矩阵列表组合成一个 big.matrix

regex - 在 R 中拆分以数字结尾的字符串

r - R中的并行foreach共享内存

c - R包开发: how to check whether the type of SEXP is "big.matrix"?