计算列的所有因子级别的相关矩阵

时间:2015-12-15 18:42:41

标签: r

如何为分类变量的每个级别计算相关矩阵,其中相关性是其余列中的所有变量?

对于下面的例子,这意味着每个q1水平的q2到q10之间的相关性。

A<-data.frame(q1=c("A","A","A","D","D","D","F","F","F","G","G","G","G","A","D"), 
      q2=sample(1:70, 15, rep=1), 
      q3=sample(1:60, 15, rep=1), 
      q4=sample(1:60, 15, rep=1), 
      q5=sample(1:103, 15, rep=1), 
      q6=sample(1:50, 15, rep=1), 
      q7=sample(1:101, 15, rep=1), 
      q8=sample(1:40, 15, rep=1), 
      q9=sample(1:100, 15, rep=1), 
      q10=sample(1:200, 15, rep=1))

另外,如何在情节中显示这些相关性?

3 个答案:

答案 0 :(得分:2)

如果我理解正确,您需要n个相关矩阵,每个级别q1都需要一个。

如果是这种情况,您可以使用简单的for循环来获取它们:

c <- list()
for(q in unique(data$q1)) {
  c[[q]] <- cor(data[data$q1 == q,2:10])
}

c列表将包含每个级别q1

的条目

答案 1 :(得分:2)

correlations <- lapply(split(A, A$q1), function(x) cor(x[,-1]))

> correlations
$A
            q2          q3          q4          q5         q6         q7          q8          q9         q10
q2   1.0000000 -0.44803503  0.50600715  0.93230347  0.4369729 -0.1247089  0.83257155  0.47128720 -0.40309437
q3  -0.4480350  1.00000000  0.53166771 -0.20412996 -0.9404926 -0.7119750  0.06469342 -0.36555367  0.95642170
q4   0.5060071  0.53166771  1.00000000  0.71679818 -0.5384897 -0.7141115  0.87705638 -0.04306433  0.57753545
q5   0.9323035 -0.20412996  0.71679818  1.00000000  0.1162383 -0.1865482  0.96352065  0.16325848 -0.08958274
q6   0.4369729 -0.94049256 -0.53848969  0.11623826  1.0000000  0.4923609 -0.13194449  0.64332166 -0.99763177
q7  -0.1247089 -0.71197500 -0.71411145 -0.18654820  0.4923609  1.0000000 -0.39564648 -0.34010040 -0.55069571
q8   0.8325715  0.06469342  0.87705638  0.96352065 -0.1319445 -0.3956465  1.00000000  0.08376171  0.16455409
q9   0.4712872 -0.36555367 -0.04306433  0.16325848  0.6433217 -0.3401004  0.08376171  1.00000000 -0.59118795
q10 -0.4030944  0.95642170  0.57753545 -0.08958274 -0.9976318 -0.5506957  0.16455409 -0.59118795  1.00000000

$D
             q2         q3          q4         q5          q6         q7          q8         q9         q10
q2   1.00000000 -0.6763867  0.95671018  0.9704899 -0.04887372  0.3729085  0.83267631 -0.2684279 -0.38379940
q3  -0.67638669  1.0000000 -0.58384573 -0.6513078 -0.60950525 -0.3943479 -0.62258512  0.8898461  0.69153886
q4   0.95671018 -0.5838457  1.00000000  0.8620453  0.01355672  0.5925750  0.63735667 -0.1899837 -0.51516909
q5   0.97048986 -0.6513078  0.86204530  1.0000000 -0.17072346  0.1417973  0.93960117 -0.2401559 -0.19646514
q6  -0.04887372 -0.6095053  0.01355672 -0.1707235  1.00000000  0.5915035 -0.23682009 -0.8533831 -0.83975896
q7   0.37290854 -0.3943479  0.59257503  0.1417973  0.59150349  1.0000000 -0.17116412 -0.3371096 -0.90364809
q8   0.83267631 -0.6225851  0.63735667  0.9396012 -0.23682009 -0.1711641  1.00000000 -0.2699661  0.02392984
q9  -0.26842790  0.8898461 -0.18998368 -0.2401559 -0.85338309 -0.3371096 -0.26996613  1.0000000  0.70740850
q10 -0.38379940  0.6915389 -0.51516909 -0.1964651 -0.83975896 -0.9036481  0.02392984  0.7074085  1.00000000

