【问题标题】:How to type in a large correlation matrix into R如何在 R 中输入一个大的相关矩阵
【发布时间】:2017-10-04 22:56:34
【问题描述】:

我有一个来自论文的下三角相关矩阵,并想在 R 中输入它以进行主成分分析。这是相关矩阵。

我试过命令

M <- diag(15) 
M[lower.tri(M, diag = T)] <-c(1.00, .77, 1.00,.53,.50,1.00,
   ...)

但是,R 按列读取数据。我尝试使用矩阵(c(),byrows = TRUE)。这没用。感谢有关要求 R 按行读取数据的建议)。

【问题讨论】:

  • Eric90,如果您在问题中添加一个小数据示例而不是屏幕截图会更好 - 最好是允许用户将您的示例复制并粘贴到本地 R 会话中。谢谢
  • 您好,由于相关表来自论文,我没有原始数据。这是一个按行显示数据的小例子。 1.00 .77 1.00 .53 .50 1.00 .54 .44 .74 1.00 .54 .48 .91 .82 1.00
  • @Eric90,你为什么不想按列构建矩阵?我认为这是最简单的方法,因为matrix() 需要所需的行数(nrow)和列数(ncol),但不假定“空”单元格。

标签: r correlation


【解决方案1】:

以正确顺序填充矩阵的一种方法是使用矩阵转置。

创建一些数据:下三角相关矩阵输出

m <- cor(mtcars[1:4])
m[upper.tri(m)] <- NA
write.table(m, temp<-tempfile(), row.names = FALSE, col.names=FALSE, na="")

使用标准工具阅读

# Change `temp` to the path to your matrix file
inmat <- as.matrix(read.table(temp, fill=TRUE))
# Fill in upper triangle
inmatt <- t(inmat)
inmatt[lower.tri(inmatt, diag=FALSE)] <- inmat[lower.tri(inmat, diag=FALSE)]

检查

all.equal(cor(mtcars[1:4]), inmatt, check.attributes=FALSE)


好的,因为您正在从图像中读取,为了节省手动输入,您可以从图像中读取文本。

library(tesseract)
# Read image
engine <- tesseract(options = list(tessedit_char_whitelist = ".-0123456789"))
text <- ocr("https://i.stack.imgur.com/OHf3z.jpg", engine)
cat(text) # looks okay
cat(text, file=temp<-tempfile())

# Now try to fill matrix
v = as.numeric(unlist(strsplit(readLines(temp), " ")))

inmat <- diag(15) # just hard-code matrix dimension
inmat[upper.tri(inmat, diag=TRUE)] <- v
inmatt <- t(inmat)
inmatt[upper.tri(inmatt, diag=TRUE)] <- v

生产

inmatt
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6] [,7] [,8]  [,9] [,10] [,11] [,12] [,13] [,14] [,15]
 [1,]  1.00  0.77  0.53  0.54  0.54  0.30 0.16 0.36 -0.11 -0.10 -0.02  0.14  0.09  0.21  0.16
 [2,]  0.77  1.00  0.50  0.44  0.48  0.28 0.20 0.34 -0.05 -0.09 -0.07 -0.02 -0.01  0.18  0.21
 [3,]  0.53  0.50  1.00  0.74  0.91  0.72 0.28 0.79  0.08 -0.03  0.22  0.04  0.06  0.15  0.22
 [4,]  0.54  0.44  0.74  1.00  0.82  0.63 0.19 0.56  0.02 -0.07  0.23  0.05  0.10  0.15  0.22
 [5,]  0.54  0.48  0.91  0.82  1.00  0.75 0.26 0.70  0.05 -0.08  0.21  0.07  0.09  0.14  0.22
 [6,]  0.30  0.28  0.72  0.63  0.75  1.00 0.31 0.63  0.20  0.02  0.27 -0.03 -0.03 -0.01  0.23
 [7,]  0.16  0.20  0.28  0.19  0.26  0.31 1.00 0.38  0.03  0.15  0.29  0.23  0.24  0.16  0.29
 [8,]  0.36  0.34  0.79  0.56  0.70  0.63 0.38 1.00  0.14  0.05  0.37  0.11  0.11  0.13  0.18
 [9,] -0.11 -0.05  0.08  0.02  0.05  0.20 0.03 0.14  1.00  0.50  0.44 -0.04 -0.10  0.13  0.03
[10,] -0.10 -0.09 -0.03 -0.07 -0.08  0.02 0.15 0.05  0.50  1.00  0.62  0.37  0.08  0.11 -0.07
[11,] -0.02 -0.07  0.22  0.23  0.21  0.27 0.29 0.37  0.44  0.62  1.00  0.31  0.21  0.28  0.09
[12,]  0.14 -0.02  0.04  0.05  0.07 -0.03 0.23 0.11 -0.04  0.37  0.31  1.00  0.73  0.12  0.10
[13,]  0.09 -0.01  0.06  0.10  0.09 -0.03 0.24 0.11 -0.10  0.08  0.21  0.73  1.00  0.31  0.32
[14,]  0.21  0.18  0.15  0.15  0.14 -0.01 0.16 0.13  0.13  0.11  0.28  0.12  0.31  1.00  0.41
[15,]  0.16  0.21  0.22  0.22  0.22  0.23 0.29 0.18  0.03 -0.07  0.09  0.10  0.32  0.41  1.00

另一个选项是将图像或图像的 url 传递给http://www.free-ocr.com/,它将尝试解析。它将创建图像的文本文档,然后您可以阅读。

【讨论】:

  • 您好,感谢您的建议。如果我只能手动输入相关表(相关表来自不提供原始数据集的论文,我的任务是复制相关表中的结果)。你有什么建议?
  • 非常感谢详细有效的解决方案。
【解决方案2】:

你可以试试这个:

## For this example, consider the version of your matrix with dim = 6*6
M <- diag(6)

# Lower triangular matrix, without values of the diagonal
M[lower.tri(M, diag = FALSE)] <- c(.77, .53, .54, .54, .30, #1st column
                                   .50, .44, .48, .28,      #2nd column
                                   .74, .91, .72,           #3rd column
                                   .82, 63,                 #4th column
                                   .75)                     #5th column

M[upper.tri(M, diag = FALSE)]  <- t(M)[upper.tri(M, diag = FALSE)] 
M # whole matrix
M[upper.tri(M, diag = FALSE)] <- ""
as.data.frame(M) 

【讨论】:

  • 您好,感谢您的建议。您的解决方案似乎会放弃一些输入。有没有办法在创建下三角矩阵的同时保留所有输入?
  • 嗨,现在,我猜对了。请考虑我的答案的新编辑。
  • 感谢您的建议。它也可以。用户 20650 建议的速度更快,因为我不需要在大矩阵中输入每个数字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2019-12-07
  • 1970-01-01
  • 2012-05-27
  • 2023-02-21
  • 2019-02-24
相关资源
最近更新 更多