警告消息:“在 cor(...) 中:标准偏差为零”

发布于 2025-01-02 06:20:03 字数 1516 浏览 1 评论 0原文

我有一个流数据向量(29 个数据)和一个 3D 矩阵数据(360*180*29),

我想找到单个向量和 3D 向量之间的相关性。相关矩阵的大小为 360*180。

> str(ScottsCk_flow_1981_2010_JJA)
 num [1:29] 0.151 0.644 0.996 0.658 1.702 ...
> str(ssta_winter)
 num [1:360, 1:180, 1:29] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
> summary(ssta_winter)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
    -2.8     -0.2      0.1      0.2      0.6      6.0 596849.0 

以上是向量和3D矩阵的结构。 3D 矩阵有很多空值。

> for (i in 1:360) {
+   for(j in 1:180){
+       cor_ScottsCk_SF_SST_JJA[i,j] = cor(ScottsCk_flow_1981_2010_JJA,ssta_winter[i,j,]) 
+    }
+ }
There were 50 or more warnings (use warnings() to see the first 50)

上面这部分代码就是寻找相关性的代码。但它

> warnings()
Warning messages:
1: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
2: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
3: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
4: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
5: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero

也给出了警告,相关矩阵的结果都是 NULL。这是怎么发生的?

> str(cor_ScottsCk_SF_SST_JJA)
 num [1:360, 1:180] NA NA NA NA NA NA NA NA NA NA ...

我使用了完全相同的代码 bfr 和 350 流向量和 360*180*350 矩阵。 这段代码完美运行。

I have a single vector of flow data (29 data) and a 3D matrix data(360*180*29)

i want to find the correlation between single vector and 3D vector. The correlation matrix will have a size of 360*180.

> str(ScottsCk_flow_1981_2010_JJA)
 num [1:29] 0.151 0.644 0.996 0.658 1.702 ...
> str(ssta_winter)
 num [1:360, 1:180, 1:29] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
> summary(ssta_winter)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
    -2.8     -0.2      0.1      0.2      0.6      6.0 596849.0 

This above is the structure of the vector and 3D matrix. 3D matrix has many values as Null.

> for (i in 1:360) {
+   for(j in 1:180){
+       cor_ScottsCk_SF_SST_JJA[i,j] = cor(ScottsCk_flow_1981_2010_JJA,ssta_winter[i,j,]) 
+    }
+ }
There were 50 or more warnings (use warnings() to see the first 50)

This part of code above is the code to find correlation. But it gives waring as

> warnings()
Warning messages:
1: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
2: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
3: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
4: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero
5: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j,  ... :
  the standard deviation is zero

also, the result of the correlation matrix is all NULL. how did this happen?

> str(cor_ScottsCk_SF_SST_JJA)
 num [1:360, 1:180] NA NA NA NA NA NA NA NA NA NA ...

I have used exact same code bfr with 350 flow vector and 360*180*350 matrix.
This code works perfectly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

优雅的叶子 2025-01-09 06:20:03

一些想法。

首先,通过使用 apply(),您可以将嵌套循环替换为如下所示:

cor_ScottsCk_SF_SST_JJA <- 
    apply(ssta_winter, MARGIN = 1:2, FUN = cor, ScottsCk_flow_1981_2010_JJA)

