'Finding partial correlation with missing value

I want to find the partial correlation between two variables holding the third variable fix using pearson method.

This is the error I had:

Error in if (det(cvx) < .Machine$double.eps) :missing value where TRUE/FALSE needed

In fact, I have a missing value in the data and (na.rm=T) didn't work with me.

r


Solution 1:[1]

Here's a reproducible example for the OP's question:

library("ppcor")

varX <- seq(from=1, to=10, length=10)
varY <- seq(from=20, to=50, length=10)
varZ <- rnorm(10)

varZ[c(1,5,7)] <- NA

pcor.test(x=varX, y=varY, z=varZ)

Error in if (det(cvx) < .Machine$double.eps) { : missing value where TRUE/FALSE needed

This is addressed in the documentation for the ppcor package (https://www.rdocumentation.org/packages/ppcor/versions/1.1/topics/pcor.test) in the Notes Section: Note: Missing values are not allowed

You might look at the partial.r() function of the psych package instead.

library("psych")
mydata <- cbind(varX, varY, varZ)
partial.r(data=mydata, x=c("varX","varY"), y="varZ")

Solution 2:[2]

The library WGCNA can handle missing values.

library(WGCNA)
varX <- seq(from=1, to=10, length=10)
varY <- seq(from=20, to=50, length=10)
varZ <- rnorm(10)

varZ[c(1,5,7)] <- NA

corAndPvalue(cbind(varX, varY, varZ), method='spearman')

correlations with some statistics.

$cor
      varX  varY  varZ
varX  1.00  1.00 -0.25
varY  1.00  1.00 -0.25
varZ -0.25 -0.25  1.00

$p
             varX         varY         varZ
varX 1.063504e-62 1.063504e-62 5.887244e-01
varY 1.063504e-62 1.063504e-62 5.887244e-01
varZ 5.887244e-01 5.887244e-01 1.411089e-39

$Z
           varX       varY       varZ
varX 51.9536816 51.9536816 -0.5711204
varY 51.9536816 51.9536816 -0.5711204
varZ -0.5711204 -0.5711204 41.0729917

$t
             varX         varY         varZ
varX 1.342177e+08 1.342177e+08 5.773503e-01
varY 1.342177e+08 1.342177e+08 5.773503e-01
varZ 5.773503e-01 5.773503e-01 1.061084e+08

$nObs
     varX varY varZ
varX   10   10    7
varY   10   10    7
varZ    7    7    7

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mehrdadorm