| 1 |
library(sfsmisc) |
| 2 |
|
| 3 |
options(digits=9) |
| 4 |
|
| 5 |
set.seed(12) |
| 6 |
m <- matrix(round(rnorm(25),2), 5, 5); m <- 1+ m + t(m); diag(m) <- diag(m) + 4 |
| 7 |
(mp <- posdefify(m)) |
| 8 |
(mp. <- posdefify(m, method = "allEV")) |
| 9 |
|
| 10 |
stopifnot(eigen(mp, only.val=TRUE)$values > 0, |
| 11 |
eigen(mp., only.val=TRUE)$values > 0, |
| 12 |
all.equal(diag(m), diag(mp), tol= 1e-15), |
| 13 |
all.equal(diag(m), diag(mp.),tol= 1e-15), |
| 14 |
T) |
| 15 |
|
| 16 |
## nearcor() |
| 17 |
pr <- matrix(c(1, 0.477, 0.644, 0.478, 0.651, 0.826, |
| 18 |
0.477, 1, 0.516, 0.233, 0.682, 0.75, |
| 19 |
0.644, 0.516, 1, 0.599, 0.581, 0.742, |
| 20 |
0.478, 0.233, 0.599, 1, 0.741, 0.8, |
| 21 |
0.651, 0.682, 0.581, 0.741, 1, 0.798, |
| 22 |
0.826, 0.75, 0.742, 0.8, 0.798, 1), |
| 23 |
nrow = 6, ncol = 6) |
| 24 |
|
| 25 |
nc. <- nearcor(pr, conv.tol = 1e-7) # default, 11 iter. |
| 26 |
ncr <- nearcor(pr, conv.tol = 1e-15) # 27-28 iterations (because of conv.tol)! |
| 27 |
ncr0 <- nearcor(pr, conv.tol = 1e-15, posd.tol = 0)# -> no posdefify step |
| 28 |
parts <- setdiff(names(nc.), "iterations") |
| 29 |
## IGNORE_RDIFF_BEGIN |
| 30 |
str(ncr) |
| 31 |
if(ncr$iterations != 27) { |
| 32 |
cat(sprintf("On this platform, the number of iterations (for tol 1e-15) is not 27, but %d;\n", |
| 33 |
ncr$iterations)) |
| 34 |
cat("sessionInfoX() :\n") |
| 35 |
print(sessionInfoX()) |
| 36 |
} |
| 37 |
## IGNORE_RDIFF_END |
| 38 |
str(ncr0[parts])# looks identical (with few digits accuracy) |
| 39 |
nr <- ncr$cor |
| 40 |
|
| 41 |
stopifnot(exprs = { |
| 42 |
nc.$iterations == 11 |
| 43 |
all.equal(nr[lower.tri(nr)], |
| 44 |
c(0.48796803265083, 0.64265188295401, 0.49063868812228, 0.64409905497094, |
| 45 |
0.80871120142824, 0.51411473401472, 0.25066882763262, 0.67235131534931, |
| 46 |
0.72583206922437, 0.59682778611131, 0.58219178154582, 0.7449631866236, |
| 47 |
0.72988206459063, 0.77215024062758, 0.81319175546212), tol = 1e-12) |
| 48 |
all.equal(ncr, ncr0, tolerance = 6e-6)# seen 6.6676e-7 |
| 49 |
}) |