$F
            q2         q3          q4         q5         q6         q7         q8         q9         q10
q2   1.0000000  0.1439251 -0.36801650 -0.7892030 -0.4045654 -0.9979487  0.8095933  0.9463303  0.96088598
q3   0.1439251  1.0000000  0.86717169  0.4941522  0.8467606 -0.2069818 -0.4643589 -0.1836355  0.41235657
q4  -0.3680165  0.8671717  1.00000000  0.8614718  0.9992149  0.3077360 -0.8437394 -0.6487839 -0.09611375
q5  -0.7892030  0.4941522  0.86147184  1.0000000  0.8809138  0.7482684 -0.9994238 -0.9453351 -0.58825368
q6  -0.4045654  0.8467606  0.99921489  0.8809138  1.0000000  0.3451900 -0.8643422 -0.6784229 -0.13547310
q7  -0.9979487 -0.2069818  0.30773601  0.7482684  0.3451900  1.0000000 -0.7703543 -0.9236982 -0.97664447
q8   0.8095933 -0.4643589 -0.84373942 -0.9994238 -0.8643422 -0.7703543  1.0000000  0.9558589  0.61536300
q9   0.9463303 -0.1836355 -0.64878390 -0.9453351 -0.6784229 -0.9236982  0.9558589  1.0000000  0.81980673
q10  0.9608860  0.4123566 -0.09611375 -0.5882537 -0.1354731 -0.9766445  0.6153630  0.8198067  1.00000000

$G
             q2         q3         q4         q5         q6         q7          q8         q9        q10
q2   1.00000000 -0.5672060  0.9137473  0.9221046  0.4872684 -0.4363765  0.06508126  0.4942240 -0.4619742
q3  -0.56720597  1.0000000 -0.6033027 -0.2675167  0.1195274  0.7432619 -0.85226036 -0.0813887  0.9611281
q4   0.91374727 -0.6033027  1.0000000  0.9014109  0.6820842 -0.7236664  0.21082792  0.7548119 -0.4087825
q5   0.92210460 -0.2675167  0.9014109  1.0000000  0.7670341 -0.3702843 -0.21819900  0.7237786 -0.1032187
q6   0.48726838  0.1195274  0.6820842  0.7670341  1.0000000 -0.4108090 -0.35300010  0.9657497  0.3702882
q7  -0.43637648  0.7432619 -0.7236664 -0.3702843 -0.4108090  1.0000000 -0.70799246 -0.6287476  0.5407077
q8   0.06508126 -0.8522604  0.2108279 -0.2181990 -0.3530001 -0.7079925  1.00000000 -0.1025016 -0.8382874
q9   0.49422403 -0.0813887  0.7548119  0.7237786  0.9657497 -0.6287476 -0.10250163  1.0000000  0.1913168
q10 -0.46197421  0.9611281 -0.4087825 -0.1032187  0.3702882  0.5407077 -0.83828741  0.1913168  1.0000000

要显示,您可以使用图片

par(mfrow=c(1, length(levels(A$q1))))
for(c in correlations) image(c)

虽然对于解释,你可能想要一个传奇。为此,请尝试Lapointe的答案中的ggplot2代码

答案 2 :(得分:0)

只需使用cor(data),以及如何将其放入图表中。

data_cor <- cor(data)
library(ggplot2)
library(reshape2)
melted_data <- melt(data_cor)

library(ggplot2)
d <-ggplot(data = melted_data, aes(x=Var1, y=Var2, fill=value))
d +geom_tile()

enter image description here