#' Neutrosophic Analysis of Covariance for Completely Randomized Design
#'
#' Performs Neutrosophic Analysis of Covariance (NANCOVA) for interval-valued
#' response and covariate data from a Completely Randomized Design (CRD).
#'
#' @param Lower_y Numeric matrix of lower bounds of the response variable.
#' @param Upper_y Numeric matrix of upper bounds of the response variable.
#' @param Lower_z Numeric matrix of lower bounds of the covariate.
#' @param Upper_z Numeric matrix of upper bounds of the covariate.
#' @param design Numeric matrix representing the CRD treatment layout.
#' @param alpha Significance level for interval-based LSD comparisons. Default is 0.05.
#' @param verbose Logical. If \code{TRUE}, prints the NANCOVA table and LSD comparisons.
#'
#' @return A list containing the Neutrosophic ANCOVA table, interval LSD comparisons
#' (if treatment effects are significant), and the interval LSD value.
#'
#' @examples
#' Lower_y <- matrix(c(
#'   20,25,30,35,
#'   21,26,31,36,
#'   19,24,29,34,
#'   20,25,30,35,
#'   21,26,31,36
#' ), nrow = 5, byrow = TRUE)
#'
#' Upper_y <- matrix(c(
#'   22,27,32,37,
#'   23,28,33,38,
#'   21,26,31,36,
#'   22,27,32,37,
#'   23,28,33,38
#' ), nrow = 5, byrow = TRUE)
#'
#' Lower_z <- matrix(c(
#'   8,9,10,11,
#'   9,10,11,12,
#'   8,9,10,11,
#'   9,10,11,12,
#'   8,9,10,11
#' ), nrow = 5, byrow = TRUE)
#'
#' Upper_z <- matrix(c(
#'   9,10,11,12,
#'   10,11,12,13,
#'   9,10,11,12,
#'   10,11,12,13,
#'   9,10,11,12
#' ), nrow = 5, byrow = TRUE)
#'
#' design <- matrix(c(
#'   1,2,3,4,
#'   1,2,3,4,
#'   1,2,3,4,
#'   1,2,3,4,
#'   1,2,3,4
#' ), nrow = 5, byrow = TRUE)
#'
#' CRDnsANCOVA(Lower_y, Upper_y, Lower_z, Upper_z, design)
#'
#' @importFrom MASS ginv
#' @importFrom stats qf qt
#' @export

