检查点是否在椭球内(3D或更高维)

时间:2015-01-12 19:56:42

标签: r pca ellipse

以下样本数据仅代表二维问题(来自以下论文:Tsong等人J. Biopharm.Stat。(1997)7(3):423-439)。

然而,可能会有更多的变量,即我正在寻找一种不仅适用于椭圆的解决方案,也适用于更高维的椭圆体。一种可能性是通过Cholesky分解来进行这种评估。 Stephen Pope的报告展示了它应该如何运作(参见https://tcg.mae.cornell.edu/pubs/Pope_FDA_08.pdf,第12章第6章)。

这是一些用于说明的R代码。不幸的是,我的解决方案对于椭圆边界上​​的点没有产生1。但目前我不知道为什么。

d.wei <- data.frame(sample=as.factor(rep(1:12, times=4)),
                    batch=as.factor(rep(1:4, each=12)),
                    type=as.factor(c(rep("ref", times=3*12), rep("test", times=12))),
                    alpha=c(0.58327, 0.49561, 0.57064, 0.57884, 0.59302, 0.60576, 0.53641, 0.56090, 0.46120, 0.58479, 0.59315, 0.60824, 0.56825, 0.56062, 0.55400, 0.44647, 0.56456, 0.62928, 0.46121, 0.53711, 0.43405, 0.57479, 0.61541, 0.57321, 0.54616, 0.52297, 0.58101, 0.40571, 0.59295, 0.58960, 0.55595, 0.57767, 0.46120, 0.52287, 0.53011, 0.58157, 0.38827, 0.42679, 0.43043, 0.43478, 0.44101, 0.43187, 0.45485, 0.45064, 0.39666, 0.42297, 0.43270, 0.45036),
                    beta=c(0.55476, 0.65515, 0.48877, 0.58846, 0.48475, 0.47271, 0.55110, 0.47149, 0.47234, 0.47851, 0.48657, 0.44891, 0.56451, 0.53604, 0.50349, 0.71136, 0.52227, 0.42176, 0.59261, 0.48729, 0.49848, 0.47205, 0.45159, 0.46977, 0.59912, 0.59704, 0.47223, 0.74920, 0.47028, 0.47993, 0.49523, 0.46373, 0.47234, 0.55290, 0.56269, 0.46102, 0.75826, 0.75668, 0.70732, 0.67568, 0.68117, 0.82644, 0.70290, 0.68447, 0.82681, 0.76226, 0.78700, 0.75428))

param <- c("alpha", "beta")
p <- length(param) # number of variables
n <- length(d.wei[d.wei$type == "ref", "sample"]) # number of samples
S2 <- cov(d.wei[d.wei$type == "ref", param]) # Covariance matrix of the reference batch data
y.bar <- apply(X=d.wei[d.wei$type == "ref", param], MARGIN=2, FUN=mean) # Averarage of each parameter of the reference batch data

# The following equation holds for a one-sample situation (Hotelling's T2 statistic) 
# [n / (n + 1)] * t(y – y.bar) %*% solve(S) %*% (y – y.bar) <= [ p * (n – 1) / (n – p)] * F.2.36-2

k <- (n / (n + 1)) / (p * (n - 1) / (n - p))
qfk <- sqrt(qf(p=0.95, df1=p, df2=c(n - p)) / k)               # F statistic for ellipse boundary (Hotelling's T2)

ctr    <- y.bar                                                # data centroid
A      <- cov(d.wei[d.wei$type == "ref", param])               # covariance matrix
RR     <- chol(A)                                              # Cholesky decomposition
angles <- seq(0, 2 * pi, length.out=200)                       # angles for ellipse
ell    <- qfk * cbind(cos(angles), sin(angles)) %*% RR         # ellipse scaled with factor qfk
ellCtr <- sweep(ell, 2, ctr, "+")                              # center ellipse to the data centroid

eigVal    <- eigen(S2)$values
eigVec    <- eigen(S2)$vectors
eigScl    <- eigVec %*% diag(sqrt(eigVal)) * qfk               # scale eigenvectors to length defined by sqrt(eigenvalues) * quantile
xMat      <- rbind(ctr[1] + eigScl[1, ], ctr[1] - eigScl[1, ])
yMat      <- rbind(ctr[2] + eigScl[2, ], ctr[2] - eigScl[2, ])

plot(d.wei[, param], pch=21, col=c("skyblue", "purple")[as.numeric(d.wei$type)], xlim=c(-3.5, 3.5), ylim=c(-3, 5), asp=1)
points(ctr[1], ctr[2], pch=4, col="red", lwd=2)                # plot data centroid
lines(ellCtr, col="royalblue")                                 # plot ellipse
matlines(xMat, yMat, lty=1, col="green")
points(xMat[, 1], yMat[, 1], pch=16, col="yellow")
points(xMat[, 2], yMat[, 2], pch=16, col="yellow")
points(xMat[2, 1], yMat[2, 1], pch=16, col="cyan")

L <- eigVec %*% t(RR) * qfk                                    # rotate and scale the matrix
L <- chol2inv(L)

tp <- c(xMat[2, 1], yMat[2, 1])                                # cyan coloured data point on ellipse boundary

diff <- (tp - ctr)
diff <- as.numeric(diff)

# Question: is the point tp covered by E (where E denotes the ellipsoid)?
# E covers tp if the quantity s = || L %*% (x - c) || <= 1

sqrt( sum((L %*% diff)^2) )

# Unfortunately, the result is not as expected, i.e. close to zero instead of close or equal to 1.
# Because the point tp sits on the ellipse boundary I would expect to get 1 as result.

0 个答案:

没有答案