I want to perform partial correlation analysis among multiple columns controlling by multiple covariates, and then extract r and p-value. My real data have some missing values.
I found that this answer (Pairwise partial correlation of a matrix, controlling by one variable) might be useful, so I adjusted this method into my code. Because I have missing values, so I cannot use ppcor::pcor.test(), which is described as 'Missing values are not allowed', to achieve my goal.
Here I use the built-in dataset mtcars to display the problem I met.
# load "ggm" packages to perform partial correlation analysis
library(ggm)
# subset mtcars dataset and make some datapoints as missing values
mydata <- cbind(mtcars[1:8])
mydata[4:10,3] <- rep(NA,7)
mydata[1:5,4] <- NA
# perform partial correlation analysis among the first 6 columns with the last two columns as covariates
sapply(1:(ncol(mydata)-2), function(x) sapply(1:(ncol(mydata)-2), function(y) {
if (x == y) 1
else ggm::pcor(c(mydata[,x], mydata[,y], mydata[,7], mydata[,8]),var(mydata))
}))
# error:
Error in S[u, u] : subscript out of bounds
I got error at this step and could neither perform partial correlation nor extract r and p-values.
Many thanks to you to help me!
Ella