首页 > 解决方案 > 卡方模拟错误 - 无法分配大小为 134217728 Tb 的内存块

问题描述

我正在对我的数据运行以下函数,并且不断收到与内存块大小有关的错误。我的数据不是那么大(800 行,3 列),并且该功能仅在我将其减少到 300 行时才有效。

SSAS <- function(x, conf.int = 0.95, B = 10000)
{
x <- as.matrix(x)
nr <- nrow(x)
nc <- ncol(x)
sr <- rowSums(x)
sc <- colSums(x)
n <- sum(x)
E <- outer(sr, sc, "*")/n
dimnames(E) <- dimnames(x)
tmp <- .Call(stats:::C_chisq_sim, sr, sc, B, E)
obs <- sum(sort((x - E)^2/E, decreasing = TRUE))/n
sim <- tmp/n
p0 <- (1 - conf.int)/2
return(c(obs, quantile(sim, p0), quantile(sim, 1 -p0)))
}

错误:

FUN(X[[i]], ...) 中的错误:无法分配大小为 134217728 Tb 的内存块

但是,当我使用提供的较大的试验数据(超过 1000 行)时,该功能可以正常工作。

cap <- read.table("http://pbil.univ-lyon1.fr/R/donnees/mfdcapreolus.txt",
h = T)

l1 <- splitmfd(cap)
w <- matrix(unlist(lapply(l1, SSAS)), ncol = 3, byrow = T)

(分裂)

splitmfd <- function(mfd) {
 loca1 <- function(x) {
  x <- t(x[, 1:2])
  dimnames(x) <- list(c("mal", "fem"), as.character(1:ncol(x)))
  x
 }
 l0 <- split(mfd, mfd$mon)
 lapply(l0, loca1)
}

我的数据如下所示:(而不是样本中的第 1-12 个月,我将其分为 3 个时期 - Prenurse、Nurse 和 PostNurse)

> head(groups)
  X n_male_mat n_fem_mat  PERIOD
1 1          0        22 PRENURS
2 2          0        14 PRENURS
3 3          4        19 PRENURS
4 4          1         0 PRENURS
5 5          0        21   NURSE
6 6          0        11   NURSE

标签: rsimulationchi-squared

解决方案


推荐阅读