其次,看起来 >31% (596849/(360*180*29) ssta_winter 中的点的 (code>) 为 NaN 或(可能)NA_real_。考虑到在包含单个 NaN 的向量上计算的相关性的返回值,

cor(c(1:3, NaN), c(1:4))
# [1] NA

所有这些 NaN 是否都可能导致 cor_ScottsCk_SF_SST_JJA 填充NA

第三,正如警告消息清楚地告诉您的那样,您传递给 cor() 的某些向量的方差为零。它们与 NaN 无关:如下所示,当涉及 NaN 时,R 不会抱怨标准差为 0。 (这也是非常明智的,因为您无法计算未定义数字的标准差):

cor(c(NaN, NaN, NaN, NaN), c(1,1,1,1))
# [1] NA

cor(c(1,1,1,1), c(1,2,3,4))
# [1] NA
# Warning message:
# In cor(c(1, 1, 1, 1), c(1, 2, 3, 4)) : the standard deviation is zero

A few thoughts.

First, by using apply(), you can replace that nested loop with something like this:

cor_ScottsCk_SF_SST_JJA <- 
    apply(ssta_winter, MARGIN = 1:2, FUN = cor, ScottsCk_flow_1981_2010_JJA)

Second, it appears that >31% (596849/(360*180*29)) of the points in ssta_winter are NaN or (possibly) NA_real_. Given the return value of a correlation calculated on vectors that contain even a single NaN,

cor(c(1:3, NaN), c(1:4))
# [1] NA

isn't it likely that all those NaNs are causing cor_ScottsCk_SF_SST_JJA to be filled with NAs?

Third, as the warning messages plainly tell you, some of the vectors you are passing to cor() have zero variance. They have nothing to do with the NaNs: as the following shows, R doesn't complain about standard deviations of 0 when NaN are involved. (Quite sensibly too, since you can't calculate standard deviations for undefined numbers):

cor(c(NaN, NaN, NaN, NaN), c(1,1,1,1))
# [1] NA

cor(c(1,1,1,1), c(1,2,3,4))
# [1] NA
# Warning message:
# In cor(c(1, 1, 1, 1), c(1, 2, 3, 4)) : the standard deviation is zero
貪欢 2025-01-09 06:20:03

如果某一列的所有观测值都具有相同的值,也可能会显示此错误。因此,您可能想要删除这些行。

This error might also be shown if a column has the same values for all observations. So, you might want to remove those rows.

方觉久 2025-01-09 06:20:03

以下使用 library("psych")

partial.r(sd,c("GPA","SAT"),"GRADE1",use = "complete.obs")
Warning Message:
 In cor(data, use = use, method = method) : the standard deviation is zero

sd 包含 SAT 的 NA。

partial.r(subset,c("GPA","SAT"),"GRADE1", use = "complete.obs")
no warnings

子集已删除 NA

The following uses library("psych")

partial.r(sd,c("GPA","SAT"),"GRADE1",use = "complete.obs")
Warning Message:
 In cor(data, use = use, method = method) : the standard deviation is zero

sd contains NA for SAT.

partial.r(subset,c("GPA","SAT"),"GRADE1", use = "complete.obs")
no warnings

subset has NA's removed

揪着可爱 2025-01-09 06:20:03

如果您的数据对于其存储格式来说太大,也可能会出现此警告。例如,如果您有数万亿的数据(也许是 GDP)并且数据存储为 32 位整数, R 可能会将它们识别为数字,即使它无法使用该格式的数据进行计算(这就是为什么它认为标准差为 0,即使您的所有值都不同)。

如果是这种情况,当您查看数据并将鼠标悬停在列标题上时,您可能会看到类似“X 列:范围未定义的数字 - 未定义”之类的消息。

在这种情况下,除以一个常数(例如 1,000,000)来减小值的大小可以解决该问题。

This warning can also occur if your data are too large for the format that they're being stored in. For example, if you have data in the trillions (for GDP, perhaps) and the data are being stored as 32-bit integers, R may recognize them as numeric, even though it's unable to do calculations with the data in that format (which is why it thinks the standard deviation is 0, even though all of your values are different).

If this is the case, when you view your data and hover over the column title, you might see a message like "column X: numeric with range undefined - undefined."

In this situation, dividing by a constant (say 1,000,000) to reduce the size of the values can fix the issue.

温柔戏命师 2025-01-09 06:20:03

这样的解决方案可能有效

x <- rep(1, 5)    
y <- 1:5   

if(sd(x) == 0){
  print("denem")
  x[1] <- x[1] * .99
}
cor(x,y)

such a solution might work

x <- rep(1, 5)    
y <- 1:5   

if(sd(x) == 0){
  print("denem")
  x[1] <- x[1] * .99
}
cor(x,y)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文