| 1 |
|
| 2 |
R version 4.3.0 RC (2023-04-13 r84270) -- "Already Tomorrow" |
| 3 |
Copyright (C) 2023 The R Foundation for Statistical Computing |
| 4 |
Platform: x86_64-pc-linux-gnu (64-bit) |
| 5 |
|
| 6 |
R is free software and comes with ABSOLUTELY NO WARRANTY. |
| 7 |
You are welcome to redistribute it under certain conditions. |
| 8 |
Type 'license()' or 'licence()' for distribution details. |
| 9 |
|
| 10 |
R is a collaborative project with many contributors. |
| 11 |
Type 'contributors()' for more information and |
| 12 |
'citation()' on how to cite R or R packages in publications. |
| 13 |
|
| 14 |
Type 'demo()' for some demos, 'help()' for on-line help, or |
| 15 |
'help.start()' for an HTML browser interface to help. |
| 16 |
Type 'q()' to quit R. |
| 17 |
|
| 18 |
> library(sfsmisc) |
| 19 |
> |
| 20 |
> options(digits=9) |
| 21 |
> |
| 22 |
> set.seed(12) |
| 23 |
> m <- matrix(round(rnorm(25),2), 5, 5); m <- 1+ m + t(m); diag(m) <- diag(m) + 4 |
| 24 |
> (mp <- posdefify(m)) |
| 25 |
[,1] [,2] [,3] [,4] [,5] |
| 26 |
[1,] 2.040000000 1.559508625 -0.887340889 -0.408171836 -0.245682172 |
| 27 |
[2,] 1.559508625 4.360000000 -0.533157355 1.705637933 2.344838314 |
| 28 |
[3,] -0.887340889 -0.533157355 3.440000000 1.195027865 1.353283301 |
| 29 |
[4,] -0.408171836 1.705637933 1.195027865 6.020000000 0.577514304 |
| 30 |
[5,] -0.245682172 2.344838314 1.353283301 0.577514304 2.940000000 |
| 31 |
> (mp. <- posdefify(m, method = "allEV")) |
| 32 |
[,1] [,2] [,3] [,4] [,5] |
| 33 |
[1,] 2.040000000 1.378740895 -0.424598775 -0.386773282 -0.434470464 |
| 34 |
[2,] 1.378740895 4.360000000 -0.613091776 1.507019105 2.225434128 |
| 35 |
[3,] -0.424598775 -0.613091776 3.440000000 0.940298040 1.156766185 |
| 36 |
[4,] -0.386773282 1.507019105 0.940298040 6.020000000 0.277226191 |
| 37 |
[5,] -0.434470464 2.225434128 1.156766185 0.277226191 2.940000000 |
| 38 |
> |
| 39 |
> stopifnot(eigen(mp, only.val=TRUE)$values > 0, |
| 40 |
+ eigen(mp., only.val=TRUE)$values > 0, |
| 41 |
+ all.equal(diag(m), diag(mp), tol= 1e-15), |
| 42 |
+ all.equal(diag(m), diag(mp.),tol= 1e-15), |
| 43 |
+ T) |
| 44 |
> |
| 45 |
> ## nearcor() |
| 46 |
> pr <- matrix(c(1, 0.477, 0.644, 0.478, 0.651, 0.826, |
| 47 |
+ 0.477, 1, 0.516, 0.233, 0.682, 0.75, |
| 48 |
+ 0.644, 0.516, 1, 0.599, 0.581, 0.742, |
| 49 |
+ 0.478, 0.233, 0.599, 1, 0.741, 0.8, |
| 50 |
+ 0.651, 0.682, 0.581, 0.741, 1, 0.798, |
| 51 |
+ 0.826, 0.75, 0.742, 0.8, 0.798, 1), |
| 52 |
+ nrow = 6, ncol = 6) |
| 53 |
> |
| 54 |
> nc. <- nearcor(pr, conv.tol = 1e-7) # default, 11 iter. |
| 55 |
> ncr <- nearcor(pr, conv.tol = 1e-15) # 27-28 iterations (because of conv.tol)! |
| 56 |
> ncr0 <- nearcor(pr, conv.tol = 1e-15, posd.tol = 0)# -> no posdefify step |
| 57 |
> parts <- setdiff(names(nc.), "iterations") |
| 58 |
> ## IGNORE_RDIFF_BEGIN |
| 59 |
> str(ncr) |
| 60 |
List of 4 |
| 61 |
$ cor : num [1:6, 1:6] 1 0.488 0.643 0.491 0.644 ... |
| 62 |
$ fnorm : num 0.0744 |
| 63 |
$ iterations: num 27 |
| 64 |
$ converged : logi TRUE |
| 65 |
- attr(*, "class")= chr "nearcor" |
| 66 |
> if(ncr$iterations != 27) { |
| 67 |
+ cat(sprintf("On this platform, the number of iterations (for tol 1e-15) is not 27, but %d;\n", |
| 68 |
+ ncr$iterations)) |
| 69 |
+ cat("sessionInfoX() :\n") |
| 70 |
+ print(sessionInfoX()) |
| 71 |
+ } |
| 72 |
> ## IGNORE_RDIFF_END |
| 73 |
> str(ncr0[parts])# looks identical (with few digits accuracy) |
| 74 |
List of 3 |
| 75 |
$ cor : num [1:6, 1:6] 1 0.488 0.643 0.491 0.644 ... |
| 76 |
$ fnorm : num 0.0744 |
| 77 |
$ converged: logi TRUE |
| 78 |
> nr <- ncr$cor |
| 79 |
> |
| 80 |
> stopifnot(exprs = { |
| 81 |
+ nc.$iterations == 11 |
| 82 |
+ all.equal(nr[lower.tri(nr)], |
| 83 |
+ c(0.48796803265083, 0.64265188295401, 0.49063868812228, 0.64409905497094, |
| 84 |
+ 0.80871120142824, 0.51411473401472, 0.25066882763262, 0.67235131534931, |
| 85 |
+ 0.72583206922437, 0.59682778611131, 0.58219178154582, 0.7449631866236, |
| 86 |
+ 0.72988206459063, 0.77215024062758, 0.81319175546212), tol = 1e-12) |
| 87 |
+ all.equal(ncr, ncr0, tolerance = 6e-6)# seen 6.6676e-7 |
| 88 |
+ }) |
| 89 |
> |
| 90 |
> proc.time() |
| 91 |
user system elapsed |
| 92 |
0.120 0.044 0.210 |