0
votes

I want to correlate one variable (a) with about 20 others, controlling for one other variable (age) using partial correlation. I have been using p.cor from the ppcor package one pair at a time:

p.cor(a, b, age)

This is very tedious and doesn't account for multiple comparisons. I have found partial.cor from RcmdrMisc, which corrects for multiple comparison but controls for all other variables in the matrix, when I only want to control for age. This question answers how to control for only one variable, but doesn't correct for multiple comparisons.

I'm only a beginner to R but have some basic scripting experience. Any help would be greatly appreciated.

1

1 Answers

1
votes

You try to get your variables into a data.frame or matrix, in this case the variable i want to correlate with age is 'y' and the other columns are the variables i want to correlate with:

set.seed(144)
df = data.frame(y = runif(100),
                age = runif(100,20,60),
                matrix(runif(100*20),ncol=20)
                )
colnames(df)[3:22] = paste0("var",1:20)

Define the variable you to correlate with all others, the control variable and the remaining columns:

DV = "y"
IV = "age"
ctrl = setdiff(colnames(df),c(DV,IV))

result = do.call(rbind,lapply(ctrl,function(i){
data.frame(ctrl =i,pcor.test(df[,DV],df[,IV],df[,i]))
}))

head(result)

  ctrl   estimate   p.value statistic   n gp  Method
1 var1 -0.1273527 0.2090534 -1.264576 100  1 pearson
2 var2 -0.1311525 0.1956741 -1.302957 100  1 pearson
3 var3 -0.1182934 0.2435512 -1.173293 100  1 pearson
4 var4 -0.1220453 0.2288147 -1.211060 100  1 pearson
5 var5 -0.1310073 0.1961739 -1.301489 100  1 pearson
6 var6 -0.1309958 0.1962135 -1.301373 100  1 pearson

To get adjusted p-values, bonferroni, you do:

result$padj = p.adjust(result$p.value,method="bonferroni")