R/LVRMeansTSIMD.R

Defines functions LVRMeansTSIMD

LVRMeansTSIMD <- function(X_stand, y_disc, n_slices){
  # Computes the left vs right means and their difference.
  # Returns the V.hat_LVR matix 
  n_predictors <- ncol(X_stand)
  y_unit <- 1:n_slices
  xgy_left <- matrix(0, n_slices-1, n_predictors)
  
  # Compute left means for the slices.
  for(i in 1:(n_slices-1)){
    xgy_left[i,] <- apply(X_stand[y_disc <= y_unit[i],], 2, mean)
  }
  
  # Similar as above, but from the right
  xgy_right = matrix(0, n_slices-1, n_predictors)
  for(i in 1:(n_slices-1)){
    xgy_right[i,] = apply(X_stand[y_disc > y_unit[i],], 2, mean)
  }
  
  # Returns M.hat.(q_r)_LVR^z
  xgy <- xgy_right - xgy_left
  
  # Predefining output matrix
  out <- matrix(0, n_predictors, n_predictors)
  
  # Compute the V.hat_LVR
  for(i in 1:(n_slices-1)){
    out <- out + 1*xgy[i,] %*% t(xgy[i,])
  }
  
  return(out)
  
}
hectorhaffenden/TSIMD documentation built on Nov. 4, 2019, 1:30 p.m.