我有一个100 K行的数据,我想要计算一个Cochran-Mantel-Haenszel测试。
我的变量是受教育程度和以分位数计算的分数,分组变量是性别,代码行如下所示:
mantelhaen.test(db$education, db$score.grouped, db$sex)
此代码引发此错误并发出警告:
qr.default(a,tol = tol)中的错误:外部函数调用中的NA/NaN/Inf (arg 1) 此外:警告消息: ntot *rowsum:整数溢出产生的NAs
这个错误似乎是由我的第一个变量引起的,因为在测试的7个变量中,我只遇到了其中两个变量的问题,这似乎没有明显的共同之处。
缺失的值和因子级别似乎在抛出错误的变量和不抛出错误的变量之间没有区别。我尝试了完整的情况(使用na.omit
),问题仍然存在。
是什么触发了这个错误?这是否意味着?
我怎么才能摆脱它?
有趣的帖子:外部函数调用中的NA/NaN/Inf (第1条),什么是R中的整数溢出,它如何发生?
增编:这是str
的结果(失败是education
和imc.cl
):
str(db[c("education","score.grouped","sex", ...)])
'data.frame': 104382 obs. of 7 variables:
$ age.cl: Ord.factor w/ 5 levels "<30 ans"<"30-40 ans"<..: 5 2 1 1 3 4 2 3 4 4 ...
..- attr(*, "label")= chr "age"
$ emploi2 : Factor w/ 8 levels "Agriculteurs exploitants",..: 3 5 6 8 8 8 8 3 3 3 ...
..- attr(*, "label")= chr "CSP"
$ tabac : Factor w/ 4 levels "ancien fumeur",..: 4 1 4 4 3 4 4 1 4 4 ...
..- attr(*, "label")= chr "tabac"
$ situ_mari2 : Factor w/ 3 levels "Vit seul","Divorsé, séparé ou veuf",..: 3 2 1 1 1 3 1 3 2 3 ...
..- attr(*, "label")= chr "marriage"
$ education : Factor w/ 3 levels "Universitaire",..: 1 1 1 1 3 1 1 1 1 1 ...
$ revenu.cl : Factor w/ 4 levels "<1800 euros/uc",..: 3 4 2 NA 4 1 1 4 4 1 ...
$ imc.cl : Ord.factor w/ 6 levels "Maigre"<"Normal"<..: 2 2 1 2 3 1 3 2 2 3 ...
..- attr(*, "label")= chr "IMC"
编辑:通过在函数内部跳转,错误和警告是由调用qr.solve
引起的。我不明白这件事,但我会试着往下跳
EDIT2:在qr.solve
中,错误由对.F_dqrdc2
的Fortran
调用引发。这超出了我的水平,我的鼻子开始流血了。
EDIT3:我试图对数据进行head
以确定哪一行是原因所在:
db2 = db %>% head(99787) #fails at 99788
db2 = db %>% tail(99698) #fails at 99699
mantelhaen.test(db2$education, db2$score.grouped, db2$sex)
这给我的信息不多,但也许可以给你。
发布于 2018-01-24 09:40:41
我能够通过扩大数据集来复制这个问题。
set.seed(101); n <- 500000
db <- data.frame(education=
factor(sample(1:3,replace=TRUE,size=n)),
score=
factor(sample(1:5,replace=TRUE,size=n)),
sex=
sample(c("M","F"),replace=TRUE,size=n))
在此之后,mantelhaen.test(db$education, db$score, db$sex)
给出了报告的错误。
值得庆幸的是,真正的问题并不在QR分解代码的内部:而是发生在QR分解之前设置一个矩阵时。有两种计算,ntot*colsums
和ntot*rowsums
,使R的整数计算能力溢出。有一种相对简单的方法可以通过创建函数的修改版本来解决这个问题:
dump("mantelhaen.test",file="my_mh.R")
ntot
更改为as.numeric(ntot)
,在溢出发生之前将整数转换为双精度
source("my_mh.R")
现在
my_mantelhaen.test(db$education, db$score, db$sex)
应该行得通。为了确保得到相同的答案,您肯定应该对照旧函数测试新函数。
现在发到R错误列表上,我们看看会发生什么.
更新2018年5月11日:这是修正了R的开发版本 (3.6be)。
https://stackoverflow.com/questions/48422398
复制相似问题