警告消息:“在 cor(...) 中:标准偏差为零”
我有一个流数据向量(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一些想法。
首先,通过使用
apply()
,您可以将嵌套循环替换为如下所示:其次,看起来 >31% (
596849/(360*180*29)
ssta_winter
中的点的 (code>) 为NaN
或(可能)NA_real_
。考虑到在包含单个NaN
的向量上计算的相关性的返回值,所有这些
NaN
是否都可能导致cor_ScottsCk_SF_SST_JJA
填充NA
?第三,正如警告消息清楚地告诉您的那样,您传递给 cor() 的某些向量的方差为零。它们与 NaN 无关:如下所示,当涉及 NaN 时,R 不会抱怨标准差为 0。 (这也是非常明智的,因为您无法计算未定义数字的标准差):
A few thoughts.
First, by using
apply()
, you can replace that nested loop with something like this:Second, it appears that >31% (
596849/(360*180*29)
) of the points inssta_winter
areNaN
or (possibly)NA_real_
. Given the return value of a correlation calculated on vectors that contain even a singleNaN
,isn't it likely that all those
NaN
s are causingcor_ScottsCk_SF_SST_JJA
to be filled withNA
s?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 theNaN
s: as the following shows, R doesn't complain about standard deviations of 0 whenNaN
are involved. (Quite sensibly too, since you can't calculate standard deviations for undefined numbers):如果某一列的所有观测值都具有相同的值,也可能会显示此错误。因此,您可能想要删除这些行。
This error might also be shown if a column has the same values for all observations. So, you might want to remove those rows.
以下使用
library("psych")
sd 包含 SAT 的 NA。
子集已删除 NA
The following uses
library("psych")
sd contains NA for SAT.
subset has NA's removed
如果您的数据对于其存储格式来说太大,也可能会出现此警告。例如,如果您有数万亿的数据(也许是 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.
这样的解决方案可能有效
such a solution might work