R 中的相关循环

Loop for Correlation in R

我试图找到一种方法在 r 中做一个嵌套的 for 循环,以获得每一个可能的相关组合:

cor(y, column1* column2), cor(y, column1* column3), cor(y, column1* column4)
等等

这是我目前尝试过的方法:

for(i in 1:length(dataframe))
{
for(j in 1:length(dataframe))
{
joint_correlation(i,j)=cor(y ~ dataframe(i) * dataframe(j));
}
}

我的数据框有 115 列,如小样本所示:

FG_pct FGA FT FT_pct FTA GP GS GmSc  MP    ORB

0.625   8   0  0.00   0  1  0   6.6  28.4   2   
0.500   4   0  0.00   1  2  0   2.1  17.5   0   
0.000   1   0  0.00   0  3  0   1.2  6.6    1   
0.500   6   0  0.00   0  4  0   3.6  13.7   1   
0.500   2   0  0.00   0  5  0   0.9  7.4    1   

我想为 column1 和 column2 切换出的每个可能组合找到 cor(MP, column1* column2) 的相关性。这样,我就不必分别完成每一个。如果可能,我想将每个相关组合 cor(MP, column1* column2)cor(MP, column1* column3)cor(MP, column2* column4) 等的输出保存在单独的列中。

这是我想要的示例: cor(MP, FG_pct*FT_pct)

编辑:Jean-Claude Arbaut 给出了更好的答案,正如对此答案的评论。使用 cor(df).

这是我拙劣的回答:使用库 corrgram(它主要是一个可视化工具)我们可以轻松地获得数据集中所有相关性的组合。示例:

library(corrgram)

#Example data

df <- data.frame(x = rnorm(50, 5, 5),
               y = rnorm(50, 2, 5))

df$z <- df$x / df$y
df$abc <- df$x * df$y * df$z

#panel arguments are necessary if you want to visualize correlations
corr <- corrgram(df,
         order = F, 
         lower.panel = panel.cor,
         upper.panel = panel.pts,
         text.panel = panel.txt,
         diag.panel = panel.minmax,
         main = "Correlation")

#call corr gives
corr

             x          y         z        abc
x   1.00000000 0.07064179 0.1402051 0.89166002
y   0.07064179 1.00000000 0.2495239 0.08024278
z   0.14020508 0.24952388 1.0000000 0.14649093
abc 0.89166002 0.08024278 0.1464909 1.00000000

绝对有一种更好的方法可以使用函数而不使用包来完成此操作,但现在还很早,如果您迫切希望获得结果,这可能会很好。

p.s 使用 corrgram() 函数而不分配它会给你一个很好的相关性可视化。

假设您想要将每一列的相关性乘以其余两列的组合。

我们可以使用 combn(names(dat), 2) 找到相应组合的名称,我们将其放入 lapply.

combs <- do.call(cbind.data.frame,
                 lapply("MP", rbind, combn(names(dat)[names(dat) != "MP"], 2)))
combs
#        1      2   3
# 1     MP     MP  MP
# 2 FG_pct FG_pct FGA
# 3    FGA     FT  FT

在另一个 lapply 中,我们对名称组合的数据进行子集化,并使用公式 cor(x1 ~ x2 * x3) 计算 cor。同时,我们将名称 pasted 作为公式存储在 attribute 中,以便稍后记住我们在每次迭代中计算的内容。

res.l <- lapply(combs, function(x) {
  `attr<-`(cor(dat[,x[1]], dat[,x[2]]*dat[,x[3]]),
           "what", {
             paste0(x[1], ", ", paste(x[2], "*", x[3]))})
})

最后我们根据属性unlistsetNames

res <- setNames(unlist(res.l), sapply(res.l, attr, "what"))
res

结果

# MP, FG_pct * FGA  MP, FG_pct * FT     MP, FGA * FT 
#        0.2121374        0.2829003        0.4737892 

检查:

(注意,你可以直接把名称,例如MP, FG_pct * FGA 放到cor函数中。)

with(dat, cor(MP, FG_pct * FGA))
# [1] 0.2121374
with(dat, cor(MP, FG_pct * FT))
# [1] 0.2829003
with(dat, cor(MP, FGA * FT))
# [1] 0.4737892

要排序,请使用例如sort(res)rev(sort(res)).

玩具数据:

set.seed(42)
dat <- as.data.frame(`colnames<-`(MASS::mvrnorm(n=1e4, 
                          mu=c(0.425, 4.2, 0.2, 3), 
                          Sigma=matrix(c(1, .3, .7, 0,
                                         .3, 1, .5, 0,
                                         .7, .5, 1, 0,
                                         0, 0, 0, 1), nrow=4), 
                          empirical=T), c("FG_pct", "MP", "FGA", "FT")))