在R中绘制相关矩阵

时间:2014-05-20 20:12:29

标签: r plot

我有一个包含矩阵的csv文件:

version getSize() length() ... power
0         23000    23421        0.8
0           ..      ..           ..
1           ..      ..           ..
1           ..      ..           ..

我希望通过将均值函数应用于列的类似版本进行聚合。列太多而无法编写它们。我还想计算相关矩阵并在图的两侧绑定电源列。我的代码是这样的:

matrix <- read.csv("/home/francesco/University/UoA/matrix.csv", header=TRUE, sep=",", fileEncoding="windows-1252")
power <- matrix[,"power"]
binded <- cbind(matrix,power)
aggregated <- aggregate(. ~ version, data = binded, mean)
corMatrix <- cor(aggregated, method="spearman")
library(lattice)
levelplot(corMatrix)

情节很混乱,我收到了这个警告:

Warning message:
In cor(aggregated, method = "spearman") : standard deviation is zero

matrix.csv的简短摘录是:

version,native_drawBitmap,nPrepareDirty,nDrawDisplayList,startGC,power
00083,8,88,308,12,0.8967960131052847
00083,0,176,404,1,0.867644513259528
00084,8,88,307,10,0.8980234065469381
00084,0,181,408,1,0.871799879659241

有人知道我做错了吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

好吧,使用您的示例数据,native_drawBitmap列将全部变为4。由于这没有任何差异,因此您无法计算与任何其他变量的成对相关性,并且您会收到错误。如果你遗漏这个专栏,那就可以了。这是一个例子。

#sample data in friendly copy/paste-able format
mm<-data.frame(
    version = c(83, 83, 84, 84), 
    native_drawBitmap = c(8, 0, 8, 0),
    nPrepareDirty = c(88, 176, 88, 181), 
    nDrawDisplayList = c(308, 404, 307, 408), 
    startGC = c(12, 1, 10, 1), 
    power = c(0.896796013105285, 0.867644513259528, 
        0.898023406546938, 0.871799879659241)
)

# these are not needed and don't make sence. Why are you
#trying to re-add the column from mm back onto mm?
# power <- mm[,"power"]
# binded <- cbind(mm,power)
aggregated <- aggregate(. ~ version, data = mm, mean)

#error
corMatrix <- cor(aggregated, method="spearman")
#no error
corMatrix <- cor(aggregated[,-2], method="spearman")

您的数据中可能有其他列在聚合后没有可变性。一定要找到并删除它们。