如何将 mult.chart 中的 T^2 hotelling 分解为输出矩阵?
How to get the decomposition of T^2 hotelling in mult.chart as a matrix in output?
我 运行 mult.chart T^2 酒店套餐。我只想得到失控的 "the first point" 和 "it's decomposition" 作为 matrix.But 运行 宁这些代码得到所有失控点和所有分解 "list"。我无法将第一点和它分解并保存。我应该怎么办?
a<- runif(400,0,1)
b<- matrix(a, nrow=100,ncol=4)
output <- mult.chart(type="t2", alpha=0.07,b)
我期望输出例如数字 70(失控点)和具有 15 行和 7 列的分解矩阵(标题:t2 decomp,ucl,p-value,1,2,3 , 4), 但实际输出是一个列表,其中包含所有失控点并将这些点分解为列表。
您看到的 table 仅在您的类型为 t2 时打印并且不会存储在输出中。可以看到the code里面的table是变量q,没有返回。
我基本上去掉了计算部分(讨厌的代码)并将其写入函数:
printOutlier = function(x,output,alpha){
p <- ncol(x)
m <- nrow(x)
x <- array(data.matrix(x), c(m, p, 1))
n <- dim(x)[3]
phase <- 2
x.jk <- matrix(0, m, p)
t2=output$t2
x.jk <- apply(x, 1:2, mean)
Xmv <- output$Xmv
S <- output$covariance
colm <- nrow(x)
ucl = output$ucl
t3 <- which(t2 > ucl)
res = vector("list",length(t3))
for (ii in 1:length(t3)) {
v = 1
k = 0
for (i in 1:p) {
k <- k + factorial(p)/(factorial(i) * factorial(p -i))
}
q <- matrix(0, k, p + 3)
for (i in 1:p) {
a <- t(combn(p, i))
for (l in 1:nrow(a)) {
for (j in 1:ncol(a)) {
q[v, j + 3] <- a[l, j]
}
v = v + 1
}
}
for (i in 1:nrow(q)) {
b <- subset(q[i, 4:ncol(q)], q[i, 4:ncol(q)] > 0)
di <- length(b)
if (length(b) > 1) {
q[i, 1] <- n * t(Xmv[b] - x.jk[t3[ii], ][b]) %*%
solve(S[b, b]) %*% (Xmv[b] - x.jk[t3[ii],][b])
}
else (q[i, 1] <- n * (x.jk[t3[ii], ][b] - Xmv[b])^2/S[b, b])
ifelse(n == 1, ifelse(phase == 1, q[i, 2] <- ((colm -
1)^2)/colm * qbeta(1 - alpha, di/2, (((2 *
(colm - 1)^2)/(3 * colm - 4) - di - 1)/2)),
q[i, 2] <- ((di * (colm + 1) * (colm - 1))/((colm^2) -
colm * di)) * qf(1 - alpha, di, colm -
di)), ifelse(phase == 1, q[i, 2] <- (di *
(colm - 1) * (n - 1))/(colm * n - colm -
di + 1) * qf(1 - alpha, di, colm * n - colm -
di + 1), q[i, 2] <- (di * (colm + 1) * (n -
1))/(colm * n - colm - di + 1) * qf(1 - alpha,
di, colm * n - colm - di + 1)))
q[i, 3] <- 1 - pf(q[i, 1], di, colm - 1)
}
colnames(q) <- c("t2 decomp", "ucl", "p-value", 1:p)
names(res)[ii] <- paste(`Decomposition of` = t3[ii])
res[[ii]] <- round(q, 4)
}
return(res)
}
现在,如果您再次 运行 您的 mult.chart,并使用此功能,它应该会给您 table:
library(MSQC)
set.seed(111)
a<- runif(400,0,1)
b<- matrix(a, nrow=100,ncol=4)
output <- mult.chart(type="t2", alpha=0.07,b)
printOutlier(b,output,0.07)
我 运行 mult.chart T^2 酒店套餐。我只想得到失控的 "the first point" 和 "it's decomposition" 作为 matrix.But 运行 宁这些代码得到所有失控点和所有分解 "list"。我无法将第一点和它分解并保存。我应该怎么办?
a<- runif(400,0,1)
b<- matrix(a, nrow=100,ncol=4)
output <- mult.chart(type="t2", alpha=0.07,b)
我期望输出例如数字 70(失控点)和具有 15 行和 7 列的分解矩阵(标题:t2 decomp,ucl,p-value,1,2,3 , 4), 但实际输出是一个列表,其中包含所有失控点并将这些点分解为列表。
您看到的 table 仅在您的类型为 t2 时打印并且不会存储在输出中。可以看到the code里面的table是变量q,没有返回。
我基本上去掉了计算部分(讨厌的代码)并将其写入函数:
printOutlier = function(x,output,alpha){
p <- ncol(x)
m <- nrow(x)
x <- array(data.matrix(x), c(m, p, 1))
n <- dim(x)[3]
phase <- 2
x.jk <- matrix(0, m, p)
t2=output$t2
x.jk <- apply(x, 1:2, mean)
Xmv <- output$Xmv
S <- output$covariance
colm <- nrow(x)
ucl = output$ucl
t3 <- which(t2 > ucl)
res = vector("list",length(t3))
for (ii in 1:length(t3)) {
v = 1
k = 0
for (i in 1:p) {
k <- k + factorial(p)/(factorial(i) * factorial(p -i))
}
q <- matrix(0, k, p + 3)
for (i in 1:p) {
a <- t(combn(p, i))
for (l in 1:nrow(a)) {
for (j in 1:ncol(a)) {
q[v, j + 3] <- a[l, j]
}
v = v + 1
}
}
for (i in 1:nrow(q)) {
b <- subset(q[i, 4:ncol(q)], q[i, 4:ncol(q)] > 0)
di <- length(b)
if (length(b) > 1) {
q[i, 1] <- n * t(Xmv[b] - x.jk[t3[ii], ][b]) %*%
solve(S[b, b]) %*% (Xmv[b] - x.jk[t3[ii],][b])
}
else (q[i, 1] <- n * (x.jk[t3[ii], ][b] - Xmv[b])^2/S[b, b])
ifelse(n == 1, ifelse(phase == 1, q[i, 2] <- ((colm -
1)^2)/colm * qbeta(1 - alpha, di/2, (((2 *
(colm - 1)^2)/(3 * colm - 4) - di - 1)/2)),
q[i, 2] <- ((di * (colm + 1) * (colm - 1))/((colm^2) -
colm * di)) * qf(1 - alpha, di, colm -
di)), ifelse(phase == 1, q[i, 2] <- (di *
(colm - 1) * (n - 1))/(colm * n - colm -
di + 1) * qf(1 - alpha, di, colm * n - colm -
di + 1), q[i, 2] <- (di * (colm + 1) * (n -
1))/(colm * n - colm - di + 1) * qf(1 - alpha,
di, colm * n - colm - di + 1)))
q[i, 3] <- 1 - pf(q[i, 1], di, colm - 1)
}
colnames(q) <- c("t2 decomp", "ucl", "p-value", 1:p)
names(res)[ii] <- paste(`Decomposition of` = t3[ii])
res[[ii]] <- round(q, 4)
}
return(res)
}
现在,如果您再次 运行 您的 mult.chart,并使用此功能,它应该会给您 table:
library(MSQC)
set.seed(111)
a<- runif(400,0,1)
b<- matrix(a, nrow=100,ncol=4)
output <- mult.chart(type="t2", alpha=0.07,b)
printOutlier(b,output,0.07)