R-警告消息:“在cor(...):标准偏差为零". [英] R - Warning message: "In cor(...): the standard deviation is zero"

查看:1754
本文介绍了R-警告消息:“在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矩阵具有许多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)

上面的代码部分是查找相关性的代码.但这警告为

> 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 ...

我使用了与350流向量和360 * 180 * 350矩阵完全相同的代码bfr. 这段代码完美地工作.

解决方案

一些想法.

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

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

其次,看来ssta_winter中的点中> 31%(596849/(360*180*29))是NaN或(可能)是NA_real_.给定基于甚至包含单个NaN的向量计算出的相关性的返回值,

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

是否所有这些NaN都导致cor_ScottsCk_SF_SST_JJANA填充?

第三,正如警告消息清楚地告诉您的那样,您传递给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

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.

解决方案

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

这篇关于R-警告消息:“在cor(...):标准偏差为零".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