R/step-function.R

Defines functions step_hessian step_score step_loglik

#-------------------------------------------------------------------------------
# Log likelihood function


step_loglik <- function(
    theta,                                      # full parameter vector
    yi,                                         # outcome vector
    sei,                                        # sampling standard errors
    pi = pnorm(Hsgn * yi / sei, lower.tail = FALSE),   # one-sided p-values
    ai = NULL,                                  # analytic weight 
    beta = NULL,                                # mean parameter coefficients
    gamma = NULL,                               # variance component coefficients
    zeta0 = NULL,                               # selection model coefficients
    zeta = NULL,                                # selection model coefficients
    steps = .025,                               # steps / cut-points
    X = NULL,                                   # mean parameter design matrix
    U = NULL,                                   # variance component design matrix
    Z0 = NULL,                                  # selection model design matrix for highest step
    Z = NULL,                                   # selection model design matrices for each cut-point
    Hsgn = 1L,                           # valence of alternative hypothesis used to compute p-values
    priors = NULL                               # selmodel_prior object to specify priors
) {
  
  params <- parse_step_params(
    theta = theta,
    yi = yi,
    sei = sei,
    pi = pi,
    beta = beta,
    gamma = gamma,
    zeta0 = zeta0,
    zeta = zeta,
    steps = steps,
    X = X,
    U = U,
    Z0 = Z0,
    Z = Z,
    Hsgn = Hsgn,
    calc_Ai = TRUE
  )
  
  # likelihood contributions
  log_lik_i <- log(params$weight_vec) - (yi - params$mu)^2 / (2 * params$eta) - log(params$eta) / 2 - log(params$Ai)
  
  # weighted log likelihood (Eq. 51)
  log_lik <- if (is.null(ai)) sum(log_lik_i) else sum(ai * log_lik_i)
  
  # priors
  if (is.null(priors)) {
    
    return(log_lik) 
    
  } else {
    
    log_prior <- priors$log_prior(beta = params$beta, gamma = params$gamma, zeta = c(params$zeta0, params$zeta))  
    return(log_lik + log_prior)
    
  }
  
  
}

#-------------------------------------------------------------------------------
# Score function

step_score <- function(
    theta,                                      # full parameter vector
    yi,                                         # outcome vector
    sei,                                        # sampling standard errors
    pi = pnorm(Hsgn * yi / sei, lower.tail = FALSE),   # one-sided p-values  
    ai = NULL,                                  # analytic weight
    beta = NULL,                                # mean parameter coefficients
    gamma = NULL,                               # variance component coefficients
    zeta0 = NULL,                               # selection model coefficients
    zeta = NULL,                                # selection model coefficients
    steps = .025,                               # steps / cut-points
    X = NULL,                                   # mean parameter design matrix   
    U = NULL,                                   # variance component design matrix
    Z0 = NULL,                                  # selection model design matrix for highest step
    Z = NULL,                                   # selection model design matrices for each cut-point
    Hsgn = 1L,                           # valence of alternative hypothesis used to compute p-values
    priors = NULL,                              # selmodel_prior object to specify priors
    contributions = FALSE                       # whether to return matrix of score contributions
) {
  
  params <- parse_step_params(
    theta = theta,
    yi = yi,
    sei = sei,
    pi = pi,
    beta = beta,
    gamma = gamma,
    zeta0 = zeta0,
    zeta = zeta,
    steps = steps,
    X = X,
    U = U,
    Z0 = Z0,
    Z = Z,
    Hsgn = Hsgn,
    calc_Ai = TRUE
  )
  
  k <- params$k
  H <- params$H
  
  #----------------------------------------------------------
  # Derivatives of A w/r/t mu, eta, lambda_h (Eq. 21-23)
  
  dB <- dB_dmu_eta(k = k, H = H, c_mat = params$c_mat, Hsgn = Hsgn)
  dA_dmu <- rowSums(dB$dmu * params$lambda_full) / sqrt(params$eta)
  dA_deta <- rowSums(dB$deta * params$lambda_full) / (2 * params$eta)
  
  dA_dlambda <- params$B_mat

  # Derivatives of weights w/r/t lambda_h (Eq. 20)
  dw_dlambda <- model.matrix(yi ~ 0 + params$cats)
  
  # Derivative of g w/r/t (z_hij * zeta_h)
  # (For VHSM this is the same as g(z_hij * zeta_h) = lambda_h.)
  
  #----------------------------------------------------------
  # score contributions (Eq. 12, just for observation ij)
  zeta_i <- (yi - params$mu) / params$eta
  
  # score contribution for mu (Eq. 15) should be x_dim X k
  S_beta_ij <- zeta_i  - dA_dmu / params$Ai
  if (!is.null(X)) S_beta_ij <- X * S_beta_ij

  # score contribution for gamma (Eq. 16) should be u_dim X k
  S_gamma_ij <- params$tausq * (zeta_i^2 / 2 - 1 / (2 * params$eta)  - dA_deta / params$Ai)
  if (!is.null(U)) S_gamma_ij <- U * S_gamma_ij
  
  # score contribution for lambda_h (Eq. 17) should be z_dim[h] X k
  scores <- calculate_S_zeta_ij(
    B_mat = params$B_mat,
    Ai = params$Ai,
    weight_vec = params$weight_vec,
    cats = params$cats,
    Z0 = Z0,
    Z = Z,
    lambda0 = params$lambda0,
    lambda = params$lambda
  )
  
  S_zeta_ij <- scores$S_zeta_ij
  S_zeta0_ij <- if (is.null(Z0)) NULL else scores$S_zeta0_ij
  
  score_contributions <- get_score_contributions(
    S_beta_ij = S_beta_ij,
    S_gamma_ij = S_gamma_ij,
    S_zeta_ij = S_zeta_ij,
    S_zeta0_ij = S_zeta0_ij,
    ai = ai,
    contributions = contributions
  )
  
  # handle prior contributions
  if (is.null(priors)) {
    
    return(score_contributions)
    
  } else {
    
    prior_score <- priors$score_prior(beta = params$beta, gamma = params$gamma, zeta = c(params$zeta0, params$zeta))
    
    if (contributions) {
      
      # prior_wt to allocate total prior score across observations
      
      if (is.null(ai)) {
        prior_wt <- rep(1 / k, times = k)
      } else {
        prior_wt <- ai / sum(ai)
      }
      
      score_contributions <- score_contributions + outer(prior_wt, prior_score)
      
      return(score_contributions)
      
    } else {
      
      return(score_contributions + prior_score)
      
    }
    
  }
  
  
}

