简体   繁体   English

如何将 mult.chart 中的 T^2 hotelling 分解为 output 中的矩阵?

[英]How to get the decomposition of T^2 hotelling in mult.chart as a matrix in output?

I run mult.chart package for T^2 hotelling.我为 T^2 酒店运行 mult.chart package。 I want to get just "the first point" that is out of control and "it's decomposition" as a matrix.But running these codes get all out of control points and all decomposition in the type of "list".我只想得到失控的“第一个点”和“它的分解”作为矩阵。但是运行这些代码会使所有的控制点和“列表”类型的所有分解都失控。 and i can't separate the first point and it's decomposition and save them.而且我无法将第一点分开,它正在分解并保存它们。 what should i do?我应该怎么办?

    a<- runif(400,0,1)
    b<- matrix(a, nrow=100,ncol=4)
    output <- mult.chart(type="t2", alpha=0.07,b)

I expect the output for example number 70 (out of control point) and the decomposition matrix of that with 15 row and 7 columns (titles: t2 decomp, ucl, p-value,1,2,3, 4), but the actual output is a list with all out of control points and the decomposition of those as a list.我希望 output 例如数字 70(失控点)和 15 行 7 列的分解矩阵(标题:t2 decomp、ucl、p-value、1、2、3、4),但实际output 是一个包含所有失控点的列表,并将这些点分解为一个列表。

The table you saw, is only printed when your type is t2 and is not stored in output.您看到的表格仅在您的类型为 t2 时打印,并且未存储在 output 中。 You can see in the code the table is variable q, and not returned.您可以在代码中看到该表是变量 q,并且没有返回。

I basically took the calculation part out (nasty bit of code) and wrote it into a function:我基本上把计算部分(讨厌的代码)写到了 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)
}

Now if you run your mult.chart again, and use this function, it should give you the table:现在如果你再次运行你的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM