# KPCgraph: Kernel partial correlation with geometric graphs In KPC: Kernel Partial Correlation Coefficient

## Description

Calculate the kernel partial correlation (KPC) coefficient with directed K-nearest neighbor (K-NN) graph or minimum spanning tree (MST).

## Usage

 ```1 2 3 4 5 6 7 8``` ```KPCgraph( Y, X, Z, k = kernlab::rbfdot(1/(2 * stats::median(stats::dist(Y))^2)), Knn = 1, trans_inv = FALSE ) ```

## Arguments

 `Y` a matrix (n by dy) `X` a matrix (n by dx) or `NULL` if X is empty `Z` a matrix (n by dz) `k` a function k(y, y') of class `kernel`. It can be the kernel implemented in `kernlab` e.g. Gaussian kernel: `rbfdot(sigma = 1)`, linear kernel: `vanilladot()`. `Knn` number of nearest neighbor to use; or "MST" `trans_inv` TRUE or FALSE. Is k(y, y) free of y?

## Details

The kernel partial correlation squared (KPC) measures the conditional dependence between Y and Z given X, based on an i.i.d. sample of (Y, Z, X). It converges to the population quantity (depending on the kernel) which is between 0 and 1. A small value indicates low conditional dependence between Y and Z given X, and a large value indicates stronger conditional dependence. If `X == NULL`, it returns the `KMAc(Y,Z,k,Knn)`, which measures the unconditional dependence between Y and Z. Euclidean distance is used for computing the K-NN graph and the MST.

## Value

The algorithm returns a real number which is the estimated KPC.

`KPCRKHS`, `KMAc`, `Klin`
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26``` ```library(kernlab) n = 2000 x = rnorm(n) z = rnorm(n) y = x + z + rnorm(n,1,1) KPCgraph(y,x,z,vanilladot(),Knn=1,trans_inv=FALSE) n = 1000 x = runif(n) z = runif(n) y = (x + z) %% 1 KPCgraph(y,x,z,rbfdot(5),Knn="MST",trans_inv=TRUE) discrete_ker = function(y1,y2) { if (y1 == y2) return(1) return(0) } class(discrete_ker) <- "kernel" set.seed(1) n = 2000 x = rnorm(n) z = rnorm(n) y = rep(0,n) for (i in 1:n) y[i] = sample(c(1,0),1,prob = c(exp(-z[i]^2/2),1-exp(-z[i]^2/2))) KPCgraph(y,x,z,discrete_ker,1) ##0.330413 ```