#' Neutrosophic Analysis of Covariance for RCBD
#'
#' Performs neutrosophic analysis of covariance for a Randomized Complete
#' Block Design using interval-valued response and covariate data.
#'
#' @param Lower_y Numeric matrix of lower response values.
#' @param Upper_y Numeric matrix of upper response values.
#' @param Lower_z Numeric matrix of lower covariate values.
#' @param Upper_z Numeric matrix of upper covariate values.
#' @param design Numeric matrix representing the RCBD layout.
#' @param alpha Significance level for LSD test. Default is `0.05`.
#' @param verbose Logical; if `TRUE`, prints the analysis. Default is `FALSE`.
#'
#' @return A list containing the NANCOVA table, LSD interval, and treatment
#' comparisons (if treatment effect is significant).
#'
#' @importFrom MASS ginv
#' @importFrom stats qf qt
#' @examples
#' Lower_y <- matrix(c(
#'   46.84, 97.69, 39.30, 49.20,
#'   51.52, 83.38, 56.48, 42.15,
#'   44.42, 49.64, 44.21, 35.79,
#'   32.77, 74.30, 55.72, 70.55
#' ), nrow = 4, byrow = TRUE)
#'
#' Upper_y <- matrix(c(
#'   49.66,101.31,47.70,55.80,
#'   60.98,87.62,60.52,44.85,
#'   52.26,59.36,52.79,44.61,
#'   42.23,82.70,59.28,75.45
#' ), nrow = 4, byrow = TRUE)
#'
#' Lower_z <- matrix(c(
#'   224.95,245.87,245.19,259.41,
#'   222.06,213.83,253.10,247.51,
#'   255.67,230.55,265.05,246.67,
#'   137.98,214.68,251.49,257.70
#' ), nrow = 4, byrow = TRUE)
#'
#' Upper_z <- matrix(c(
#'   229.05,250.13,252.81,268.59,
#'   229.94,222.17,258.90,256.49,
#'   262.33,237.45,274.95,249.33,
#'   142.02,219.32,260.51,264.30
#' ), nrow = 4, byrow = TRUE)
#'
#' design <- matrix(c(
#'   1,2,3,4,
#'   1,2,3,4,
#'   1,2,3,4,
#'   1,2,3,4
#' ), nrow = 4, byrow = TRUE)
#'
#' result <- RCBDnsANCOVA(
#'   Lower_y,
#'   Upper_y,
#'   Lower_z,
#'   Upper_z,
#'   design,
#'   alpha = 0.05,
#'   verbose = TRUE
#' )
#' @export


