Tôi đã làm việc trên một số dữ liệu, có sẵn ở đây: Dropbox' csv file (hãy loại để sử dụng nó để nhân rộng lỗi).R cor trả về NaN đôi khi
Khi tôi chạy mã:
t<-read.csv("120.csv")
x<-NULL
for (i in 1:100){
x<-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete"))
}
sum(is.nan(x))
tôi nhận được các giá trị ngẫu nhiên của biểu thức cuối cùng, thường là khoảng 55 đến 60. Tôi hy vọng cor
để cho kết quả repetible, vì vậy tôi mong đợi x
là một vector có độ dài = 100 được làm bằng các giá trị giống hệt nhau. Xem, ví dụ: kết quả của hai lần chạy độc lập:
> x<-NULL; for (i in 1:100){x<-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete"))}
> sum(is.nan(x))
[1] 62
> head(x,10)
[1] NaN NaN 0.2967441 NaN 0.2967441 NaN NaN NaN
[9] 0.2967441 NaN
> x<-NULL; for (i in 1:100){x<-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete"))}
> sum(is.nan(x))
[1] 52
> head(x,10)
[1] 0.2967441 NaN NaN NaN NaN 0.2967441 0.2967441 NaN
[9] 0.2967441 0.2967441
>
Tôi tự hỏi nếu tôi đang làm điều gì đó sai ở đây, hoặc nếu đó là lỗi đã biết [n] [un]. Nếu đúng như vậy, tôi đánh giá cao nếu ai đó thận trọng hơn tôi giúp tôi báo cáo cho CRAN.
tôi đọc một rất cũ (2001) bài nơi hành vi tương tự đã được trưng bày bởi cor.test (xem cor.test produces NaN sometimes.
Tôi đánh giá cao những giải thích của các bạn, như tôi là một Noob để R. Cảm ơn!
mỗi Ben Góp ý:
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Spanish_Colombia.1252 LC_CTYPE=Spanish_Colombia.1252 LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Colombia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_0.6.2 digest_0.6.4 RCurl_1.95-4.3 bitops_1.0-6 qpcR_1.4-0 Matrix_1.1-4 robustbase_0.91-1 rgl_0.95.1157
[9] minpack.lm_1.1-8 MASS_7.3-35 plyr_1.8.1 swirl_2.2.16 ggplot2_1.0.0 lattice_0.20-29
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 DEoptimR_1.0-2 grid_3.1.1 gtable_0.1.2 httr_0.5 labeling_0.3 munsell_0.4.2 proto_0.3-10 Rcpp_0.11.3
[10] reshape2_1.4 scales_0.2.4 testthat_0.9.1 tools_3.1.1 yaml_2.1.13
Kết quả tìm ("cor"):
> find("cor")
[1] "package:stats"
---------- ### Thứ hai Chỉnh sửa ### --------
Tôi đã khởi động lại phiên (Tôi không tìm thấy cách vượt qua đối số --vanilla . Tôi đang sử dụng Rstudio), và đây là sessionInfo mới:
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Spanish_Colombia.1252 LC_CTYPE=Spanish_Colombia.1252 LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Colombia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.1.1
Tôi chạy các lệnh một lần nữa trong phiên làm việc mới, và vẫn nhận được sum (is.nan (x)) = 52 :(
Chỉ trong trường hợp đó là hữu ích:.
> cor
function (x, y = NULL, use = "everything", method = c("pearson",
"kendall", "spearman"))
{
na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs",
"everything", "na.or.complete"))
if (is.na(na.method))
stop("invalid 'use' argument")
method <- match.arg(method)
if (is.data.frame(y))
y <- as.matrix(y)
if (is.data.frame(x))
x <- as.matrix(x)
if (!is.matrix(x) && is.null(y))
stop("supply both 'x' and 'y' or a matrix-like 'x'")
if (!(is.numeric(x) || is.logical(x)))
stop("'x' must be numeric")
stopifnot(is.atomic(x))
if (!is.null(y)) {
if (!(is.numeric(y) || is.logical(y)))
stop("'y' must be numeric")
stopifnot(is.atomic(y))
}
Rank <- function(u) {
if (length(u) == 0L)
u
else if (is.matrix(u)) {
if (nrow(u) > 1L)
apply(u, 2L, rank, na.last = "keep")
else row(u)
}
else rank(u, na.last = "keep")
}
if (method == "pearson")
.Call(C_cor, x, y, na.method, FALSE)
else if (na.method %in% c(2L, 5L)) {
if (is.null(y)) {
.Call(C_cor, Rank(na.omit(x)), NULL, na.method, method ==
"kendall")
}
else {
nas <- attr(na.omit(cbind(x, y)), "na.action")
dropNA <- function(x, nas) {
if (length(nas)) {
if (is.matrix(x))
x[-nas, , drop = FALSE]
else x[-nas]
}
else x
}
.Call(C_cor, Rank(dropNA(x, nas)), Rank(dropNA(y,
nas)), na.method, method == "kendall")
}
}
else if (na.method != 3L) {
x <- Rank(x)
if (!is.null(y))
y <- Rank(y)
.Call(C_cor, x, y, na.method, method == "kendall")
}
else {
if (is.null(y)) {
ncy <- ncx <- ncol(x)
if (ncx == 0)
stop("'x' is empty")
r <- matrix(0, nrow = ncx, ncol = ncy)
for (i in seq_len(ncx)) {
for (j in seq_len(i)) {
x2 <- x[, i]
y2 <- x[, j]
ok <- complete.cases(x2, y2)
x2 <- rank(x2[ok])
y2 <- rank(y2[ok])
r[i, j] <- if (any(ok))
.Call(C_cor, x2, y2, 1L, method == "kendall")
else NA
}
}
r <- r + t(r) - diag(diag(r))
rownames(r) <- colnames(x)
colnames(r) <- colnames(x)
r
}
else {
if (length(x) == 0L || length(y) == 0L)
stop("both 'x' and 'y' must be non-empty")
matrix_result <- is.matrix(x) || is.matrix(y)
if (!is.matrix(x))
x <- matrix(x, ncol = 1L)
if (!is.matrix(y))
y <- matrix(y, ncol = 1L)
ncx <- ncol(x)
ncy <- ncol(y)
r <- matrix(0, nrow = ncx, ncol = ncy)
for (i in seq_len(ncx)) {
for (j in seq_len(ncy)) {
x2 <- x[, i]
y2 <- y[, j]
ok <- complete.cases(x2, y2)
x2 <- rank(x2[ok])
y2 <- rank(y2[ok])
r[i, j] <- if (any(ok))
.Call(C_cor, x2, y2, 1L, method == "kendall")
else NA
}
}
rownames(r) <- colnames(x)
colnames(r) <- colnames(y)
if (matrix_result)
r
else drop(r)
}
}
}
<bytecode: 0x0000000008ce0158>
<environment: namespace:stats>
Cảm ơn một lần nữa
Tôi đánh giá cao nếu ai đó có thể thêm thẻ "cor". Danh tiếng của tôi (vẫn dưới 1500) không cho phép tôi thêm các thẻ mới, và điều này tôi nghĩ là rất quan trọng đối với những người phải đối mặt với cùng một vấn đề. Cảm ơn! – PavoDive
FWIW có vẻ như sự cố cũ đã được khắc phục trong R 1.4.0 (!): Http://cran.r-project.org/src/base/NEWS.1 nói 'cor (*, use =" all.obs ") <= 1 bây giờ được đảm bảo để đảm bảo rằng sqrt (1 - r^2) luôn luôn là ok trong cor.test(). (PR # 1099) ' –
Tôi không thể tái tạo; Tôi luôn nhận được 'sum (is.nan (x))' bằng không. (1) thử bắt đầu trong một phiên R sạch (với '--vanilla' nếu có thể); (2) kết quả của 'sessionInfo()'? (3) kết quả của 'tìm (" cor ")'? –