计算并绘制一个变量与多个变量的相关性和置信区间

Calculate and plot correlation & confidence intervals of one variable against multiple variables

我有以下数据(这是一个头,实际数据>100行)由所有序数变量组成。在真实数据中,"Beds" 运行ges 从 1 到 8,所有 "Test" 变量 运行ge 从 1 到 4。我想计算并绘制相关性每个 "Testx" 变量针对 "Beds," 但我不想要一个完整的矩阵,因为我不想将 "Testx" 变量相互关联。

这是数据的 csv ...我将其保存为 "test.csv"

Beds,Test1,Test2,Test3,Test4,Test5,Test6,Test7,Test8
4,4,1,4,4,4,4,3,4
1,3,1,1,1,1,4,2,1
2,4,1,1,2,4,1,1,1
1,4,1,1,4,1,1,1,1
1,2,1,1,1,4,2,2,2
1,4,1,1,1,2,1,1,1
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1,4,1,1,4,3,2,2,1

编辑:

我有一个可以工作但不优雅的 kluge:

test <- read.csv("test.csv")

#Initialize results table as blank dataframe
cTable <- data.frame(matrix(ncol=5, nrow=0))
colnames(cTable) <- c("Test", "Cor", "cL", "cH", "p")

#Begin correlation calculation for Test 1
df <- cor.test(test$Beds, test$Test1)

#Pull the 95% confidence interval and break it into upper and lower limits
interv <- as.character(df$conf.int)
cL <- as.numeric(strsplit(interv, " ")[[1]]) #lower 95% confidence limit
cH <- as.numeric(strsplit(interv, " ")[[2]]) #upper 95% confidence limit

t <- data.frame(Test="Test1", Cor=df$estimate, cL=cL, cH=cH, p=df$p.value)
rownames(t)<-NULL

cTable <- rbind(cTable, t)

rm(df,t) #Repeat code doesn't work unless temporary dataframes are cleared out

#Repeat for Test5
df <- cor.test(test$Beds, test$Test5)

interv <- as.character(df$conf.int)
cL <- as.numeric(strsplit(interv, " ")[[1]])
cH <- as.numeric(strsplit(interv, " ")[[2]])

t <- data.frame(Test="Test5", Cor=df$estimate, cL=cL, cH=cH, p=df$p.value)
rownames(t)<-NULL

cTable <- rbind(cTable, t)

rm(df,t)

虽然这可能不是做我想做的最好的方法,但我现在必须对 Test2 到 Test8 重复它。但它有效。我 运行 它用于 Test1 和 Test5,因为碰巧 Test2 的置信区间未定义。这在实时数据中不是问题。这是输出:

   Test       Cor          cL        cH          p
1 Test1 0.3947710 -0.31253956 0.8204642 0.25890218
2 Test5 0.5921565 -0.05974491 0.8899691 0.07128552

每个 Testx 的最终输出应该有一行。

另一个所需的输出是一个图,其中每个 Textx 作为 X 轴上的序数,Y 轴上的相关系数,显示系数加上置信区间。结果证明这部分很简单:

ggplot(cTable, aes(x=cTable$Test, y=cTable$Cor))+
  geom_point(size=4)+
  geom_errorbar(aes(ymax=cTable$cH, ymin=cTable$cL))

产生:

所以总而言之,我有我需要的东西,但到达那里并不好。在我看来,应该有一种方法可以用一些命令替换上面的重复代码,该命令采用一列 "Beds" 并将其依次与所有其他列相关联,从而产生与此处相同的输出。

我明白了。答案在 corr.test,"psych" 包的一部分。它只需要将输出子集化为 select 只有我想要的相关性并丢弃其余的。

library("ggplot2")
library("data.table") #used for the %like% string operator
library ("psych")  #used for the corr.test function

test <- read.csv("test.csv")

cTab <- print(corr.test(test, use = "pairwise", method = "pearson", adjust = "none"), short=FALSE)
cTab <- cTab[rownames(cTab) %like% "Beds",] #Subsets the variable whose correlations I actually want
cTab$names <- rownames(cTab) #so I can use the rownames as the X variable

ggplot(cTab, aes(x=cTab$names, y=cTab$raw.r))+
  geom_point(size=4)+
  geom_errorbar(aes(ymax=cTab$raw.upper, ymin=cTab$raw.lower), width=0.3, size=0.75)+
  labs(x="Test", y="Correlation Coefficient")+
  theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust=0.5, face = "italic"),
        axis.text.x = element_text(angle = 90, hjust = 1.0))

这是输出的样子(记住 Test2 有错误的数据,我没有费心去修复它)