[英]How to get the decomposition of T^2 hotelling in mult.chart as a matrix in output?
我為 T^2 酒店運行 mult.chart package。 我只想得到失控的“第一個點”和“它的分解”作為矩陣。但是運行這些代碼會使所有的控制點和“列表”類型的所有分解都失控。 而且我無法將第一點分開,它正在分解並保存它們。 我應該怎么辦?
a<- runif(400,0,1)
b<- matrix(a, nrow=100,ncol=4)
output <- mult.chart(type="t2", alpha=0.07,b)
我希望 output 例如數字 70(失控點)和 15 行 7 列的分解矩陣(標題:t2 decomp、ucl、p-value、1、2、3、4),但實際output 是一個包含所有失控點的列表,並將這些點分解為一個列表。
您看到的表格僅在您的類型為 t2 時打印,並且未存儲在 output 中。 您可以在代碼中看到該表是變量 q,並且沒有返回。
我基本上把計算部分(討厭的代碼)寫到了 function 中:
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,並使用這個 function,它應該給你表格:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.