相关系数计算中缺失值的处理问题
问题如下:当use=“complete.obs”时,空值的地方会被casewise deletion( If use is "complete.obs" then missing values are handled by casewise deletion (and if there are no complete cases, that gives an error). )但计算出来的相关系数矩阵是完整的没有缺失值的,那么那些空值的地方的值是用什么方法填补的呢?
有缺失值,是不填补的。以下图为例:
1.如果是complete的情况,那么进行计算时,采用的是下列情况:
计算出来的相关系数
- > cp
- x y z
- 1 1 3 4
- 2 2 4 5
- 3 5 8 9
- 4 7 6 12
- > zz
- x y z
- 1 1 3 4
- 2 2 4 5
- 3 3 NA 6
- 4 4 7 NA
- 5 5 8 9
- 6 NA NA 10
- 7 6 NA 11
- 8 7 6 12
- > cor(zz,use="complete.obs")
- x y z
- x 1.0000000 0.7779078 0.9986590
- y 0.7779078 1.0000000 0.7522874
- z 0.9986590 0.7522874 1.0000000
- > cor(cp)
- x y z
- x 1.0000000 0.7779078 0.9986590
- y 0.7779078 1.0000000 0.7522874
- z 0.9986590 0.7522874 1.0000000
2.如果是pairwise.complete.obs,那么计算,对应的应该就是下列方式:
代码示例如下:
- > cor(zz,use="pairwise.complete.obs")
- x y z
- x 1.0000000 0.7372609 0.9953212
- y 0.7372609 1.0000000 0.7522874
- z 0.9953212 0.7522874 1.0000000
- >
- > xy<-na.omit(zz[,1:2])
- > xy
- x y
- 1 1 3
- 2 2 4
- 4 4 7
- 5 5 8
- 8 7 6
- > cor(xy)
- x y
- x 1.0000000 0.7372609
- y 0.7372609 1.0000000
嗯,就这么多了,欢迎讨论!