R Under development (unstable) (2024-01-31 r85845 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 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. 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. > library(sfsmisc) > > options(digits=9) > > set.seed(12) > m <- matrix(round(rnorm(25),2), 5, 5); m <- 1+ m + t(m); diag(m) <- diag(m) + 4 > (mp <- posdefify(m)) [,1] [,2] [,3] [,4] [,5] [1,] 2.040000000 1.559508625 -0.887340889 -0.408171836 -0.245682172 [2,] 1.559508625 4.360000000 -0.533157355 1.705637933 2.344838314 [3,] -0.887340889 -0.533157355 3.440000000 1.195027865 1.353283301 [4,] -0.408171836 1.705637933 1.195027865 6.020000000 0.577514304 [5,] -0.245682172 2.344838314 1.353283301 0.577514304 2.940000000 > (mp. <- posdefify(m, method = "allEV")) [,1] [,2] [,3] [,4] [,5] [1,] 2.040000000 1.378740895 -0.424598775 -0.386773282 -0.434470464 [2,] 1.378740895 4.360000000 -0.613091776 1.507019105 2.225434128 [3,] -0.424598775 -0.613091776 3.440000000 0.940298040 1.156766185 [4,] -0.386773282 1.507019105 0.940298040 6.020000000 0.277226191 [5,] -0.434470464 2.225434128 1.156766185 0.277226191 2.940000000 > > stopifnot(eigen(mp, only.val=TRUE)$values > 0, + eigen(mp., only.val=TRUE)$values > 0, + all.equal(diag(m), diag(mp), tol= 1e-15), + all.equal(diag(m), diag(mp.),tol= 1e-15), + T) > > ## nearcor() > pr <- matrix(c(1, 0.477, 0.644, 0.478, 0.651, 0.826, + 0.477, 1, 0.516, 0.233, 0.682, 0.75, + 0.644, 0.516, 1, 0.599, 0.581, 0.742, + 0.478, 0.233, 0.599, 1, 0.741, 0.8, + 0.651, 0.682, 0.581, 0.741, 1, 0.798, + 0.826, 0.75, 0.742, 0.8, 0.798, 1), + nrow = 6, ncol = 6) > > nc. <- nearcor(pr, conv.tol = 1e-7) # default, 11 iter. > ncr <- nearcor(pr, conv.tol = 1e-15) # 27-28 iterations (because of conv.tol)! > ncr0 <- nearcor(pr, conv.tol = 1e-15, posd.tol = 0)# -> no posdefify step > parts <- setdiff(names(nc.), "iterations") > ## IGNORE_RDIFF_BEGIN > str(ncr) List of 4 $ cor : num [1:6, 1:6] 1 0.488 0.643 0.491 0.644 ... $ fnorm : num 0.0744 $ iterations: num 27 $ converged : logi TRUE - attr(*, "class")= chr "nearcor" > if(ncr$iterations != 27) { + cat(sprintf("On this platform, the number of iterations (for tol 1e-15) is not 27, but %d;\n", + ncr$iterations)) + cat("sessionInfoX() :\n") + print(sessionInfoX()) + } > ## IGNORE_RDIFF_END > str(ncr0[parts])# looks identical (with few digits accuracy) List of 3 $ cor : num [1:6, 1:6] 1 0.488 0.643 0.491 0.644 ... $ fnorm : num 0.0744 $ converged: logi TRUE > nr <- ncr$cor > > stopifnot(exprs = { + nc.$iterations == 11 + all.equal(nr[lower.tri(nr)], + c(0.48796803265083, 0.64265188295401, 0.49063868812228, 0.64409905497094, + 0.80871120142824, 0.51411473401472, 0.25066882763262, 0.67235131534931, + 0.72583206922437, 0.59682778611131, 0.58219178154582, 0.7449631866236, + 0.72988206459063, 0.77215024062758, 0.81319175546212), tol = 1e-12) + all.equal(ncr, ncr0, tolerance = 6e-6)# seen 6.6676e-7 + }) > > proc.time() user system elapsed 0.17 0.04 0.20