0

我正在创建一个(apa)表,其中包含来自更大矩阵的特定相关性,如果相关性很重要,我还想添加一个星号。我使用 Hmisc 包来创建所有可能的相关性和相应的 p 值。然后我使用 MOTE 包对相关性进行四舍五入并去掉前导零。然后我将 p 值更改为星号。我提取了这些感兴趣的相关性并将它们放入一个新矩阵中。假设我只想创建一个新的相关矩阵(3 x 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 中做的事情,但我还没有那么远)。当然,如果有更简单——或更优雅的方式——来完成这一切,我会全力以赴。谢谢你。

4

1 回答 1

0

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

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***"
于 2018-03-05T07:49:42.783 回答