
R version 4.5.1 (2025-06-13 ucrt) -- "Great Square Root"
Copyright (C) 2025 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ##  NOTE: This code pertains to panels R15.1 - R15.4
> 
> ###################################################
> ### code chunk Chap15init
> ###################################################
> options(width=65, digits=5,show.signif.stars = FALSE)   
> date()
[1] "Mon Aug  4 21:12:35 2025"
> packageVersion("Matrix")
[1] '1.7.1'
> sessionInfo()
R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
[1] compiler_4.5.1
> SeedValue <- 17761
> set.seed(SeedValue)
> 
> ###################################################
> ### code chunk: R15.1
> ###################################################
> n1 <- 2                 # Number of levels for the factor g1
> n2 <- 3                 # Number of levels for the factor g2
> i <- gl(n1, n2)         # i 
> j <- gl(n2, 1, n1*n2)   # j
> b1x <- rnorm(n1, 0, 1)  # b_i
> b2x <- rnorm(n2, 0, 2)  # b_j
> dt0 <- data.frame(i, j)
> (dtc <- 
+    within(dt0,
+           {             # g1 and g2 are crossed
+            eps <- rnorm(nrow(dt0), 0, 0.2)
+            b1 <- b1x[i]
+            b2 <- b2x[j]
+            y <- 10 + b1 + b2 + eps
+            g2 <- factor(j, labels = letters[1:n2])
+            g1 <- factor(LETTERS[i])
+            }))
  i j g1 g2       y        b2      b1       eps
1 1 1  A  a 10.1444 -0.055214 0.13972  0.059933
2 1 2  A  b 12.2306  2.012640 0.13972  0.078235
3 1 3  A  c  9.3669 -0.601585 0.13972 -0.171216
4 2 1  B  a 11.2304 -0.055214 1.25311  0.032463
5 2 2  B  b 13.1089  2.012640 1.25311 -0.156847
6 2 3  B  c 10.8498 -0.601585 1.25311  0.198232
> 
> 
> ###################################################
> ### code chunk: R15.2
> ###################################################
> Zg1 <- model.matrix(~ 0 + g1, data = dtc)  # Z_1 for g1
> Zg2 <- model.matrix(~ 0 + g2, data = dtc)  # Z_2 for g2
> Z0 <- cbind(Zg1, Zg2)                      # Z for g1 and g2
> A0 <- t(Z0)                                # A = Z' 
> A0c <- tcrossprod(A0)                      # A*A' 
> Dg <- diag(nrow(A0))
> (A0q <- A0c + Dg)                          # A*A' + I
    g1A g1B g2a g2b g2c
g1A   4   0   1   1   1
g1B   0   4   1   1   1
g2a   1   1   3   0   0
g2b   1   1   0   3   0
g2c   1   1   0   0   3
> 
> 
> ###################################################
> ### code chunk: R15.3a
> ###################################################
> L0 <- t(chol(A0q))           # L such that L*L' = A*A' + I  
> sum(L0 != 0.0)               # Count of non-zero elements 
[1] 14
> max(abs(L0 %*% t(L0)- A0q))  # Verify L*L' = A*A' + I  
[1] 4.4409e-16
> 
> 
> ###################################################
> ### code chunk: R15.3b
> ###################################################
> pvec <- c(3, 4, 5, 1, 2)     # Permutation vector
> A1   <- A0[pvec, ]           # Rows permuted in A 
> A1c  <- tcrossprod(A1)       
> (A1q <- A1c + Dg)            # A*A' + I (permuted)
    g2a g2b g2c g1A g1B
g2a   3   0   0   1   1
g2b   0   3   0   1   1
g2c   0   0   3   1   1
g1A   1   1   1   4   0
g1B   1   1   1   0   4
> 
> 
> ###################################################
> ### code chunk: R15.3b  (continued)
> ###################################################
> A1q. <- A0q[pvec, pvec]      # Cols and rows permuted in A * A' 
> identical(A1q, A1q.)
[1] TRUE
> L1   <-  t(chol(A1q.))       # L*L' = A*A' + I (permuted) 
> sum(L1 != 0.0)               # Count of non-zero elements 
[1] 12
> 
> 
> 
> 
> ###################################################
> ### code chunk: R15.4a  
> ###################################################
> library(Matrix)
> A0 <- as(A0, "dgCMatrix")           # A0 matrix coerced to sparse
> A0c <- tcrossprod(A0)                   # A * A' 
> L0 <- Cholesky(A0c, perm = FALSE, Imult = 1, LDL = FALSE)
> nnzero(L0. <- as(L0, "sparseMatrix"))   # Coerced to verify 
[1] 14
> Dg <- Diagonal(nrow(A0))
> (A0q   <- A0c + Dg)
5 x 5 sparse Matrix of class "dsCMatrix"
    g1A g1B g2a g2b g2c
g1A   4   .   1   1   1
g1B   .   4   1   1   1
g2a   1   1   3   .   .
g2b   1   1   .   3   .
g2c   1   1   .   .   3
> max(abs(L0. %*% t(L0.) - A0q))          # L*L' = A*A' + I 
[1] 4.4409e-16
> 
> 
> ###################################################
> ### code chunk: R15.4b  
> ###################################################
> pvec <-  c(3, 4, 5, 1, 2)               # Permutation vector
> P1   <-  as(pvec, "pMatrix")            # Permutation matrix
> A1c  <-  P1 %*% A0c %*% t(P1)
> L1   <-  Cholesky(A1c, perm = FALSE, Imult = 1, LDL = FALSE)
> nnzero(as(L1, "sparseMatrix"))
[1] 12
> 
> 
> ###################################################
> ### code chunk: R15.4c 
> ###################################################
> L2 <-  Cholesky(A0c, perm=TRUE, Imult =1, LDL = FALSE)
> nnzero(as(L2, "sparseMatrix"))
[1] 13
> slot(L2,"perm") + 1L                    # Permutation
[1] 5 1 3 4 2
> 
> ### SessionInfo 
> sessionInfo()            # before detaching package Matrix
R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] Matrix_1.7-1

loaded via a namespace (and not attached):
[1] compiler_4.5.1 grid_4.5.1     lattice_0.22-6
> detach(package:Matrix)
> 
> proc.time()
   user  system elapsed 
   0.59    0.12    1.01 
