R/get_prof_sigma2.R

Defines functions get_prof_sigma2

#' Get overall variance if profiling used
#'
#' @param spcov_params_val A \code{spcov_params} object
#' @param dist_matrix A distance matrix
#' @param estmethod The estimation method
#' @param X A model matrix
#' @param y A response vector
#' @param n The sample size
#' @param p The number of fixed effects
#' @param observed_index A vector indicating observed values
#'
#' @return The overall variance
#'
#' @noRd
get_prof_sigma2 <- function(spcov_params_val, data_object, estmethod,
                            dist_matrix_list, randcov_params_val = NULL) {
  # given the other (correlation-scale) covariance parameters, the overall
  # variance sigma^2 has a closed-form maximizer (l2, a quadratic form in the
  # whitened residuals, scaled by the degrees of freedom) -- this lets
  # optimization profile sigma^2 out entirely rather than searching over it
  gll_prods <- gloglik_products(
    spcov_params_val, data_object, estmethod,
    dist_matrix_list, randcov_params_val
  )
  ## finding sigma2 estimate (as we are have a spcov_profiled likelihood)
  if (estmethod == "reml") {
    sigma2 <- gll_prods$l2 / (data_object$n - data_object$p)
  } else if (estmethod == "ml") {
    sigma2 <- gll_prods$l2 / data_object$n
  }
  as.numeric(sigma2)
}

Try the spmodel package in your browser

Any scripts or data that you put into this service are public.

spmodel documentation built on Sept. 11, 2026, 1:07 a.m.