Debian Codegraph @

R/CR-adjustments.R

raw
1 #---------------------------------------------
2 # Auxilliary functions for CR* functions
3 #---------------------------------------------
4
5 IH_jj_list <- function(M, X_list, XW_list) {
6 Map(function(x, xw) diag(nrow = nrow(x)) - x %*% M %*% xw,
7 x = X_list, xw = XW_list)
8 }
9
10 #---------------------------------------------
11 # Estimating function adjustments
12 #---------------------------------------------
13
14 CR0 <- function(J) NULL
15
16 CR1 <- function(J) sqrt(J / (J - 1))
17
18 CR1p <- function(J, p) sqrt(J / (J - p))
19
20 CR1S <- function(J, N, p) sqrt(J * (N - 1) / ((J - 1) * (N - p)))
21
22 CR2 <- function(M_U, U_list, UW_list, Theta_list, inverse_var = FALSE) {
23
24 Theta_chol <- lapply(Theta_list, chol)
25
26 if (inverse_var) {
27 IH_jj <- IH_jj_list(M_U, U_list, UW_list)
28 G_list <- Map(function(a,b,ih) as.matrix(a %*% ih %*% b %*% t(a)),
29 a = Theta_chol, b = Theta_list, ih = IH_jj)
30 } else {
31 H_jj <- Map(function(u, uw) u %*% M_U %*% uw,
32 u = U_list, uw = UW_list)
33 uwTwu <- Map(function(uw, th) uw %*% th %*% t(uw),
34 uw = UW_list, th = Theta_list)
35 MUWTWUM <- M_U %*% Reduce("+", uwTwu) %*% M_U
36 G_list <- Map(function(thet, h, u, v)
37 as.matrix(v %*% (thet - h %*% thet - thet %*% t(h) + u %*% MUWTWUM %*% t(u)) %*% t(v)),
38 thet = Theta_list, h = H_jj, u = U_list, v = Theta_chol)
39 }
40
41 Map(function(v, g) as.matrix(t(v) %*% matrix_power(g, -1/2) %*% v),
42 v = Theta_chol, g = G_list)
43 }
44
45 CR3 <- function(X_list, XW_list) {
46 XWX_list <- Map(function(xw, x) xw %*% x, xw = XW_list, x = X_list)
47 M <- chol2inv(chol(Reduce("+", XWX_list)))
48 IH_jj <- IH_jj_list(M, X_list, XW_list)
49 lapply(IH_jj, solve)
50 }
51
52 CR4 <- function(M_U, U_list, UW_list, X_list, XW_list, Theta_list, inverse_var = FALSE) {
53
54 if (inverse_var) {
55 F_list <- Map(function(xw, x) xw %*% x, xw = XW_list, x = X_list)
56 UWX_list <- Map(function(uw, x) uw %*% x, uw = UW_list, x = X_list)
57 F_chol <- lapply(F_list, chol_psd)
58 G_list <- Map(function(fc, fm, uwx) fc %*% (fm - t(uwx) %*% M_U %*% uwx) %*% t(fc),
59 fc = F_chol, fm = F_list, uwx = UWX_list)
60 } else {
61 F_list <- Map(function(xw, theta) xw %*% theta %*% t(xw),
62 xw = XW_list, theta = Theta_list)
63 F_chol <- lapply(F_list, chol_psd)
64 UWX_list <- Map(function(uw, x) uw %*% x, uw = UW_list, x = X_list)
65 UWTWX_list <- Map(function(uw, xw, theta) uw %*% theta %*% t(xw), uw = UW_list, xw = XW_list, theta = Theta_list)
66 UWTWU_list <- Map(function(uw, theta) uw %*% theta %*% t(uw), uw = UW_list, theta = Theta_list)
67 MUWTWUM <- M_U %*% Reduce("+", UWTWU_list) %*% M_U
68 G_list <- Map(function(fc, fm, uwx, uwtwx)
69 as.matrix(fc %*% (fm - t(uwx) %*% M_U %*% uwtwx - t(uwtwx) %*% M_U %*% uwx + t(uwx) %*% MUWTWUM %*% uwx) %*% t(fc)),
70 fc = F_chol, fm = F_list, uwx = UWX_list, uwtwx = UWTWX_list)
71 }
72
73 Map(function(fc, g) as.matrix(t(fc) %*% matrix_power(g, -1/2) %*% fc),
74 fc = F_chol, g = G_list)
75 }