gpt4 book ai didi

r - 大矩阵的线性回归

转载 作者:行者123 更新时间:2023-12-04 17:45:27 24 4
gpt4 key购买 nike

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

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com