Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

r - How to iterate through parameters to analyse

Is there a better way to iterate through a set of parameters of a given dataset? Obviously, I try to get a table of correlation coefficients: columns are "CI, CVP, mean PAP, mean SAP", rows are "ALAT, ASAT, GGT, Bili, LDH, FBG". For each combination I′d like to get the correlation coefficient and the significance level (p=...). Below You see "the hard way". But is there a more elegant way, possibly with a printable table?

attach(Liver)
cor.test(CI, ALAT, method = "spearman")
cor.test(CI, ASAT, method = "spearman")
cor.test(CI, GGT, method = "spearman")
cor.test(CI, Bili, method = "spearman")
cor.test(CI, LDH, method = "spearman")
cor.test(CI, FBG, method = "spearman")

cor.test(CVP, ALAT, method = "spearman")
cor.test(CVP, ASAT, method = "spearman")
cor.test(CVP, GGT, method = "spearman")
cor.test(CVP, Bili, method = "spearman")
cor.test(CVP, LDH, method = "spearman")
cor.test(CVP, FBG, method = "spearman")

cor.test(meanPAP, ALAT, method = "spearman")
cor.test(meanPAP, ASAT, method = "spearman")
cor.test(meanPAP, GGT, method = "spearman")
cor.test(meanPAP, Bili, method = "spearman")
cor.test(meanPAP, LDH, method = "spearman")
cor.test(meanPAP, FBG, method = "spearman")

cor.test(meanSAP, ALAT, method = "spearman")
cor.test(meanSAP, ASAT, method = "spearman")
cor.test(meanSAP, GGT, method = "spearman")
cor.test(meanSAP, Bili, method = "spearman")
cor.test(meanSAP, LDH, method = "spearman")
cor.test(meanSAP, FBG, method = "spearman")

detach("Liver")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The trick is to get all possible combinations. Here, I create a data.frame with 10 columns and get all combinations using the combn function. Then its pretty straightforward to obtain the corrleation- and p- values.

set.seed(12)
x <- as.data.frame(matrix(rnorm(100), nrow=10))
combinations <- combn(ncol(x), 2)

out <- apply(combinations, 2, function(idx) {
    t <- cor.test(x[, idx[1]], x[, idx[2]], method = "spearman")
    c(names(x)[idx[1]], names(x)[idx[2]], t$estimate, t$p.value)
})
# more formatting if necessary
out <- as.data.frame(t(out))
names(out) <- c("col.idx1", "col.idx2", "cor", "pval")

Edit: An even more compact code by utilizing FUN argument within combn (as per Greg's suggestion)

set.seed(12)
x <- as.data.frame(matrix(rnorm(100), nrow=10))
out <- as.data.frame(t(combn(ncol(x), 2, function(idx) {
    t <- cor.test(x[, idx[1]], x[, idx[2]], method = "spearman")
    c(names(x)[idx[1]], names(x)[idx[2]], t$estimate, t$p.value)
})))
names(out) <- c("col.idx1", "col.idx2", "cor", "pval")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...