R/hw5_2_hr.R

Defines functions hw5_2_hr

Documented in hw5_2_hr

#' Household regression and check hw5 question 2
#' @export
#' @param X matrix variable
#' @param y vector variable


hw5_2_hr <- function(X, y) {
  p <- ncol(X)
  n <- nrow(X)
  Xy <- cbind(X, y)
  u <- as.list(1:p)
  # initial
  temp.hh <- hw5_2_hh(X[,1])
  U <- temp.hh$U
  for (i in 2:p) {
    x <- (U %*% X)[i:n,i]
    temp.hh <- hw5_2_hh(x)
    temp.U <- temp.hh$U
    temp.I <- diag(i-1)
    temp.01 <- matrix(0, i-1, n - i +1)
    temp.02 <- matrix(0, n - i + 1, i-1)
    U <- rbind(cbind(temp.I, temp.01), cbind(temp.02, temp.U)) %*% U
  }
  temp <- U %*% Xy
  R <- temp[1:p,1:p]
  z1 <- temp[1:p,p+1]
  z2 <- temp[-(1:p),p+1]
  obj <- list(U = U, R = R, z1 = z1, z2 = z2)
}
exp500/MAT8054 documentation built on Dec. 20, 2021, 7:39 a.m.