How can I fix the while loop problem in R?

46 Views Asked by At

I wrote the code as like below, and sometime it gets proper value but sometime it could not give me the value for a long time.

I guess it looks like it has infinite problem with while function but I couldn't get it how to fix it.

I've already tried to search about the while loop but I guess I wrote proeprly but I couldn't get it why it sometime run properly and sometime run not.

Could you please give me advice or the proper modification?

Thank you.

rm(list=ls())

library(readxl)
library(dplyr)
library(ggplot2)

library(MASS)


# Mean Vector, Covariance Matrix Construction

mu <- c(0,0,0)
mu <- t(mu)
mu <- t(mu)
mu

# Construct 40 random variables for Phase II

mu2 <- c(1, 2, 1)
mu2 <- t(mu2)
mu2 <- t(mu2)
mu2

Sigma <- matrix(c(1, 0.9, 0.9, 0.9, 1, 0.9, 0.9, 0.9, 1), 3)
Sigma




getResult <- function(Result) {
    
    # Construct 50 Random Variables for Phase I
    Obs <- mvrnorm(50, mu = mu, Sigma = Sigma)
    
    VecT2 <- apply(Obs, 2, mean)
    VecT2 <- round(VecT2, 3)
    
    ST2 <- cov(Obs)
    ST2 <- round(ST2, 3)
    
    Obs <- as.matrix(Obs)    
    T2All <- rep(0, nrow(Obs))
    
    for(i in 1:nrow(Obs)) {
        T2All[i] = t(Obs[i, ] - VecT2) %*% solve(ST2) %*% (Obs[i, ] - VecT2) 
    }
    
    # Construct Control Limit
    
    Alpha <- 0.005
    
    M <- nrow(Obs)
    M
    
    p <- ncol(Obs)
    p
    
    UCL <- ((p * (M-1) * (M + 1))) / ((M - p) * M) * qf((1-Alpha), p, (M-p))
    
    UCL <- round(UCL, 3)
    
    Compare <- which(T2All > UCL)
    
    
    # Repeat when is there are Out of Control in Phase I with eliminating it
    
    while(isTRUE(Compare > UCL)) {
        
        Obs <- Obs[-Compare,]
        
        Alpha <- 0.005
        
        M <- nrow(Obs)
        
        p <- ncol(Obs)
        
        UCL <- ((p * (M-1) * (M + 1))) / ((M - p) * M) * qf((1-Alpha), p, (M-p))
        
        Compare <- which(T2All > UCL)
        
    }
    
    UCL <- round(UCL, 3)
    
    
    
    # Prepare Observations two types of cases with Variable 20_1, Variable 20_2
    
    Obs20_1 <- mvrnorm(20, mu = mu, Sigma = Sigma)
    Obs20_2 <- mvrnorm(20, mu = mu2, Sigma = Sigma)
    Obs40 <- rbind(Obs20_1, Obs20_2)
    Obs40 <- as.matrix(Obs40)
    
    
    T2 <- rep(0, nrow(Obs40))
    
    for(i in 1:nrow(Obs40)) {
        T2[i] = t(Obs40[i, ] - mu) %*% solve(Sigma) %*% (Obs40[i, ] - mu) 
    }
    
    Result <- which(T2 > UCL)[1]
    
    
    # Repeat when Out of Control occur in ARL0 section
    
    while(isTRUE(Result < 20)) {
        
        Obs20_1 <- mvrnorm(20, mu = mu, Sigma = Sigma)
        Obs40 <- rbind(Obs20_1, Obs20_2)
        Obs40 <- as.matrix(Obs40)
        
        T2 <- rep(0, nrow(Obs40))
        
        for(i in 1:nrow(Obs40)) {
            T2[i] = t(Obs40[i, ] - mu) %*% solve(Sigma) %*% (Obs40[i, ] - mu)
        }
        
        Result <- which(T2 > UCL)[1]
    }
    
    Result
}


# Result

Final <- replicate(n = 200, expr = getResult(Result))

Final <- Final - 20

Final

mean(Final)
1

There are 1 best solutions below

0
On

You could try using a for loop instead of a while loop.