#-------------------------------------------------------------------------------
# Hessian function

step_hessian <- function(
    theta,                                      # full parameter vector
    yi,                                         # outcome vector
    sei,                                        # sampling standard errors
    pi = pnorm(Hsgn * yi / sei, lower.tail = FALSE),   # one-sided p-values  
    ai = NULL,                                  # analytic weight
    beta = NULL,                                # mean parameter coefficients
    gamma = NULL,                               # variance component coefficients
    zeta0 = NULL,                               # selection model coefficients
    zeta = NULL,                                # selection model coefficients
    steps = .025,                               # steps / cut-points
    X = NULL,                                   # mean parameter design matrix
    U = NULL,                                   # variance component design matrix
    Z0 = NULL,                                  # selection model design matrix for highest step
    Z = NULL,                                   # selection model design matrices for each cut-point
    Hsgn = 1L,                           # valence of alternative hypothesis used to compute p-values
    priors = NULL                               # selmodel_prior object to specify priors
) {
  
  params <- parse_step_params(
    theta = theta,
    yi = yi,
    sei = sei,
    pi = pi,
    beta = beta,
    gamma = gamma,
    zeta0 = zeta0,
    zeta = zeta,
    steps = steps,
    X = X,
    U = U,
    Z0 = Z0,
    Z = Z,
    Hsgn = Hsgn,
    calc_Ai = TRUE
  )
  
  k <- params$k
  H <- params$H

  # analytic weights
  if (is.null(ai)) ai <- rep(1, k)
  
  d1_mat <- cbind(rep(0,k), dnorm(params$c_mat), rep(0,k))
  d2_mat <- cbind(rep(0,k), params$c_mat * d1_mat[,2:H,drop=FALSE], rep(0,k))
  d3_mat <- cbind(rep(0,k), (params$c_mat^2 - 1) * d1_mat[,2:H,drop=FALSE], rep(0,k))
  d4_mat <- cbind(rep(0,k), (params$c_mat^3 - 3 * params$c_mat) * d1_mat[,2:H,drop=FALSE], rep(0,k))
  
  
  # First derivatives of A w/r/t mu, eta, lambda_h (Eq. 21-23)
  
  dB_dmu <- Hsgn * (d1_mat[,2:(H+1L),drop=FALSE] - d1_mat[,1:H,drop=FALSE])
  dA_dmu <- rowSums(dB_dmu * params$lambda_full) / sqrt(params$eta)
  
  dB_deta <- d2_mat[,2:(H+1L),drop=FALSE] - d2_mat[,1:H,drop=FALSE]
  dA_deta <- rowSums(dB_deta * params$lambda_full) / (2 * params$eta)
  
  dA_dlambda <- if (is.null(Z0)) {
    params$B_mat[,-1,drop=FALSE]
  } else {
    params$B_mat
  }
  
  # Second derivatives of A w/r/t mu, eta, lambda_h (Eq. 25-30)
  
  # dA_dmu_dmu is k * 1
  dA_dmu_dmu <- rowSums(dB_deta * params$lambda_full) / params$eta
  
  # dA_dmu_deta is k * 1
  dB_d3 <- Hsgn * (d3_mat[,2:(H+1L)] - d3_mat[,1:H])
  dA_dmu_deta <- rowSums(dB_d3 * params$lambda_full) / (2 * params$eta^1.5)
  
  # dA_deta_deta is k * 1
  dB_d4 <- d4_mat[,2:(H+1L)] - d4_mat[,1:H]
  dA_deta_deta <- rowSums(dB_d4 * params$lambda_full) / (4 * params$eta^2)
  
  # dA_dmu_dlambda is k * (H - 1) or k * H if !is.null(Z0)
  # dA_deta_dlambda is k * (H - 1) or k * H if  !is.null(Z0)
  
  if (is.null(Z0)) {
    dA_dmu_dlambda <- dB_dmu[,-1,drop=FALSE] / sqrt(params$eta)
    dA_deta_dlambda <- dB_deta[,-1,drop=FALSE] / (2 * params$eta)
    
    # First derivatives of weights w/r/t lambda_h (Eq. 20)
    dw_dlambda <- model.matrix(~ 0 + params$cats)[,-1,drop=FALSE]
    
  } else {
    dA_dmu_dlambda <- dB_dmu / sqrt(params$eta)
    dA_deta_dlambda <- dB_deta / (2 * params$eta)
    
    # First derivatives of weights w/r/t lambda_h (Eq. 20)
    dw_dlambda <- model.matrix(~ 0 + params$cats)
    
  }
  
  # dA_dlambda_dlambda is all zeros (Eq. 30)
  
  # Second derivatives of weights are all zero (Eq. 24)
  
  
  # First and second derivative of g w/r/t (z_hij * zeta_h)
  # (For VHSM this is the same as g(z_hij * zeta_h) = lambda_h.)
  
  #----------------------------------------------------------
  # calculate weighted Hessian (Eq. 53 on p. 16)
  # See p. 10 for component matrices
  # Calculate the components by first calculating the pieces inside parentheses,
  # then multiplying by the analytic weights (ai),
  # then taking cross-product with matrix terms (X, U, Z).
  
  # MJ: do i need to multiply by ai somewhere here?
  
  #----------------------------------------------------------
  
  zeta_i <- (yi - params$mu) / params$eta
  
  # H_beta_beta is x_dim * x_dim
  H_beta_beta_right <- dA_dmu^2 / params$Ai^2 - dA_dmu_dmu / params$Ai - 1/params$eta 
  H_beta_beta <- matrix_diag_crossprod(d = ai * H_beta_beta_right, A = X, B = X)

  # H_beta_gamma is x_dim * u_dim
  H_beta_gamma_right <- params$tausq * (dA_dmu * dA_deta / params$Ai^2 - dA_dmu_deta / params$Ai - (yi - params$mu) / params$eta^2)
  H_beta_gamma <- matrix_diag_crossprod(d = ai * H_beta_gamma_right, A = X, B = U)

  # H_gamma_gamma is u_dim * u_dim
  H_gamma_gamma_right <- params$tausq * (zeta_i^2 / 2 - 1 / (2 * params$eta) - dA_deta / params$Ai) + 
                          params$tausq^2 * (dA_deta^2 / params$Ai^2 - dA_deta_deta / params$Ai -  zeta_i^2 / params$eta + 1 / (2 * params$eta^2))
  H_gamma_gamma <- matrix_diag_crossprod(d = ai * H_gamma_gamma_right, A = U, B = U)
  
  # H_beta_zeta is x_dim * sum(z_dim) or x_dim * (z0_dim + sum(z_dim)) if !is.null(Z0)
  # H_gamma_zeta is u_dim * sum(z_dim) or x_dim * (z0_dim + sum(z_dim)) if !is.null(Z0)
  
  H_zeta <- calculate_H_zeta(B_mat = params$B_mat,
                               Ai = params$Ai,
                               weight_vec = params$weight_vec,
                               cats = params$cats,
                               eta = params$eta,
                               c_mat = params$c_mat,
                               k= params$k,
                               H = params$H,
                               tausq = params$tausq,
                               X = X,
                               U = U,
                               Z0 = Z0,
                               Z = Z,
                               ai = ai,
                               z0_dim = params$z0_dim,
                               z_dim = params$z_dim,
                               lambda0 = params$lambda0,
                               lambda = params$lambda,
                               lambda_full = params$lambda_full,
                               dB = list(dmu = dB_dmu, deta = dB_deta))    
  
  H_zeta_ <- cbind(t(H_zeta$H_beta_zeta), t(H_zeta$H_gamma_zeta), H_zeta$H_zeta_zeta)

   
  # Put the whole Hessian together

  H_matrix <- rbind(
    cbind(H_beta_beta, H_beta_gamma, H_zeta$H_beta_zeta),
    cbind(t(H_beta_gamma), H_gamma_gamma, H_zeta$H_gamma_zeta),
    H_zeta_
  )
  colnames(H_matrix) <- rownames(H_matrix) <- params$H_names
  
  # Handle priors
  
  if (is.null(priors)) {
  
    return(H_matrix)
    
  } else {
    hessian_prior <- priors$hessian_prior(beta = params$beta, gamma = params$gamma, zeta = c(params$zeta0, params$zeta))
    return(H_matrix + hessian_prior)
  }
  
  
}

Try the metaselection package in your browser

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

metaselection documentation built on Aug. 31, 2026, 5:08 p.m.