RCBDnsANCOVA <- 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)

  rep_vec <- rep(1:r, each = b/r * k)

  Rmat <- matrix(0, n, r)

  for(i in 1:n){
    Rmat[i, rep_vec[i]] <- 1
  }

  X <- cbind(Tmat, mu, Rmat)

  #-------------------------------------------------------
  # Interval Decomposition of Response and Covariate
  #-------------------------------------------------------

  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 Interval Responses
  #-------------------------------------------------------

  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 Effects
  #-------------------------------------------------------

  Z <- cbind(mu, Rmat)

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

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

  bcdy_t_ad_d <- t(Tmat) %*% cbind(mu, Rmat) %*%
    MASS::ginv(t(cbind(mu, Rmat)) %*% cbind(mu, Rmat)) %*%
    t(cbind(mu, Rmat)) %*% dy

  bcdy_t_ad_i <- t(Tmat) %*% cbind(mu, Rmat) %*%
    MASS::ginv(t(cbind(mu, Rmat)) %*% cbind(mu, Rmat)) %*%
    t(cbind(mu, Rmat)) %*% 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 <- MASS::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

  #-------------------------------------------------------
  # Estimation of Adjusted Block Effects
  #-------------------------------------------------------

  Z <- cbind(mu, Tmat)

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

  Cb_adj <- t(Rmat) %*% M %*% Rmat

  bcdy_b_ad_d <- t(Rmat) %*% cbind(mu, Tmat) %*%
    MASS::ginv(t(cbind(mu, Tmat)) %*% cbind(mu, Tmat)) %*%
    t(cbind(mu, Tmat)) %*% dy

  bcdy_b_ad_i <- t(Rmat) %*% cbind(mu, Tmat) %*%
    MASS::ginv(t(cbind(mu, Tmat)) %*% cbind(mu, Tmat)) %*%
    t(cbind(mu, Tmat)) %*% iy

  Qy_ad_l <- t(Rmat) %*% dy - bcdy_b_ad_d

  Qy_ad_u <- Qy_ad_l[(1:b), ] +
    t(Rmat) %*% iy - bcdy_b_ad_i

  Qy_b_ad_d <- (Qy_ad_l + Qy_ad_u)/2
  Qy_b_ad_i <- abs(Qy_ad_l - Qy_ad_u)/2

  ay_b_ad_l <- MASS::ginv(Cb_adj) %*% Qy_b_ad_d

  ay_b_ad_u <- ay_b_ad_l + Qy_b_ad_i/v

  ay_b_ad_d <- (ay_b_ad_l + ay_b_ad_u)/2
  ay_b_ad_i <- abs(ay_b_ad_l - ay_b_ad_u)/2

  #-------------------------------------------------------
  # Estimation of Overall Mean Effects
  #-------------------------------------------------------

  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

  #-------------------------------------------------------
  # Combined Adjusted Effects
  #-------------------------------------------------------

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

  Qy_tmue_ad_d <- c(Qy_t_ad_d, Qy_mu_d)
  Qy_tmue_ad_i <- c(Qy_t_ad_i, Qy_mu_i)

  Qy_mueb_ad_d <- c(Qy_mu_d, Qy_b_ad_d)
  Qy_mueb_ad_i <- c(Qy_mu_i, Qy_b_ad_i)

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

  ay_tmue_ad_d <- c(ay_t_ad_d, ay_mu_d)
  ay_tmue_ad_i <- c(ay_t_ad_i, ay_mu_i)

  ay_mueb_ad_d <- c(ay_mu_d, ay_b_ad_d)
  ay_mueb_ad_i <- c(ay_mu_i, ay_b_ad_i)

  #-------------------------------------------------------
  # Estimation of Adjusted Covariate Effects
  #-------------------------------------------------------

  Z <- cbind(mu, Rmat)

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

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

  bcdz_t_ad_d <- t(Tmat) %*% cbind(mu, Rmat) %*%
    MASS::ginv(t(cbind(mu, Rmat)) %*% cbind(mu, Rmat)) %*%
    t(cbind(mu, Rmat)) %*% dz

  bcdz_t_ad_i <- t(Tmat) %*% cbind(mu, Rmat) %*%
    MASS::ginv(t(cbind(mu, Rmat)) %*% cbind(mu, Rmat)) %*%
    t(cbind(mu, Rmat)) %*% 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 <- MASS::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

  #-------------------------------------------------------
  # Estimation of Block Effects for Covariate
  #-------------------------------------------------------

  Cb_unadj <- t(Rmat) %*% Rmat

  bcdz_b_un_d <- t(cbind(Rmat, Tmat)) %*% mu %*%
    MASS::ginv(t(mu) %*% mu) %*%
    t(mu) %*% dz

  bcdz_b_un_i <- t(cbind(Rmat, Tmat)) %*% mu %*%
    MASS::ginv(t(mu) %*% mu) %*%
    t(mu) %*% iz

  Qz_un_l <- t(cbind(Rmat, Tmat)) %*% dz -
    bcdz_b_un_d

  Qz_un_u <- Qz_un_l +
    t(cbind(Rmat, Tmat)) %*% iz -
    bcdz_b_un_i

  Qz_b_un_d <- (Qz_un_l[1:r, ] + Qz_un_u[1:r, ])/2
  Qz_b_un_i <- abs(Qz_un_l[1:r, ] - Qz_un_u[1:r, ])/2

  az_b_l <- MASS::ginv(Cb_unadj) %*% Qz_b_un_d

  az_b_u <- az_b_l + Qz_b_un_i/v

  az_b_d <- (az_b_l + az_b_u)/2
  az_b_i <- abs(az_b_l - az_b_u)/2

  #-------------------------------------------------------
  # Estimation of Overall Mean for Covariate
  #-------------------------------------------------------

  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

  #-------------------------------------------------------
  # Combined Adjusted Covariate Effects
  #-------------------------------------------------------

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

  Qz_tmue_ad_d <- c(Qz_t_ad_d, Qz_mu_d)
  Qz_tmue_ad_i <- c(Qz_t_ad_i, Qz_mu_i)

  Qz_mueb_ad_d <- c(Qz_mu_d, Qz_b_un_d)
  Qz_mueb_ad_i <- c(Qz_mu_i, Qz_b_un_i)

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

  az_tmue_ad_d <- c(az_t_ad_d, az_mu_d)
  az_tmue_ad_i <- c(az_t_ad_i, az_mu_i)

  az_mueb_ad_d <- c(az_mu_d, az_b_d)
  az_mueb_ad_i <- c(az_mu_i, az_b_i)

  #-------------------------------------------------------
  # Computation of QZAZ Products
  #-------------------------------------------------------

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

  QZAZ_all_ad_u <- QZAZ_all_ad_l +
    c(Qz_t_ad_d * az_t_ad_i,
      Qz_mu_d * az_mu_i,
      Qz_b_un_d * az_b_i) +
    c(Qz_t_ad_i * az_t_ad_d,
      Qz_mu_i * az_mu_d,
      Qz_b_un_i * az_b_d) +
    c(Qz_t_ad_i * az_t_ad_i,
      Qz_mu_i * az_mu_i,
      Qz_b_un_i * az_b_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_tmue_ad_l <- c(
    Qz_t_ad_d * az_t_ad_d,
    Qz_mu_d * az_mu_d
  )

  QZAZ_tmue_ad_u <- QZAZ_tmue_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_tmue_ad_d <- sum((QZAZ_tmue_ad_l + QZAZ_tmue_ad_u)/2)
  QZAZ_tmue_ad_i <- sum(abs((QZAZ_tmue_ad_u - QZAZ_tmue_ad_l)/2))

  QZAZ_mueb_ad_l <- c(
    Qz_mu_d * az_mu_d,
    Qz_b_un_d * az_b_d
  )

  QZAZ_mueb_ad_u <- QZAZ_mueb_ad_l +
    c(Qz_mu_d * az_mu_i,
      Qz_b_un_d * az_b_i) +
    c(Qz_mu_i * az_mu_d,
      Qz_b_un_i * az_b_d) +
    c(Qz_mu_i * az_mu_i,
      Qz_b_un_i * az_b_i)

  QZAZ_mueb_ad_d <- sum(QZAZ_mueb_ad_l + QZAZ_mueb_ad_u)/2
  QZAZ_mueb_ad_i <- sum(abs(QZAZ_mueb_ad_u - QZAZ_mueb_ad_l)/2)
  #-------------------------------------------------------
  # Computation of QZAy Products
  #-------------------------------------------------------

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

  QZAy_all_ad_u <- QZAy_all_ad_l +
    c(Qz_t_ad_d * ay_t_ad_i,
      Qz_mu_d * ay_mu_i,
      Qz_b_un_d * ay_b_ad_i) +
    c(Qz_t_ad_i * ay_t_ad_d,
      Qz_mu_i * ay_mu_d,
      Qz_b_un_i * ay_b_ad_d) +
    c(Qz_t_ad_i * ay_t_ad_i,
      Qz_mu_i * ay_mu_i,
      Qz_b_un_i * ay_b_ad_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_tmue_ad_l <- c(
    Qz_t_ad_d * ay_t_ad_d,
    Qz_mu_d * ay_mu_d
  )

  QZAy_tmue_ad_u <- QZAy_tmue_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_tmue_ad_d <- sum((QZAy_tmue_ad_l + QZAy_tmue_ad_u)/2)
  QZAy_tmue_ad_i <- sum(abs((QZAy_tmue_ad_u - QZAy_tmue_ad_l)/2))

  QZAy_mueb_ad_l <- c(
    Qz_mu_d * ay_mu_d,
    Qz_b_un_d * ay_b_ad_d
  )

  QZAy_mueb_ad_u <- QZAy_mueb_ad_l +
    c(Qz_mu_d * ay_mu_i,
      Qz_b_un_d * ay_b_ad_i) +
    c(Qz_mu_i * ay_mu_d,
      Qz_b_un_i * ay_b_ad_d) +
    c(Qz_mu_i * ay_mu_i,
      Qz_b_un_i * ay_b_ad_i)

  QZAy_mueb_ad_d <- sum(QZAy_mueb_ad_l + QZAy_mueb_ad_u)/2
  QZAy_mueb_ad_i <- sum(abs(QZAy_mueb_ad_u - QZAy_mueb_ad_l)/2)

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

  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_tmue_ad_l <- zpz_d - QZAZ_tmue_ad_d
  EXX_tmue_ad_u <- EXX_tmue_ad_l + zpz_i - QZAZ_tmue_ad_i

  EXX_tmue_ad_d <- (EXX_tmue_ad_l + EXX_tmue_ad_u)/2
  EXX_tmue_ad_i <- abs(EXX_tmue_ad_u - EXX_tmue_ad_l)/2

  EXX_mueb_ad_l <- zpz_d - QZAZ_mueb_ad_d
  EXX_mueb_ad_u <- EXX_mueb_ad_l + zpz_i - QZAZ_mueb_ad_i

  EXX_mueb_ad_d <- (EXX_mueb_ad_l + EXX_mueb_ad_u)/2
  EXX_mueb_ad_i <- abs(EXX_mueb_ad_u - EXX_mueb_ad_l)/2

  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_tmue_ad_l <- zpy_d - QZAy_tmue_ad_d
  EXy_tmue_ad_u <- EXy_tmue_ad_l + zpy_i - QZAy_tmue_ad_i

  EXy_tmue_ad_d <- (EXy_tmue_ad_l + EXy_tmue_ad_u)/2
  EXy_tmue_ad_i <- abs(EXy_tmue_ad_u - EXy_tmue_ad_l)/2

  EXy_mueb_ad_l <- zpy_d - QZAy_mueb_ad_d
  EXy_mueb_ad_u <- EXy_mueb_ad_l + zpy_i - QZAy_mueb_ad_i

  EXy_mueb_ad_d <- (EXy_mueb_ad_l + EXy_mueb_ad_u)/2
  EXy_mueb_ad_i <- abs(EXy_mueb_ad_u - EXy_mueb_ad_l)/2

  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_tmue_ad_d/EXX_tmue_ad_d

  gamma_2_ad_u <- gamma_2_ad_l +
    (EXy_tmue_ad_d * EXX_tmue_ad_i +
       EXy_tmue_ad_i * EXX_tmue_ad_d +
       EXy_tmue_ad_i * EXX_tmue_ad_i) /
    (EXX_tmue_ad_d * EXX_tmue_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

  gamma_3_ad_l <- EXy_mueb_ad_d/EXX_mueb_ad_d

  gamma_3_ad_u <- gamma_3_ad_l +
    (EXy_mueb_ad_d * EXX_mueb_ad_i +
       EXy_mueb_ad_i * EXX_mueb_ad_d +
       EXy_mueb_ad_i * EXX_mueb_ad_i) /
    (EXX_mueb_ad_d * EXX_mueb_ad_d)

  gamma_3_ad_d <- (gamma_3_ad_l + gamma_3_ad_u)/2
  gamma_3_ad_i <- abs(gamma_3_ad_u - gamma_3_ad_l)/2
  #-------------------------------------------------------
  # Estimation of Adjusted Responses
  #-------------------------------------------------------

  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_tmue_ad_l <- az_tmue_ad_d %*% gamma_2_ad_d

  gaz_tmue_ad_u <- gaz_tmue_ad_l +
    az_tmue_ad_d %*% gamma_2_ad_i +
    az_tmue_ad_i %*% gamma_2_ad_d +
    az_tmue_ad_i %*% gamma_2_ad_i

  gaz_tmue_ad_d <- (gaz_tmue_ad_l + gaz_tmue_ad_u)/2
  gaz_tmue_ad_i <- abs(gaz_tmue_ad_u - gaz_tmue_ad_l)/2

  acap_tmue_ad_l <- ay_tmue_ad_d - gaz_tmue_ad_d
  acap_tmue_ad_u <- acap_tmue_ad_l + ay_tmue_ad_i - gaz_tmue_ad_i

  acap_tmue_ad_d <- (acap_tmue_ad_l + acap_tmue_ad_u)/2
  acap_tmue_ad_i <- abs(acap_tmue_ad_u - acap_tmue_ad_l)/2


  gaz_mueb_ad_l <- az_mueb_ad_d %*% gamma_3_ad_d

  gaz_mueb_ad_u <- gaz_mueb_ad_l +
    az_mueb_ad_d %*% gamma_3_ad_i +
    az_mueb_ad_i %*% gamma_3_ad_d +
    az_mueb_ad_i %*% gamma_3_ad_i

  gaz_mueb_ad_d <- (gaz_mueb_ad_l + gaz_mueb_ad_u)/2
  gaz_mueb_ad_i <- abs(gaz_mueb_ad_u - gaz_mueb_ad_l)/2

  acap_mueb_ad_l <- ay_mueb_ad_d - gaz_mueb_ad_d
  acap_mueb_ad_u <- acap_mueb_ad_l + ay_mueb_ad_i - gaz_mueb_ad_i

  acap_mueb_ad_d <- (acap_mueb_ad_l + acap_mueb_ad_u)/2
  acap_mueb_ad_i <- abs(acap_mueb_ad_u - acap_mueb_ad_l)/2

  #-------------------------------------------------------
  # Computation of Adjusted Sum 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_tmue_ad_l <- Qy_tmue_ad_d * acap_tmue_ad_d

  acapss_tmue_ad_u <- acapss_tmue_ad_l +
    Qy_tmue_ad_d * acap_tmue_ad_i +
    Qy_tmue_ad_i * acap_tmue_ad_d +
    Qy_tmue_ad_i * acap_tmue_ad_i

  acapss_tmue_ad_d <- (acapss_tmue_ad_u + acapss_tmue_ad_l)/2
  acapss_tmue_ad_i <- abs(acapss_tmue_ad_u - acapss_tmue_ad_l)/2


  acapss_mueb_ad_l <- Qy_mueb_ad_d * acap_mueb_ad_d

  acapss_mueb_ad_u <- acapss_mueb_ad_l +
    Qy_mueb_ad_d * acap_mueb_ad_i +
    Qy_mueb_ad_i * acap_mueb_ad_d +
    Qy_mueb_ad_i * acap_mueb_ad_i

  acapss_mueb_ad_d <- (acapss_mueb_ad_u + acapss_mueb_ad_l)/2
  acapss_mueb_ad_i <- abs(acapss_mueb_ad_u - acapss_mueb_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_tmue_ad_l <- sum(acapss_tmue_ad_l)
  ss_tmue_ad_u <- sum(acapss_tmue_ad_u)
  ss_tmue_ad_d <- (ss_tmue_ad_l + ss_tmue_ad_u)/2
  ss_tmue_ad_i <- sum(acapss_tmue_ad_i)

  ss_mueb_ad_l <- sum(acapss_mueb_ad_l)
  ss_mueb_ad_u <- sum(acapss_mueb_ad_u)
  ss_mueb_ad_d <- sum(acapss_mueb_ad_d)
  ss_mueb_ad_i <- sum(acapss_mueb_ad_i)

  #-------------------------------------------------------
  # Computation of Regression Sum of Squares
  #-------------------------------------------------------

  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


  gzy_3_ad_l <- gamma_3_ad_d * zpy_d

  gzy_3_ad_u <- gzy_3_ad_l +
    gamma_3_ad_d * zpy_i +
    gamma_3_ad_i * zpy_d +
    gamma_3_ad_i * zpy_i

  gzy_3_ad_d <- (gzy_3_ad_l + gzy_3_ad_u)/2
  gzy_3_ad_i <- abs(gzy_3_ad_u - gzy_3_ad_l)/2

  #-------------------------------------------------------
  # Computation of Model Sum of Squares
  #-------------------------------------------------------

  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_tmue_ad_l <- ss_tmue_ad_d + gzy_2_ad_d
  mdss_tmue_ad_u <- mdss_tmue_ad_l + ss_tmue_ad_i + gzy_2_ad_i

  mdss_tmue_ad_d <- (mdss_tmue_ad_u + mdss_tmue_ad_l)/2
  mdss_tmue_ad_i <- abs(mdss_tmue_ad_u - mdss_tmue_ad_l)/2


  mdss_mueb_ad_l <- ss_mueb_ad_d + gzy_3_ad_d
  mdss_mueb_ad_u <- mdss_mueb_ad_l + ss_mueb_ad_i + gzy_3_ad_i

  mdss_mueb_ad_d <- (mdss_mueb_ad_u + mdss_mueb_ad_l)/2
  mdss_mueb_ad_i <- abs(mdss_mueb_ad_u - mdss_mueb_ad_l)/2
  #-------------------------------------------------------
  # Computation of 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_mueb_ad_d

  esst_ad_u <- esst_ad_l +
    ypy_i - mdss_mueb_ad_i

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


  essb_ad_l <- ypy_d - mdss_tmue_ad_d

  essb_ad_u <- essb_ad_l +
    ypy_i - mdss_tmue_ad_i

  essb_ad_d <- (essb_ad_u + essb_ad_l)/2
  essb_ad_i <- abs(essb_ad_u - essb_ad_l)/2

  #-------------------------------------------------------
  # Computation of Adjusted Treatment and
  # Replication Sum of Squares
  #-------------------------------------------------------

  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


  bss_ad_l <- essb_ad_d - ess_all_ad_d

  bss_ad_u <- bss_ad_l +
    essb_ad_i - ess_all_ad_i

  bss_ad_d <- (bss_ad_u + bss_ad_l)/2
  bss_ad_i <- abs(bss_ad_u - bss_ad_l)/2

  #-------------------------------------------------------
  # Computation of 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

  #-------------------------------------------------------
  # Computation of 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",
    "Replication",
    "Error",
    "Total"
  )

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

  SSN <- list(
    c(gss_l, gss_u),
    c(tss_ad_l, tss_ad_u),
    c(bss_ad_l, bss_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(bss_ad_l/(b - 1), bss_ad_u/(b - 1)),
    c(
      ess_all_ad_l/((v - 1) * (b - 1) - 1),
      ess_all_ad_u/((v - 1) * (b - 1) - 1)
    ),
    c(NA, NA)
  )

  F_g_L <- (gss_d/1) /
    (ess_all_ad_d/((v - 1) * (b - 1) - 1))

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

  F_t_ad_L <- (tss_ad_d/(v - 1)) /
    (ess_all_ad_d/((v - 1) * (b - 1) - 1))

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

  F_b_ad_L <- (bss_ad_d/(b - 1)) /
    (ess_all_ad_d/((v - 1) * (b - 1) - 1))

  F_b_ad_U <- F_b_ad_L +
    (
      ((bss_ad_d/(b - 1)) *
         (ess_all_ad_i/((v - 1) * (b - 1) - 1))) +
        ((bss_ad_i/(b - 1)) *
           (ess_all_ad_d/((v - 1) * (b - 1) - 1))) +
        ((bss_ad_i/(b - 1)) *
           (ess_all_ad_i/((v - 1) * (b - 1) - 1)))
    ) /
    (
      (ess_all_ad_d/((v - 1) * (b - 1) - 1)) *
        (ess_all_ad_d/((v - 1) * (b - 1) - 1))
    )

  FN <- list(
    c(F_g_L, F_g_U),
    c(F_t_ad_L, F_t_ad_U),
    c(F_b_ad_L, F_b_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
  #-------------------------------------------------------

  Ftab5_cov <- qf(0.95, 1, (v-1)*(b-1)-1)
  Ftab1_cov <- qf(0.99, 1, (v-1)*(b-1)-1)
  Ftab001_cov <- qf(0.999, 1, (v-1)*(b-1)-1)

  Ftab5_trt <- qf(0.95, v-1, (v-1)*(b-1)-1)
  Ftab1_trt <- qf(0.99, v-1, (v-1)*(b-1)-1)
  Ftab001_trt <- qf(0.999, v-1, (v-1)*(b-1)-1)

  Ftab5_blk <- qf(0.95, b-1, (v-1)*(b-1)-1)
  Ftab1_blk <- qf(0.99, b-1, (v-1)*(b-1)-1)
  Ftab001_blk <- qf(0.999, b-1, (v-1)*(b-1)-1)

  sig_cov <- ifelse(
    F_g_L > Ftab001_cov,"***",
    ifelse(F_g_L > Ftab1_cov,"**",
           ifelse(F_g_L > Ftab5_cov,"*","NS"))
  )

  sig_trt <- ifelse(
    F_t_ad_L > Ftab001_trt,"***",
    ifelse(F_t_ad_L > Ftab1_trt,"**",
           ifelse(F_t_ad_L > Ftab5_trt,"*","NS"))
  )

  sig_blk <- ifelse(
    F_b_ad_L > Ftab001_blk,"***",
    ifelse(F_b_ad_L > Ftab1_blk,"**",
           ifelse(F_b_ad_L > Ftab5_blk,"*","NS"))
  )

  nancova_table$Significance <- c(
    sig_cov,
    sig_trt,
    sig_blk,
    "",
    ""
  )

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

  dfe <- (v-1)*(b-1)-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

  trtmeans_d <- as.vector(acap_all_ad_d[1:v])
  trtmeans_i <- as.vector(acap_all_ad_i[1:v])

  grand_mean <- mean((Lower_y+Upper_y)/2)

  adjmeans_d <- grand_mean+trtmeans_d
  adjmeans_i <- trtmeans_i

  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
        )
      )

    }

  }

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

  if(verbose){

    message("\nNeutrosophic Analysis of Covariance Table")

    print(nancova_table,row.names=FALSE)

    if(sig_trt!="NS"){

      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.")
      message("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=if(sig_trt!="NS")
        comparison
      else
        NULL,

      LSD=if(sig_trt!="NS")
        c(
          Lower=LSD_l,
          Upper=LSD_u
        )
      else
        NULL

    ))
  }
}



