简体   繁体   English

如何将 Corrplot 与手动创建的相关矩阵一起使用(列表类型)

[英]How to use Corrplot with correlation matrix created by hand (of type list)

I used a for loop to create a correlation matrix, because I needed to use polychor to generate polychoric correaltions and I was only able to get polychor to correlate two variables at a time.我使用了一个 for 循环来创建一个相关矩阵,因为我需要使用 polychor 来生成 polychoric correaltions,而且我只能让 polychor 一次关联两个变量。 Anyway, I created my own correlation table with the following code:无论如何,我使用以下代码创建了自己的关联表:


    for(i in 1:ncol(gd2)) {
        for (j in 1:ncol(gd2)) {
            corVal 

The table looks like this:


     head(dtnew)
       Better Afraid Alive Bored Drop Empty Energy Happy Help Home Hope Memory Satis Spirit Worth  TOT
    1:   1.00   0.32  0.29  0.39 0.36  0.46   0.25  0.43 0.39 0.13 0.46   0.39  0.50   0.45  0.48 0.67
    2:   0.32   1.00  0.25  0.20 0.24  0.30   0.23  0.30 0.43 0.15 0.44   0.28  0.31   0.29  0.34 0.62
    3:   0.29   0.25  1.00  0.26 0.28  0.46   0.38  0.60 0.35 0.19 0.41   0.10  0.49   0.53  0.43 0.65
    4:   0.39   0.20  0.26  1.00 0.36  0.56   0.31  0.36 0.39 0.16 0.32   0.23  0.39   0.35  0.44 0.67
    5:   0.36   0.24  0.28  0.36 1.00  0.44   0.41  0.37 0.43 0.31 0.35   0.22  0.42   0.37  0.40 0.72
    6:   0.46   0.30  0.46  0.56 0.44  1.00   0.32  0.55 0.51 0.18 0.45   0.17  0.62   0.52  0.64 0.75
    > 

But longer.

Here is the dput()

structure(list(Better = c(1, 0.32, 0.29, 0.39, 0.36, 0.46, 0.25, 
0.43, 0.39, 0.13, 0.46, 0.39, 0.5, 0.45, 0.48, 0.67), Afraid = c(0.32, 
1, 0.25, 0.2, 0.24, 0.3, 0.23, 0.3, 0.43, 0.15, 0.44, 0.28, 0.31, 
0.29, 0.34, 0.62), Alive = c(0.29, 0.25, 1, 0.26, 0.28, 0.46, 
0.38, 0.6, 0.35, 0.19, 0.41, 0.1, 0.49, 0.53, 0.43, 0.65), Bored = c(0.39, 
0.2, 0.26, 1, 0.36, 0.56, 0.31, 0.36, 0.39, 0.16, 0.32, 0.23, 
0.39, 0.35, 0.44, 0.67), Drop = c(0.36, 0.24, 0.28, 0.36, 1, 
0.44, 0.41, 0.37, 0.43, 0.31, 0.35, 0.22, 0.42, 0.37, 0.4, 0.72
), Empty = c(0.46, 0.3, 0.46, 0.56, 0.44, 1, 0.32, 0.55, 0.51, 
0.18, 0.45, 0.17, 0.62, 0.52, 0.64, 0.75), Energy = c(0.25, 0.23, 
0.38, 0.31, 0.41, 0.32, 1, 0.48, 0.37, 0.36, 0.31, 0.14, 0.4, 
0.43, 0.38, 0.74), Happy = c(0.43, 0.3, 0.6, 0.36, 0.37, 0.55, 
0.48, 1, 0.45, 0.21, 0.49, 0.22, 0.69, 0.84, 0.49, 0.8), Help = c(0.39, 
0.43, 0.35, 0.39, 0.43, 0.51, 0.37, 0.45, 1, 0.2, 0.51, 0.32, 
0.5, 0.44, 0.6, 0.73), Home = c(0.13, 0.15, 0.19, 0.16, 0.31, 
0.18, 0.36, 0.21, 0.2, 1, 0.23, 0.13, 0.13, 0.15, 0.26, 0.63), 
    Hope = c(0.46, 0.44, 0.41, 0.32, 0.35, 0.45, 0.31, 0.49, 
    0.51, 0.23, 1, 0.38, 0.48, 0.47, 0.59, 0.73), Memory = c(0.39, 
    0.28, 0.1, 0.23, 0.22, 0.17, 0.14, 0.22, 0.32, 0.13, 0.38, 
    1, 0.25, 0.24, 0.31, 0.66), Satis = c(0.5, 0.31, 0.49, 0.39, 
    0.42, 0.62, 0.4, 0.69, 0.5, 0.13, 0.48, 0.25, 1, 0.66, 0.6, 
    0.78), Spirit = c(0.45, 0.29, 0.53, 0.35, 0.37, 0.52, 0.43, 
    0.84, 0.44, 0.15, 0.47, 0.24, 0.66, 1, 0.51, 0.77), Worth = c(0.48, 
    0.34, 0.43, 0.44, 0.4, 0.64, 0.38, 0.49, 0.6, 0.26, 0.59, 
    0.31, 0.6, 0.51, 1, 0.77), TOT = c(0.67, 0.62, 0.65, 0.67, 
    0.72, 0.75, 0.74, 0.8, 0.73, 0.63, 0.73, 0.66, 0.78, 0.77, 
    0.77, 0.89)), row.names = c(NA, -16L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x000001d7adc21ef0>)

</pre/> </pre/>

I would like to generate a visual using corrplot.我想使用 corrplot 生成视觉效果。 However, when I try, I get an error: Error in is.finite(tmp): default method not implemented for type 'list'但是,当我尝试时,出现错误:is.finite(tmp) 中的错误:未为类型“列表”实现默认方法

My data is indeed of type list.我的数据确实是列表类型。 I have tried usuing 'unlist'.我试过使用'unlist'。 Not sure what else to try.不知道还有什么可以尝试的。

There is a problem with your dput() output, possibly because you have a data.table.你的dput() output 有问题,可能是因为你有一个 data.table。 I can read it by deleting ", .internal.selfref = <pointer: 0x000001d7adc21ef0>" from the last line so that it ends class = c("data.table", "data.frame")) .我可以通过从最后一行删除 ", .internal.selfref = <pointer: 0x000001d7adc21ef0>" 来读取它,以便它结束class = c("data.table", "data.frame")) Printing that out shows a problem with the last line/column ( Tot ).打印出来显示最后一行/列( Tot )有问题。 The bottom row in that column should be 1.00, but it is 0.89.该列的底行应该是 1.00,但它是 0.89。 We can trim that and use as.matrix (my mistake in the earlier comment) to convert the data frame:我们可以修剪它并使用as.matrix (我在前面的评论中的错误)来转换数据框:

gd3 <- gd2[-16, -16]
corrplot(as.matrix(gd3))

在此处输入图像描述

library(corrplot)
M <- cor(df)
head(round(M,2))
corrplot(M, method="number")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM