轻松输入R中的相关矩阵 [英] Easily input a correlation matrix in R
问题描述
我正在运行一个R脚本,该脚本当前正在使用3个相关变量。我想加一个4,想知道是否有一种简单的方法来输入矩阵数据,尤其是对于相关矩阵-一种类似于Matlab的技术,可以在R中输入相关矩阵3x3或4x4,而无需线性输入
I have a R script I'm running now that is currently using 3 correlated variables. I'd like to add a 4th, and am wondering if there's a simple way to input matrix data, particularly for correlation matrices---some Matlab-like technique to enter a correlation matrix, 3x3 or 4x4, in R without the linear to matrix reshape I've been using.
在Matlab中,您可以使用分号作为行尾定界符,因此很容易跟踪互相关的位置
In Matlab, you can use the semicolon as an end-row delimiter, so it's easy to keep track of where the cross correlations are.
在R中,我首先创建
corr <- c(1, 0.1, 0.5,
0.1, 1, 0.9,
0.5, 0.9, 1)
cormat <- matrix(corr, ncol=3)
对
cormat = [1 0.1 0.5;
0.1 1 0.9;
0.5 0.9 1]
感觉比较笨拙,这让我怀疑这是一种更聪明的方法我还没抬头有想法吗?
It just feels clunkier, which makes me suspect there's a smarter way I haven't looked up yet. Thoughts?
推荐答案
这里是另一种方式:
CorrMat <- matrix(scan(),3,3,byrow=TRUE)
1 0.1 0.5
0.1 1 0.9
0.5 0.9 1
跟踪白线很重要。
这篇关于轻松输入R中的相关矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!