两组变量之间的相关性

时间:2019-05-14 13:35:00

标签: r correlation

我想在两组变量之间建立一个相关矩阵。在第一组中,我有9个变量,在第二组中,我有24个变量。第二组变量包括第一组的9个变量。

例如

这是我的两套。

dep <- c("anxiety", "avoidance", "NEURO", "EXTRA","AGREE", "OPEN", "CONSC", "socialt", "solitaryt")

indep <-c("anxiety", "EXTRA", "NEURO", "CONSC", "OPEN", 
      "avoidance", "AGREE", "RMTQ", "RSE", "selfcontrol", "socialt",  
      "solitaryt", "riska", "stress", "socialdiversity", "networksize", "socialdiversity",
      "socialembedded", "rwa", "lonely", "SDO", "DASSD", "DASSA", "WEMWBS")

1 个答案:

答案 0 :(得分:0)

如果有人遇到相同的问题,我找到了解决方法。

我为每个变量集创建了两个数据框:

df1 <- dat[, names(dat)%in% c("EXTRA", "NEURO", "CONSC", "OPEN", "AGREE", "anxiety", 
                          "avoidance", "socialt","solitaryt", "RMTQ", "RSE", "selfcontrol","riska", "stress", "socialdiversity", "networksize", "socialdiversity"
      ,"socialembedded", "rwa", "lonely", "SDO", "DASSD", "DASSA", "WEMWBS")]

df2 <- dat[,names(dat)%in% c("EXTRA", "NEURO", "CONSC", "OPEN", "AGREE", "anxiety", 
                         "avoidance", "socialt", "solitaryt")]

然后我将这些df转换为矩阵

A <- as.matrix(df1)
B <- as.matrix(df2)

最后,我们可以轻松地关联这些数据帧

Matrice <- cor(A, B, use = "pairwise.complete.obs")