Re: [問題] matrix of P-values (correlation test)
# examples of cor.test
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
cor.test(x, y, method = "kendall", alternative = "greater")
# You need to input x and y, in addition, you can edit some parameters.
# For your question, I would write the program like this way
# Suppose your dataset is a data.frame which is named as data.fr
data.fr = data.frame(matrix(rnorm(1000),100)) # assumed data
n = dim(data.fr)[2]
all.comb = t(combn(n,2))
# To produce all combinations of all variables
cor_test_res = lapply(1:dim(all.comb)[1], function(v) {
cor.test(data.fr[,all.comb[v,1]],data.fr[,all.comb[v,2]])})
# Get the results of tests
cor.m = sapply(c(4, 1:3), function(i) {
sapply(1:dim(all.comb)[1], function(v) cor_test_res[[v]][[i]])})
# transfer the result to a matrix format
colnames(cor.m) = c("estimate", "statistic (t)", "df", "p.value")
rownames(cor.m) = sapply(1:dim(all.comb)[1], function(k) {
paste0(names(data.fr)[all.comb[k,1]], "_",
names(data.fr)[all.comb[k,2]])})
# Another
data.fr = data.frame(matrix(rnorm(1000),100)) # assumed data
n = dim(data.fr)[2]
all.comb = t(combn(n,2))
# To produce all combinations of all variables
cor.m = Reduce(rbind, lapply(1:dim(all.comb)[1], function(v) {
Reduce(cbind, cor.test(data.fr[,all.comb[v,1]],
data.fr[,all.comb[v,2]])[c(4,1:3)])}))
colnames(cor.m) = c("estimate", "statistic (t)", "df", "p.value")
rownames(cor.m) = sapply(1:dim(all.comb)[1], function(k) {
paste0(names(data.fr)[all.comb[k,1]], "_",
names(data.fr)[all.comb[k,2]])})
※ 引述《foam (Working Class Hero)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 入門(寫過其他程式,只是對語法不熟悉)
: [問題敘述]:
: 我用 corMat <- cor(df)
: 可以得到一個correlation matrix
: (我的dataframe(df) 每一個column數值間的correlation coefficient)
: 請問如果我想要得到matrix of p-values (correlation test的 p-value)
: 應該怎麼寫呢?
: 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.164.76.109
※ 編輯: celestialgod 來自: 218.164.76.109 (07/30 23:42)
推
07/31 16:45, , 1F
07/31 16:45, 1F
討論串 (同標題文章)
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章