Re: [問題] matrix of P-values (correlation test)

看板R_Language作者 (讓你喜歡這世界~)時間11年前 (2013/07/31 00:17), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串4/4 (看更多)
直接引用上一文章作者的例子 data.fr = data.frame(matrix(rnorm(1000),100)) #假資料 100 row 10 column data.fr.cor <- cor(data.fr) data.fr.cor.tval <- data.fr.cor*sqrt((100-2)/(1-(data.fr.cor*data.fr.cor))) data.fr.cor.pval <- 2*pt(abs(data.fr.cor.tval), 98, lower.tail=F) ### 參考資料 (wiki) Pearson product-moment correlation coefficient ※ 引述《celestialgod (攸藍)》之銘言: : # 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: 220.136.18.41

07/31 00:31, , 1F
我太依賴內建的函數了qq
07/31 00:31, 1F

08/03 00:57, , 2F
謝謝
08/03 00:57, 2F
文章代碼(AID): #1Hz-S2zm (R_Language)
文章代碼(AID): #1Hz-S2zm (R_Language)