| 1 |
|
| 2 |
#-------------------------- |
| 3 |
# get G list and H array |
| 4 |
#-------------------------- |
| 5 |
|
| 6 |
get_GH <- function(obj, vcov) { |
| 7 |
|
| 8 |
cluster <- attr(vcov, "cluster") |
| 9 |
M <- attr(vcov, "bread") / attr(vcov, "v_scale") |
| 10 |
|
| 11 |
E_list <- adjust_est_mats(type = attr(vcov, "type"), |
| 12 |
est_mats = attr(vcov, "est_mats"), |
| 13 |
adjustments = attr(vcov, "adjustments")) |
| 14 |
|
| 15 |
target <- attr(vcov, "target") |
| 16 |
inverse_var <- attr(vcov, "inverse_var") |
| 17 |
ignore_FE <- attr(vcov, "ignore_FE") |
| 18 |
|
| 19 |
N <- length(cluster) |
| 20 |
J <- nlevels(cluster) |
| 21 |
|
| 22 |
X <- model_matrix(obj) |
| 23 |
alias <- is.na(coef_CS(obj)) |
| 24 |
if (any(alias)) X <- X[, !alias, drop = FALSE] |
| 25 |
p <- ncol(X) |
| 26 |
|
| 27 |
W_list <- weightMatrix(obj, cluster) |
| 28 |
w_scale <- attr(W_list, "w_scale") |
| 29 |
if (is.null(w_scale)) w_scale <- 1 |
| 30 |
|
| 31 |
S <- augmented_model_matrix(obj, cluster, inverse_var, ignore_FE) |
| 32 |
|
| 33 |
if (is.null(S)) { |
| 34 |
U_list <- matrix_list(X, cluster, "row") |
| 35 |
rm(X, S) |
| 36 |
u <- p |
| 37 |
UW_list <- Map(function(u, w) as.matrix(t(u) %*% w), u = U_list, w = W_list) |
| 38 |
M_U <- w_scale * M |
| 39 |
} else { |
| 40 |
U_list <- matrix_list(cbind(X, S), cluster, "row") |
| 41 |
rm(X, S) |
| 42 |
u <- ncol(U_list[[1]]) |
| 43 |
UW_list <- Map(function(u, w) as.matrix(t(u) %*% w), u = U_list, w = W_list) |
| 44 |
UWU_list <- Map(function(uw, u) uw %*% u, uw = UW_list, u = U_list) |
| 45 |
M_U <- chol2inv(chol(Reduce("+",UWU_list))) |
| 46 |
rm(UWU_list) |
| 47 |
} |
| 48 |
|
| 49 |
M_U_ct <- t(chol(M_U)) |
| 50 |
|
| 51 |
ME_list <- lapply(E_list, function(e) M %*% e) |
| 52 |
|
| 53 |
G_list <- Map(function(me, theta) me %*% t(chol(theta)), me = ME_list, theta = target) |
| 54 |
|
| 55 |
if (inverse_var) { |
| 56 |
H_array <- array(unlist(Map(function(me, u) me %*% u %*% M_U_ct, me = ME_list, u = U_list)), |
| 57 |
dim = c(p, u, J)) |
| 58 |
} else { |
| 59 |
H_array <- array(NA, dim = c(3, p, u, J)) |
| 60 |
MEU_list <- Map(function(me, u) me %*% u, me = ME_list, u = U_list) |
| 61 |
H_array[1,,,] <- unlist(lapply(MEU_list, function(meu) meu %*% M_U_ct)) |
| 62 |
TWU_list <- Map(function(t, w, u) t %*% w %*% u, t = target, w = W_list, u = U_list) |
| 63 |
MEF_list <- Map(function(me, twu) me %*% twu, me = ME_list, twu = TWU_list) |
| 64 |
H_array[2,,,] <- unlist(lapply(MEF_list, function(mef) mef %*% M_U_ct)) |
| 65 |
rm(MEF_list) |
| 66 |
UWTWU_list <- Map(function(uw, twu) uw %*% twu, uw = UW_list, twu = TWU_list) |
| 67 |
Omega_ct <- t(chol(M_U %*% Reduce("+", UWTWU_list) %*% M_U)) |
| 68 |
rm(TWU_list, UWTWU_list) |
| 69 |
H_array[3,,,] <- unlist(lapply(MEU_list, function(meu) meu %*% Omega_ct)) |
| 70 |
rm(MEU_list, Omega_ct) |
| 71 |
} |
| 72 |
|
| 73 |
list(G = G_list, H = H_array) |
| 74 |
} |
| 75 |
|
| 76 |
#-------------------------- |
| 77 |
# get P array |
| 78 |
#-------------------------- |
| 79 |
|
| 80 |
get_P_array <- function(GH, all_terms = FALSE) { |
| 81 |
|
| 82 |
dims <- dim(GH$H) |
| 83 |
|
| 84 |
if (all_terms) { |
| 85 |
|
| 86 |
if (length(dims)==3) { |
| 87 |
P_array <- array(NA, dim = c(dims[1], dims[1], dims[3], dims[3])) |
| 88 |
for (i in 1:dims[1]) for (j in i:dims[1]) { |
| 89 |
if (dims[2] == 1L) { |
| 90 |
tmp <- -tcrossprod(GH$H[i,,], GH$H[j,,]) |
| 91 |
} else { |
| 92 |
tmp <- -crossprod(GH$H[i,,], GH$H[j,,]) |
| 93 |
} |
| 94 |
diag(tmp) <- diag(tmp) + sapply(GH$G, function(x) sum(x[i,] * x[j,])) |
| 95 |
P_array[i,j,,] <- tmp |
| 96 |
if (j > i) P_array[j,i,,] <- t(tmp) |
| 97 |
} |
| 98 |
} else { |
| 99 |
P_array <- array(NA, dim = c(dims[2], dims[2], dims[4], dims[4])) |
| 100 |
for (i in 1:dims[2]) for (j in i:dims[2]) { |
| 101 |
tmp <- crossprod(GH$H[3,i,,], GH$H[3,j,,]) - |
| 102 |
crossprod(GH$H[1,i,,], GH$H[2,j,,]) - |
| 103 |
crossprod(GH$H[2,i,,], GH$H[1,j,,]) |
| 104 |
diag(tmp) <- diag(tmp) + sapply(GH$G, function(x) sum(x[i,] * x[j,])) |
| 105 |
P_array[i,j,,] <- tmp |
| 106 |
if (j > i) P_array[j,i,,] <- t(tmp) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
} else { |
| 111 |
|
| 112 |
if (length(dims)==3) { |
| 113 |
P_array <- array(-apply(GH$H, 1, crossprod), dim = c(dims[3], dims[3], dims[1])) |
| 114 |
P_diag <- matrix(sapply(GH$G, function(x) rowSums(x^2)), nrow = dims[1], ncol = dims[3]) |
| 115 |
for (i in 1:dims[1]) diag(P_array[,,i]) <- diag(P_array[,,i]) + P_diag[i,] |
| 116 |
} else { |
| 117 |
if (dims[3] == 1L) { |
| 118 |
P_array <- array(apply(GH$H, 2, function(h) { |
| 119 |
uf <- tcrossprod(h[1,1,], h[2,1,]) |
| 120 |
tcrossprod(h[3,1,]) - uf - t(uf) |
| 121 |
}), dim = c(dims[4], dims[4], dims[2])) |
| 122 |
} else { |
| 123 |
P_array <- array(apply(GH$H, 2, function(h) { |
| 124 |
uf <- crossprod(h[1,,], h[2,,]) |
| 125 |
crossprod(h[3,,]) - uf - t(uf) |
| 126 |
}), dim = c(dims[4], dims[4], dims[2])) |
| 127 |
} |
| 128 |
P_diag <- matrix(sapply(GH$G, function(x) rowSums(x^2)), nrow = dims[2], ncol = dims[4]) |
| 129 |
for (i in 1:dims[2]) diag(P_array[,,i]) <- diag(P_array[,,i]) + P_diag[i,] |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
P_array |
| 135 |
} |
| 136 |
|
| 137 |
#-------------------------- |
| 138 |
# get S array |
| 139 |
#-------------------------- |
| 140 |
|
| 141 |
Sj <- function(M, e, u, tc, cl, cluster, MUWTheta_cholT) { |
| 142 |
s <- -u %*% MUWTheta_cholT |
| 143 |
s[,cluster==cl] <- tc + s[,cluster==cl] |
| 144 |
M %*% e %*% s |
| 145 |
} |
| 146 |
|
| 147 |
get_S_array <- function(obj, vcov) { |
| 148 |
|
| 149 |
cluster <- attr(vcov, "cluster") |
| 150 |
M <- attr(vcov, "bread") / attr(vcov, "v_scale") |
| 151 |
|
| 152 |
E_list <- adjust_est_mats(type = attr(vcov, "type"), |
| 153 |
est_mats = attr(vcov, "est_mats"), |
| 154 |
adjustments = attr(vcov, "adjustments")) |
| 155 |
|
| 156 |
target <- attr(vcov, "target") |
| 157 |
inverse_var <- attr(vcov, "inverse_var") |
| 158 |
ignore_FE <- attr(vcov, "ignore_FE") |
| 159 |
|
| 160 |
N <- length(cluster) |
| 161 |
J <- nlevels(cluster) |
| 162 |
|
| 163 |
X <- model_matrix(obj) |
| 164 |
alias <- is.na(coef_CS(obj)) |
| 165 |
if (any(alias)) X <- X[, !alias, drop = FALSE] |
| 166 |
p <- ncol(X) |
| 167 |
|
| 168 |
S <- augmented_model_matrix(obj, cluster, inverse_var, ignore_FE) |
| 169 |
|
| 170 |
if (is.null(S)) { |
| 171 |
U <- X |
| 172 |
} else { |
| 173 |
U <- cbind(X, S) |
| 174 |
} |
| 175 |
|
| 176 |
U_list <- matrix_list(U, cluster, "row") |
| 177 |
|
| 178 |
W_list <- weightMatrix(obj, cluster) |
| 179 |
|
| 180 |
UW_list <- Map(function(u, w) as.matrix(t(u) %*% w), u = U_list, w = W_list) |
| 181 |
UWU_list <- Map(function(uw, u) uw %*% u, uw = UW_list, u = U_list) |
| 182 |
M_U <- chol2inv(chol(Reduce("+",UWU_list))) |
| 183 |
|
| 184 |
Theta_cholT <- lapply(target, function(x) t(chol(x))) |
| 185 |
UWThetaC_list <- Map(function(uw, tc) uw %*% tc, uw = UW_list, tc = Theta_cholT) |
| 186 |
MUWTheta_cholT <- M_U %*% (matrix(unlist(UWThetaC_list), ncol(U), N)[,order(order(cluster))]) |
| 187 |
|
| 188 |
S_list <- mapply(Sj, e = E_list, u = U_list, tc = Theta_cholT, cl = levels(cluster), |
| 189 |
MoreArgs = list(M = M, cluster=cluster, MUWTheta_cholT=MUWTheta_cholT), |
| 190 |
SIMPLIFY = FALSE) |
| 191 |
|
| 192 |
array(unlist(S_list), dim = c(p, N, J)) |
| 193 |
} |
| 194 |
|