【问题标题】:Extracting specific pairwise correlations from correlation matrix and add appropiate number of asterisks for corresponding significance level从相关矩阵中提取特定的成对相关,并为相应的显着性水平添加适当数量的星号
【发布时间】:2018-08-12 11:27:10
【问题描述】:

我正在创建一个(apa)表,其中包含来自更大矩阵的特定相关性,如果相关性很重要,我还想添加一个星号。我使用 Hmisc 包来创建所有可能的相关性和相应的 p 值。然后我使用 MOTE 包对相关性进行四舍五入并去掉前导零。然后我将 p 值更改为星号。我提取了这些感兴趣的相关性并将它们放入一个新矩阵中。假设我只想创建一个新的相关矩阵(3乘4) 'am'、'gear' 和 'carb' 定义 3 行,'mpg'、'cyl'、'disp' 和 'hp' 定义 4 列。

library(Hmisc) # to get correlations and p-values

cormat = rcorr(as.matrix(mtcars))
cormat$r # to see the correlations
cormat$P # to see the p-values


cor.table = matrix(NA, nrow = 3, ncol = 4) # create empty matrix

library(MOTE) # to round and get rid of leading 0's

cor.table[1:3,1:4] = c(apa(cormat$r[9:11, c(1:4)],2,F)) # fill with correlations and get rid of leading zero's

pm = ifelse(cormat$P <= .001, "***", 
        ifelse(cormat$P <= .01, "**", 
           ifelse(cormat$P <= .05, "*", " "))) # create the appr. number of asterisks per cell

在这之后,我陷入了困境。现在我想为每个单元格添加适当数量的星号(在相关值的后面),如果可能的话,让所有东西都很好地垂直对齐。小数点的点在彼此垂直上方,也许这是我需要在 rmarkdown 中做的事情,但我还没有那么远)。当然,如果有更简单的 - 或者更优雅的方式 - 完成这一切,我全神贯注。谢谢你。

【问题讨论】:

    标签: r matrix correlation p-value significance


    【解决方案1】:

    找到最重要部分的解决方案,提取特定的成对相关性并为其显着性水平添加适当数量的星号(根据小数点垂直对齐值并使星号显得更小尚无解决方案):

    library(Hmisc)                    # to get correlations and p-values
    
    cormat = rcorr(as.matrix(mtcars))
    cormat$r                          # to see all correlations
    cormat$P                          # to see the p-values
    
    cor.table = matrix(NA, nrow = 3, ncol = 4) # create empty matrix
    
    
    library(MOTE)                     # to round and get rid of leading 0's
    
    pm = ifelse(cormat$P <= .001, "***", 
            ifelse(cormat$P <= .01, "**", 
               ifelse(cormat$P <= .05, "*", " "))) # create the appr. number of asterisks per cell
    
    
    cor.table = matrix(NA, 
                       nrow = 3, 
                       ncol = 4) # create empty matrix
    
    cor.table[1:3,1:4] = paste(
      apa(cormat$r[9:11, 1:4], 2,F),
      pm[9:11, 1:4],
      sep = ""
      )
    
    cor.table  # to see resulting matrix
    
    #      [,1]     [,2]     [,3]      [,4]    
    # [1,] ".60***" "-.52**" "-.59***" "-.24 " 
    # [2,] ".48**"  "-.49**" "-.56***" "-.13 " 
    # [3,] "-.55**" ".53**"  ".39*"    ".75***"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 2022-01-22
      • 2015-11-20
      • 2020-07-23
      相关资源
      最近更新 更多