有没有办法在“R”(属性之间具有 p 值的矩阵)中找到卡方 p 值矩阵?
例如,考虑iris数据集。我正在寻找一个矩阵如下:
| | Sepal length | Sepal width | Petal length | Petal width | Species |
|----------------|--------------|-------------|--------------|-------------|---------|
| Sepal length | | | | | |
| Sepal width | | | | | |
| Petal length | | | | | |
| Petal width | | | | | |
| Species | | | | | |
Run Code Online (Sandbox Code Playgroud)
矩阵的元素将是iris数据集(i,j) 变量的卡方值。
我有22个变量,我想获取相关分数,而不是作为相关矩阵,而是在数据帧中按对数获取...
我的意思是...不是这样
v1 v2 v3 v4
v1 1 x x x
v2 x 1 x x
v3 x x 1 x
v4 x x x 1
Run Code Online (Sandbox Code Playgroud)
但是像这样:
var1 var2 cor
v1 v2 x
v1 v3 x
v1 v4 x
v2 v3 x
v2 v4 x
v3 v4 x
Run Code Online (Sandbox Code Playgroud)
我是R的新手,我已经进行了很多研究,最终我得到了一个代码,该代码真诚地根本没有效率...我的代码创建了一个庞大的数据框,其中包含22个变量的所有可能组合(是4194304 combinatios ... 很多!!!))然后代码只为前211行分配相关性,这是只有2个变量的组合...然后我排除了我不感兴趣的所有内容恩...我得到了我所需要的。但是我敢肯定,这是一种非常愚蠢的方式,我想学习更好的方式...有什么提示吗?
我的代码:
#Getting the variable names from the data frame
av_variables<-variable.names(data.1)
#Creating a huge data frame for all possible combinations
corr_combinations <- as.data.frame(matrix(1,0,length(av_variables)))
for (i in 1:length(av_variables)){
corr_combinations.i …Run Code Online (Sandbox Code Playgroud)