CRDnsANCOVA <- function(Lower_y, Upper_y,
                        Lower_z, Upper_z,
                        design,
                        alpha = 0.05,
                        verbose = FALSE){

  #-------------------------------------------------------
  # Input Validation and Initialization
  #-------------------------------------------------------

  Lower_y <- as.matrix(Lower_y)
  Upper_y <- as.matrix(Upper_y)
  Lower_z <- as.matrix(Lower_z)
  Upper_z <- as.matrix(Upper_z)
  design  <- as.matrix(design)

  v <- max(design)
  b <- nrow(design)
  k <- ncol(design)

  rep_counts <- table(design)
  r <- unique(rep_counts)

  interval <- function(L, U){
    list(
      d = (L + U)/2,
      i = abs(U - L)/2
    )
  }

  n <- b * k

  #-------------------------------------------------------
  # Construction of Experimental Design and Model Matrices
  #-------------------------------------------------------

  trt_vec <- as.vector(t(design))

  Tmat <- matrix(0, n, v)

  for(i in 1:n){
    Tmat[i, trt_vec[i]] <- 1
  }

  mu <- matrix(1, n, 1)

  X <- cbind(Tmat, mu)

  #-------------------------------------------------------
  # Interval Decomposition of Responses and Covariates
  #-------------------------------------------------------

  d_y <- (Lower_y + Upper_y)/2
  i_y <- abs(Upper_y - Lower_y)/2

  d_z <- (Lower_z + Upper_z)/2
  i_z <- abs(Upper_z - Lower_z)/2

  #-------------------------------------------------------
  # Computation of Overall Interval Means and
  # Centered Responses and Covariates
  #-------------------------------------------------------

  ybar_l <- mean(Lower_y)
  ybar_u <- mean(Upper_y)
  ybar_d <- mean(d_y)
  ybar_i <- abs(ybar_u - ybar_l)/2

  zbar_l <- mean(Lower_z)
  zbar_u <- mean(Upper_z)
  zbar_d <- (zbar_l + zbar_u)/2
  zbar_i <- abs(zbar_u - zbar_l)/2

  yminusbar_l <- d_y - ybar_d
  yminusbar_u <- yminusbar_l + i_y - ybar_i
  yminusbar_d <- (yminusbar_l + yminusbar_u)/2
  yminusbar_i <- abs(yminusbar_u - yminusbar_l)/2

  zminusbar_l <- d_z - zbar_d
  zminusbar_u <- zminusbar_l + i_z - zbar_i
  zminusbar_d <- (zminusbar_l + zminusbar_u)/2
  zminusbar_i <- abs(zminusbar_u - zminusbar_l)/2

  dy <- as.vector(t(yminusbar_d))
  iy <- as.vector(t(yminusbar_i))

  dz <- as.vector(t(zminusbar_d))
  iz <- as.vector(t(zminusbar_i))

  #-------------------------------------------------------
  # Computation of Interval Cross Products
  #-------------------------------------------------------

  zpz_l <- t(dz) %*% dz
  zpz_u <- zpz_l +
    t(dz) %*% iz +
    t(iz) %*% dz +
    t(iz) %*% iz

  zpz_d <- (zpz_l + zpz_u)/2
  zpz_i <- abs(zpz_u - zpz_l)/2

  zpy_l <- t(dz) %*% dy
  zpy_u <- zpy_l +
    t(dz) %*% iy +
    t(iz) %*% dy +
    t(iz) %*% iy

  zpy_d <- (zpy_l + zpy_u)/2
  zpy_i <- abs(zpy_u - zpy_l)/2

  ypy_l <- t(dy) %*% dy
  ypy_u <- ypy_l +
    t(dy) %*% iy +
    t(iy) %*% dy +
    t(iy) %*% iy

  ypy_d <- (ypy_l + ypy_u)/2
  ypy_i <- abs(ypy_u - ypy_l)/2


  #-------------------------------------------------------
  # Estimation of Adjusted Treatment and Overall Mean Effects
  #-------------------------------------------------------

  Z <- cbind(mu)

  M <- diag(nrow(X)) - Z %*% ginv(t(Z) %*% Z) %*% t(Z)

  Ct_adj <- t(Tmat) %*% M %*% Tmat

  bcdy_t_ad_d <- t(Tmat) %*% cbind(mu) %*%
    ginv(t(cbind(mu)) %*% cbind(mu)) %*%
    t(cbind(mu)) %*% dy

  bcdy_t_ad_i <- t(Tmat) %*% cbind(mu) %*%
    ginv(t(cbind(mu)) %*% cbind(mu)) %*%
    t(cbind(mu)) %*% iy

  Qy_ad_l <- t(Tmat) %*% dy - bcdy_t_ad_d

  Qy_ad_u <- Qy_ad_l[(1:v), ] +
    t(Tmat) %*% iy - bcdy_t_ad_i

  Qy_t_ad_d <- (Qy_ad_l + Qy_ad_u)/2
  Qy_t_ad_i <- abs(Qy_ad_l - Qy_ad_u)/2

  ay_t_ad_l <- ginv(Ct_adj) %*% Qy_t_ad_d

  ay_t_ad_u <- ay_t_ad_l + Qy_t_ad_i/b

  ay_t_ad_d <- (ay_t_ad_l + ay_t_ad_u)/2
  ay_t_ad_i <- abs(ay_t_ad_l - ay_t_ad_u)/2

  ################ Overall Mean ################

  Qy_mu_l <- sum(round(dy, 6))
  Qy_mu_u <- Qy_mu_l + sum(round(iy, 7))

  Qy_mu_d <- (Qy_mu_l + Qy_mu_u)/2
  Qy_mu_i <- abs(Qy_mu_l - Qy_mu_u)/2

  ay_mu_l <- Qy_mu_d/(b * k)
  ay_mu_u <- ay_mu_l + Qy_mu_d/(b * k)

  ay_mu_d <- (ay_mu_l + ay_mu_u)/2
  ay_mu_i <- abs(ay_mu_l - ay_mu_u)/2

  Qy_all_ad_d <- c(Qy_t_ad_d, Qy_mu_d)
  Qy_all_ad_i <- c(Qy_t_ad_i, Qy_mu_i)

  Qy_t_ad_d <- c(Qy_t_ad_d)
  Qy_t_ad_i <- c(Qy_t_ad_i)

  Qy_mue_ad_d <- c(Qy_mu_d)
  Qy_mue_ad_i <- c(Qy_mu_i)

  ay_all_ad_d <- c(ay_t_ad_d, ay_mu_d)
  ay_all_ad_i <- c(ay_t_ad_i, ay_mu_i)

  ay_t_ad_d <- c(ay_t_ad_d)
  ay_t_ad_i <- c(ay_t_ad_i)

  ay_mue_ad_d <- c(ay_mu_d)
  ay_mue_ad_i <- c(ay_mu_i)


  #-------------------------------------------------------
  # Estimation of Adjusted Covariate and Overall Mean Effects
  #-------------------------------------------------------

  Z <- cbind(mu)

  M <- diag(nrow(X)) - Z %*% ginv(t(Z) %*% Z) %*% t(Z)

  Ct_adj <- t(Tmat) %*% M %*% Tmat

  bcdz_t_ad_d <- t(Tmat) %*% cbind(mu) %*%
    ginv(t(cbind(mu)) %*% cbind(mu)) %*%
    t(cbind(mu)) %*% dz

  bcdz_t_ad_i <- t(Tmat) %*% cbind(mu) %*%
    ginv(t(cbind(mu)) %*% cbind(mu)) %*%
    t(cbind(mu)) %*% iz

  #### Covariate

  Z <- cbind(mu)

  M <- diag(nrow(X)) - Z %*% ginv(t(Z) %*% Z) %*% t(Z)

  Ct_adj <- t(Tmat) %*% M %*% Tmat

  bcdz_t_ad_d <- t(Tmat) %*% cbind(mu) %*%
    ginv(t(cbind(mu)) %*% cbind(mu)) %*%
    t(cbind(mu)) %*% dz

  bcdz_t_ad_i <- t(Tmat) %*% cbind(mu) %*%
    ginv(t(cbind(mu)) %*% cbind(mu)) %*%
    t(cbind(mu)) %*% iz

  Qz_ad_l <- t(Tmat) %*% dz - bcdz_t_ad_d

  Qz_ad_u <- Qz_ad_l +
    t(Tmat) %*% iz - bcdz_t_ad_i

  Qz_t_ad_d <- (Qz_ad_l + Qz_ad_u)/2
  Qz_t_ad_i <- abs(Qz_ad_l - Qz_ad_u)/2

  az_t_ad_l <- ginv(Ct_adj) %*% Qz_t_ad_d

  az_t_ad_u <- az_t_ad_l + Qz_t_ad_i/r

  az_t_ad_d <- (az_t_ad_l + az_t_ad_u)/2
  az_t_ad_i <- abs(az_t_ad_l - az_t_ad_u)/2

  ################ Overall Mean ################

  Qz_mu_l <- sum(round(dz, 6))
  Qz_mu_u <- Qz_mu_l + sum(round(iz, 7))

  Qz_mu_d <- (Qz_mu_l + Qz_mu_u)/2
  Qz_mu_i <- abs(Qz_mu_l - Qz_mu_u)/2

  az_mu_l <- Qz_mu_d/(b * k)
  az_mu_u <- az_mu_l + Qz_mu_d/(b * k)

  az_mu_d <- (az_mu_l + az_mu_u)/2
  az_mu_i <- abs(az_mu_l - az_mu_u)/2

  Qz_all_ad_d <- c(Qz_t_ad_d, Qz_mu_d)
  Qz_all_ad_i <- c(Qz_t_ad_i, Qz_mu_i)

  Qz_mue_ad_d <- c(Qz_mu_d)
  Qz_mue_ad_i <- c(Qz_mu_i)

  az_all_ad_d <- c(az_t_ad_d, az_mu_d)
  az_all_ad_i <- c(az_t_ad_i, az_mu_i)

  az_mue_ad_d <- c(az_mu_d)
  az_mue_ad_i <- c(az_mu_i)

  #-------------------------------------------------------
  # Computation of Interval Cross Products for
  # Regression Coefficient Estimation
  #-------------------------------------------------------

  #### Qz × Az

  QZAZ_all_ad_l <- c(Qz_t_ad_d * az_t_ad_d,
                     Qz_mu_d * az_mu_d)

  QZAZ_all_ad_u <- QZAZ_all_ad_l +
    c(Qz_t_ad_d * az_t_ad_i,
      Qz_mu_d * az_mu_i) +
    c(Qz_t_ad_i * az_t_ad_d,
      Qz_mu_i * az_mu_d) +
    c(Qz_t_ad_i * az_t_ad_i,
      Qz_mu_i * az_mu_i)

  QZAZ_all_ad_d <- sum((QZAZ_all_ad_l + QZAZ_all_ad_u)/2)
  QZAZ_all_ad_i <- sum(abs(QZAZ_all_ad_u - QZAZ_all_ad_l)/2)

  QZAZ_mue_ad_l <- c(Qz_mu_d * az_mu_d)

  QZAZ_mue_ad_u <- QZAZ_mue_ad_l +
    c(Qz_mu_d * az_mu_i) +
    c(Qz_mu_i * az_mu_d) +
    c(Qz_mu_i * az_mu_i)

  QZAZ_mue_ad_d <- sum(QZAZ_mue_ad_l + QZAZ_mue_ad_u)/2
  QZAZ_mue_ad_i <- sum(abs(QZAZ_mue_ad_u - QZAZ_mue_ad_l)/2)

  #### Qz × Ay

  QZAy_all_ad_l <- c(Qz_t_ad_d * ay_t_ad_d,
                     Qz_mu_d * ay_mu_d)

  QZAy_all_ad_u <- QZAy_all_ad_l +
    c(Qz_t_ad_d * ay_t_ad_i,
      Qz_mu_d * ay_mu_i) +
    c(Qz_t_ad_i * ay_t_ad_d,
      Qz_mu_i * ay_mu_d) +
    c(Qz_t_ad_i * ay_t_ad_i,
      Qz_mu_i * ay_mu_i)

  QZAy_all_ad_d <- sum((QZAy_all_ad_l + QZAy_all_ad_u)/2)
  QZAy_all_ad_i <- sum(abs(QZAy_all_ad_u - QZAy_all_ad_l)/2)

  QZAy_mue_ad_l <- c(Qz_mu_d * ay_mu_d)

  QZAy_mue_ad_u <- QZAy_mue_ad_l +
    c(Qz_mu_d * ay_mu_i) +
    c(Qz_mu_i * ay_mu_d) +
    c(Qz_mu_i * ay_mu_i)

  QZAy_mue_ad_d <- sum(QZAy_mue_ad_l + QZAy_mue_ad_u)/2
  QZAy_mue_ad_i <- sum(abs(QZAy_mue_ad_u - QZAy_mue_ad_l)/2)


  #-------------------------------------------------------
  # Estimation of Neutrosophic Regression Coefficients
  #-------------------------------------------------------

  ###### EXX

  EXX_all_ad_l <- zpz_d - QZAZ_all_ad_d
  EXX_all_ad_u <- EXX_all_ad_l + zpz_i - QZAZ_all_ad_i

  EXX_all_ad_d <- (EXX_all_ad_l + EXX_all_ad_u)/2
  EXX_all_ad_i <- abs(EXX_all_ad_u - EXX_all_ad_l)/2

  EXX_mue_ad_l <- zpz_d - QZAZ_mue_ad_d
  EXX_mue_ad_u <- EXX_mue_ad_l + zpz_i - QZAZ_mue_ad_i

  EXX_mue_ad_d <- (EXX_mue_ad_l + EXX_mue_ad_u)/2
  EXX_mue_ad_i <- abs(EXX_mue_ad_u - EXX_mue_ad_l)/2

  ###### EXY

  EXy_all_ad_l <- zpy_d - QZAy_all_ad_d
  EXy_all_ad_u <- EXy_all_ad_l + zpy_i - QZAy_all_ad_i

  EXy_all_ad_d <- (EXy_all_ad_l + EXy_all_ad_u)/2
  EXy_all_ad_i <- abs(EXy_all_ad_u - EXy_all_ad_l)/2

  EXy_mue_ad_l <- zpy_d - QZAy_mue_ad_d
  EXy_mue_ad_u <- EXy_mue_ad_l + zpy_i - QZAy_mue_ad_i

  EXy_mue_ad_d <- (EXy_mue_ad_l + EXy_mue_ad_u)/2
  EXy_mue_ad_i <- abs(EXy_mue_ad_u - EXy_mue_ad_l)/2

  ###### Regression Coefficients

  gamma_1_ad_l <- EXy_all_ad_d / EXX_all_ad_d

  gamma_1_ad_u <- gamma_1_ad_l +
    (EXy_all_ad_d * EXX_all_ad_i +
       EXy_all_ad_i * EXX_all_ad_d +
       EXy_all_ad_i * EXX_all_ad_i) /
    (EXX_all_ad_d * EXX_all_ad_d)

  gamma_1_ad_d <- (gamma_1_ad_l + gamma_1_ad_u)/2
  gamma_1_ad_i <- abs(gamma_1_ad_u - gamma_1_ad_l)/2

  gamma_2_ad_l <- EXy_mue_ad_d / EXX_mue_ad_d

  gamma_2_ad_u <- gamma_2_ad_l +
    (EXy_mue_ad_d * EXX_mue_ad_i +
       EXy_mue_ad_i * EXX_mue_ad_d +
       EXy_mue_ad_i * EXX_mue_ad_i) /
    (EXX_mue_ad_d * EXX_mue_ad_d)

  gamma_2_ad_d <- (gamma_2_ad_l + gamma_2_ad_u)/2
  gamma_2_ad_i <- abs(gamma_2_ad_u - gamma_2_ad_l)/2


  #-------------------------------------------------------
  # Computation of Covariate-Adjusted Effects
  #-------------------------------------------------------

  ###### Gamma × Az

  gaz_all_ad_l <- az_all_ad_d %*% gamma_1_ad_d

  gaz_all_ad_u <- gaz_all_ad_l +
    az_all_ad_d %*% gamma_1_ad_i +
    az_all_ad_i %*% gamma_1_ad_d +
    az_all_ad_i %*% gamma_1_ad_i

  gaz_all_ad_d <- (gaz_all_ad_l + gaz_all_ad_u)/2
  gaz_all_ad_i <- abs(gaz_all_ad_u - gaz_all_ad_l)/2

  acap_all_ad_l <- ay_all_ad_d - gaz_all_ad_d

  acap_all_ad_u <- acap_all_ad_l +
    ay_all_ad_i - gaz_all_ad_i

  acap_all_ad_d <- (acap_all_ad_l + acap_all_ad_u)/2
  acap_all_ad_i <- abs(acap_all_ad_u - acap_all_ad_l)/2


  gaz_mue_ad_l <- az_mue_ad_d %*% gamma_2_ad_d

  gaz_mue_ad_u <- gaz_mue_ad_l +
    az_mue_ad_d %*% gamma_2_ad_i +
    az_mue_ad_i %*% gamma_2_ad_d +
    az_mue_ad_i %*% gamma_2_ad_i

  gaz_mue_ad_d <- (gaz_mue_ad_l + gaz_mue_ad_u)/2
  gaz_mue_ad_i <- abs(gaz_mue_ad_u - gaz_mue_ad_l)/2

  acap_mue_ad_l <- ay_mue_ad_d - gaz_mue_ad_d

  acap_mue_ad_u <- acap_mue_ad_l +
    ay_mue_ad_i - gaz_mue_ad_i

  acap_mue_ad_d <- (acap_mue_ad_l + acap_mue_ad_u)/2
  acap_mue_ad_i <- abs(acap_mue_ad_u - acap_mue_ad_l)/2

  #-------------------------------------------------------
  # Computation of Covariate-Adjusted Sums of Squares
  #-------------------------------------------------------

  acapss_all_ad_l <- Qy_all_ad_d * acap_all_ad_d

  acapss_all_ad_u <- acapss_all_ad_l +
    Qy_all_ad_d * acap_all_ad_i +
    Qy_all_ad_i * acap_all_ad_d +
    Qy_all_ad_i * acap_all_ad_i

  acapss_all_ad_d <- (acapss_all_ad_u + acapss_all_ad_l)/2
  acapss_all_ad_i <- abs(acapss_all_ad_u - acapss_all_ad_l)/2

  acapss_mue_ad_l <- Qy_mue_ad_d * acap_mue_ad_d

  acapss_mue_ad_u <- acapss_mue_ad_l +
    Qy_mue_ad_d * acap_mue_ad_i +
    Qy_mue_ad_i * acap_mue_ad_d +
    Qy_mue_ad_i * acap_mue_ad_i

  acapss_mue_ad_d <- (acapss_mue_ad_u + acapss_mue_ad_l)/2
  acapss_mue_ad_i <- abs(acapss_mue_ad_u - acapss_mue_ad_l)/2

  ss_all_ad_l <- sum(acapss_all_ad_l)
  ss_all_ad_u <- sum(acapss_all_ad_u)
  ss_all_ad_d <- (ss_all_ad_l + ss_all_ad_u)/2
  ss_all_ad_i <- sum(acapss_all_ad_i)

  ss_mue_ad_l <- sum(acapss_mue_ad_l)
  ss_mue_ad_u <- sum(acapss_mue_ad_u)
  ss_mue_ad_d <- (ss_mue_ad_l + ss_mue_ad_u)/2
  ss_mue_ad_i <- sum(acapss_mue_ad_i)

  gzy_1_ad_l <- gamma_1_ad_d * zpy_d

  gzy_1_ad_u <- gzy_1_ad_l +
    gamma_1_ad_d * zpy_i +
    gamma_1_ad_i * zpy_d +
    gamma_1_ad_i * zpy_i

  gzy_1_ad_d <- (gzy_1_ad_l + gzy_1_ad_u)/2
  gzy_1_ad_i <- abs(gzy_1_ad_u - gzy_1_ad_l)/2

  gzy_2_ad_l <- gamma_2_ad_d * zpy_d

  gzy_2_ad_u <- gzy_2_ad_l +
    gamma_2_ad_d * zpy_i +
    gamma_2_ad_i * zpy_d +
    gamma_2_ad_i * zpy_i

  gzy_2_ad_d <- (gzy_2_ad_l + gzy_2_ad_u)/2
  gzy_2_ad_i <- abs(gzy_2_ad_u - gzy_2_ad_l)/2

  mdss_all_ad_l <- ss_all_ad_d + gzy_1_ad_d

  mdss_all_ad_u <- mdss_all_ad_l +
    ss_all_ad_i + gzy_1_ad_i

  mdss_all_ad_d <- (mdss_all_ad_u + mdss_all_ad_l)/2
  mdss_all_ad_i <- abs(mdss_all_ad_u - mdss_all_ad_l)/2

  mdss_mue_ad_l <- ss_mue_ad_d + gzy_2_ad_d

  mdss_mue_ad_u <- mdss_mue_ad_l +
    ss_mue_ad_i + gzy_2_ad_i

  mdss_mue_ad_d <- (mdss_mue_ad_u + mdss_mue_ad_l)/2
  mdss_mue_ad_i <- abs(mdss_mue_ad_u - mdss_mue_ad_l)/2

  #-------------------------------------------------------
  # Computation of Error, Treatment, and Covariate
  # Sums of Squares
  #-------------------------------------------------------

  #### Error Sum of Squares

  ess_all_ad_l <- ypy_d - mdss_all_ad_d
  ess_all_ad_u <- ess_all_ad_l + ypy_i - mdss_all_ad_i

  ess_all_ad_d <- (ess_all_ad_l + ess_all_ad_u)/2
  ess_all_ad_i <- abs(ess_all_ad_u - ess_all_ad_l)/2

  esst_ad_l <- ypy_d - mdss_mue_ad_d
  esst_ad_u <- esst_ad_l + ypy_i - mdss_mue_ad_i

  esst_ad_d <- (esst_ad_u + esst_ad_l)/2
  esst_ad_i <- abs(esst_ad_u - esst_ad_l)/2

  #### Treatment Sum of Squares Adjusted for Covariate

  tss_ad_l <- esst_ad_d - ess_all_ad_d
  tss_ad_u <- tss_ad_l + esst_ad_i - ess_all_ad_i

  tss_ad_d <- (tss_ad_u + tss_ad_l)/2
  tss_ad_i <- abs(tss_ad_u - tss_ad_l)/2

  #### Covariate Sum of Squares

  gss_l <- gamma_1_ad_d * EXy_all_ad_d

  gss_u <- gss_l +
    gamma_1_ad_d * EXy_all_ad_i +
    gamma_1_ad_i * EXy_all_ad_d +
    gamma_1_ad_i * EXy_all_ad_i

  gss_d <- (gss_l + gss_u)/2
  gss_i <- abs(gss_u - gss_l)/2

  #### Total Sum of Squares

  toss_l <- t(dy) %*% dy

  toss_u <- toss_l +
    2 * t(dy) %*% iy +
    t(iy) %*% iy

  toss_d <- (toss_l + toss_u)/2
  toss_i <- abs(toss_u - toss_l)/2

  #-------------------------------------------------------
  # Computation of Interval Mean Squares and
  # F-Statistics
  #-------------------------------------------------------

  SV <- c("Covariate", "Treatment", "Error", "Total")

  DF <- c(1, v - 1, n - v - 1, n - 1)

  SSN <- list(
    c(gss_l, gss_u),
    c(tss_ad_l, tss_ad_u),
    c(ess_all_ad_l, ess_all_ad_u),
    c(toss_l, toss_u)
  )

  MSSN <- list(
    c(gss_l, gss_u),
    c(tss_ad_l/(v - 1), tss_ad_u/(v - 1)),
    c(ess_all_ad_l/(n - v - 1), ess_all_ad_u/(n - v - 1)),
    c(NA, NA)
  )

  F_g_L <- (gss_d/1) /
    (ess_all_ad_d/(n - v - 1))

  F_g_U <- F_g_L +
    ((gss_d * (ess_all_ad_i/(n - v - 1)) +
        gss_i * (ess_all_ad_d/(n - v - 1)) +
        gss_i * (ess_all_ad_i/(n - v - 1))) /
       ((ess_all_ad_d/(n - v - 1)) *
          (ess_all_ad_d/(n - v - 1))))

  F_t_ad_L <- (tss_ad_d/(v - 1)) /
    (ess_all_ad_d/(n - v - 1))

  F_t_ad_U <- F_t_ad_L +
    (((tss_ad_d/(v - 1)) *
        (ess_all_ad_i/(n - v - 1)) +
        (tss_ad_i/(v - 1)) *
        (ess_all_ad_d/(n - v - 1)) +
        (tss_ad_i/(v - 1)) *
        (ess_all_ad_i/(n - v - 1))) /
       ((ess_all_ad_d/(n - v - 1)) *
          (ess_all_ad_d/(n - v - 1))))

  FN <- list(
    c(F_g_L, F_g_U),
    c(F_t_ad_L, F_t_ad_U),
    c(NA, NA),
    c(NA, NA)
  )

  format_interval <- function(x){
    if(any(is.na(x))) return("")
    paste0("[", round(x[1], 2), ", ", round(x[2], 2), "]")
  }

  SSN_fmt <- sapply(SSN, format_interval)
  MSSN_fmt <- sapply(MSSN, format_interval)
  FN_fmt <- sapply(FN, format_interval)

  nancova_table <- data.frame(
    SV = SV,
    DF = DF,
    SSN = SSN_fmt,
    MSSN = MSSN_fmt,
    FN = FN_fmt,
    stringsAsFactors = FALSE
  )

  #-------------------------------------------------------
  # Determination of Significance
  #-------------------------------------------------------

  Ftab_5   <- qf(0.95, 1, n - v - 1)
  Ftab_1   <- qf(0.99, 1, n - v - 1)
  Ftab_001 <- qf(0.999, 1, n - v - 1)

  cov_significance <- ifelse(
    F_g_L > Ftab_001, "***",
    ifelse(
      F_g_L > Ftab_1, "**",
      ifelse(
        F_g_L > Ftab_5, "*", "NS"
      )
    )
  )

  Ftab_5   <- qf(0.95, v - 1, n - v - 1)
  Ftab_1   <- qf(0.99, v - 1, n - v - 1)
  Ftab_001 <- qf(0.999, v - 1, n - v - 1)

  trt_significance <- ifelse(
    F_t_ad_L > Ftab_001, "***",
    ifelse(
      F_t_ad_L > Ftab_1, "**",
      ifelse(
        F_t_ad_L > Ftab_5, "*", "NS"
      )
    )
  )

  nancova_table$Significance <- c(
    cov_significance,
    trt_significance,
    "",
    ""
  )

  #-------------------------------------------------------
  # Interval-Based LSD Analysis
  #-------------------------------------------------------

  if(trt_significance != "NS"){

    dfe <- n - v - 1

    MSE_d <- ess_all_ad_d/dfe
    MSE_i <- ess_all_ad_i/dfe

    SEd_d <- sqrt((2*MSE_d)/r)
    SEd_i <- sqrt((2*MSE_i)/r)

    tval <- qt(1-alpha/2,dfe)

    LSD_l <- tval*SEd_d
    LSD_u <- LSD_l+tval*SEd_i

    adjmeans_d <- acap_all_ad_d[1:v]
    adjmeans_i <- acap_all_ad_i[1:v]

    comparison <- data.frame()

    for(i in 1:(v-1)){

      for(j in (i+1):v){

        Diff_l <- abs(adjmeans_d[i]-adjmeans_d[j])
        Diff_u <- Diff_l+adjmeans_i[i]+adjmeans_i[j]

        Lower <- min(Diff_l,Diff_u)
        Upper <- max(Diff_l,Diff_u)

        if(Upper < LSD_l){

          Result <- "NS"

        }else if(Lower > LSD_u){

          Result <- "S"

        }else{

          Result <- "I"

        }

        comparison <- rbind(
          comparison,
          data.frame(
            Treatment1=i,
            Treatment2=j,
            Difference=paste0("[",
                              round(Lower,2),
                              ", ",
                              round(Upper,2),
                              "]"),
            LSD=paste0("[",
                       round(LSD_l,2),
                       ", ",
                       round(LSD_u,2),
                       "]"),
            Result=Result,
            stringsAsFactors=FALSE
          )
        )

      }

    }

  }else{

    comparison <- NULL
    LSD_l <- NULL
    LSD_u <- NULL

  }

  #-------------------------------------------------------
  # Display Results
  #-------------------------------------------------------

  if(verbose){

    message("\nNeutrosophic Analysis of Covariance Table")
    print(nancova_table,row.names=FALSE)

    if(trt_significance != "NS"){

      message("\nTreatment effect is significant. Hence multiple comparison using LSD is performed.")

      message("\nLSD Interval : [",
              round(LSD_l,4),
              ", ",
              round(LSD_u,4),
              "]")

      message("\nTreatment Comparisons Using LSD")

      print(comparison,row.names=FALSE)

    }else{

      message("\nTreatment effect is non significant. Hence multiple comparison is not performed.")

    }

    message("\nSignificance Codes:")
    message("*** : Significant at p < 0.001")
    message("**  : Significant at p < 0.01")
    message("*   : Significant at p < 0.05")
    message("NS  : Non Significant")
    message("I   : Indeterminate")

  }

  #-------------------------------------------------------
  # Return Results
  #-------------------------------------------------------

  invisible(list(
    nancova_table = nancova_table,
    comparison = comparison,
    LSD = if(!is.null(LSD_l))
      c(Lower=LSD_l,Upper=LSD_u)
    else NULL
  ))

}
