new_GARCHX <- function(eps, X, order = c(1, 1), delta = 2, covariates = "all", optim.method = "NR"){
  
  # Fit the model and returns all the p-values
  
  # Declaring initial values for paramters
  # d = number of exogenous covariates
  if (is.null(X)) {
    d = 0
    covariates = NULL
  } else
  {
    d <- ncol(X)
  }
  r <- 10
  
  
  
  if (!identical(covariates,"all")) {
    if ((covariates[1] == 0) || is.null(covariates)) {
      covariates = NULL
      #return(ARCH_internal(eps=eps, p=order[1], delta=delta))
    } else {
      X <- as.matrix(X[,covariates])
      d = length(covariates)
    }
  } else { #covariates = "all" so change to a vector
    covariates <- 1:ncol(X)
  }
  
  
  # Create object of class GARCHX
  
  obj <- NULL
  obj$covariates <- covariates
  obj$eps <- eps
  obj$selectedX <- X
  obj$X <- X
  obj$N <- length(eps)
  obj$order <- order
  obj$optim.method <- optim.method
  obj$d <- d
  obj$p <- order[1]
  obj$q <- order[2]
  obj$delta <- delta
  obj$r <- r
  obj$alpha.level <- NA
  obj$adjust.method <- 'none'
  obj$omega <- NA     # Initialize omega, alpha, beta, and gamma coefficients as NA
  obj$alpha <- NA
  obj$beta <- NA
  obj$gamma <- NA
  obj$p.values <- NA
  obj$covariate_p.values <- NA
  obj$Likelihood <- NA
  obj$log_QL <- NA
  obj$call <- match.call()   # Assign class to object
  class(obj) <- "GARCHX"
  
  
  # Extract p and q from order
  p <- order[1]
  q <- order[2]
  
  
  # Estimate coefficients
  estimated_values <- GARCHX_estimate(obj, optim.method)
  
  # Extract all estimated coefficients
  estimated_coefs <- estimated_values$coef
  
  # Adjust and assign p-values
  #obj$p.values <- p.adjust(estimated_values$pval[(p+q+2):(1+p+q+d)], method = obj$adjust.method)
  
  # Assign coefficients
  obj$omega <- estimated_coefs[1]
  obj$alpha <- estimated_coefs[2:(p+1)]
  if (q > 0)
    obj$beta <- estimated_coefs[(p+2):(p+q+1)]
  if (d > 0)
    obj$gamma <- estimated_coefs[(p+q+2):(1+p+q+d)]
  
  #if (d > 0)
  #{
  #  obj$selectedX_index <- 1:d # there is no covariate selection, this function is only estimation for all
    obj$p.values <- estimated_values$pval
  #}
  # if covariates are present, set covariate p-values
  if (d > 0) {
    obj$covariate_p.values <- estimated_values$pval[(p + q + 2):(p + q + d + 1)]
  } else {
    obj$covariate_p.values <- NULL ## numeric(0)
  }
  
  # 1/30/26 get covariate p-values
  #if (is.null(covariates)) {
  #  obj$covariate_p_values = NULL
  #} else {
  #  n_covariates <- length(covariates)
  #  obj$covariate_p_values <- obj$p.values[(length(obj$p.values) - n_covariates + 1):(length(obj$p.values))]
  #}
  
  # Assign estimated likelihood
  obj$Likelihood <- estimated_values$minimum
  obj$log_QL <- -1 * obj$Likelihood
  
  # Return obj of GARCHX
  return(obj)
} # End GARCHX.default