| 1 |
% Copyright (2007) Jens Oehlschlägel |
| 2 |
% GPL licence, no warranty, use at your own risk |
| 3 |
% Copyright (2007-2010) Martin Maechler |
| 4 |
\name{nearcor} |
| 5 |
\alias{nearcor} |
| 6 |
\title{Find the Nearest Proper Correlation Matrix} |
| 7 |
\description{ |
| 8 |
This function \dQuote{smoothes} an improper correlation matrix as it can result |
| 9 |
from \code{\link{cor}} with \code{use="pairwise.complete.obs"} or |
| 10 |
\code{\link[polycor]{hetcor}}. |
| 11 |
|
| 12 |
It is \emph{deprecated} now, in favor of |
| 13 |
\code{\link[Matrix]{nearPD}()} from package \CRANpkg{Matrix}.% 2013-01-16 |
| 14 |
} |
| 15 |
\usage{ |
| 16 |
nearcor(R, eig.tol = 1e-6, conv.tol = 1e-07, posd.tol = 1e-8, |
| 17 |
maxits = 100, verbose = FALSE) |
| 18 |
} |
| 19 |
\arguments{ |
| 20 |
\item{R}{a square symmetric approximate correlation matrix} |
| 21 |
\item{eig.tol}{defines relative positiveness of eigenvalues compared |
| 22 |
to largest, default=1e-6.} |
| 23 |
\item{conv.tol}{convergence tolerance for algorithm, default=1.0e-7 } |
| 24 |
\item{posd.tol}{tolerance for enforcing positive definiteness, default=1.0e-8} |
| 25 |
\item{maxits}{maximum number of iterations} |
| 26 |
\item{verbose}{logical specifying if convergence monitoring should be |
| 27 |
verbose.} |
| 28 |
} |
| 29 |
\details{ |
| 30 |
This implements the algorithm of Higham (2002), then forces symmetry, |
| 31 |
then forces positive definiteness using code from |
| 32 |
\code{\link[sfsmisc]{posdefify}}. This implementation does not make |
| 33 |
use of direct LAPACK access for tuning purposes as in the MATLAB code |
| 34 |
of Lucas (2001). The algorithm of Knol DL and ten Berge (1989) (not |
| 35 |
implemented here) is more general in (1) that it allows contraints to |
| 36 |
fix some rows (and columns) of the matrix and (2) to force the |
| 37 |
smallest eigenvalue to have a certain value. |
| 38 |
} |
| 39 |
\value{ |
| 40 |
A \code{\link{list}}, with components |
| 41 |
\item{cor}{resulting correlation matrix} |
| 42 |
\item{fnorm}{Froebenius norm of difference of input and output} |
| 43 |
\item{iterations}{number of iterations used} |
| 44 |
\item{converged}{logical} |
| 45 |
} |
| 46 |
\references{See those in \code{\link[sfsmisc]{posdefify}}. |
| 47 |
} |
| 48 |
\author{ Jens Oehlschlägel } |
| 49 |
\seealso{the slightly more flexible \code{\link[Matrix]{nearPD}} which also |
| 50 |
returns a \emph{classed} matrix (class \code{dpoMatrix}). |
| 51 |
For new code, \code{nearPD()} is really preferred to \code{nearcor()}, |
| 52 |
which hence is considered deprecated. |
| 53 |
|
| 54 |
\code{\link[polycor]{hetcor}}, \code{\link{eigen}}; |
| 55 |
\code{\link[sfsmisc]{posdefify}} for a simpler algorithm. |
| 56 |
} |
| 57 |
\examples{ |
| 58 |
cat("pr is the example matrix used in Knol DL, ten Berge (1989)\n") |
| 59 |
pr <- matrix(c(1, 0.477, 0.644, 0.478, 0.651, 0.826, |
| 60 |
0.477, 1, 0.516, 0.233, 0.682, 0.75, |
| 61 |
0.644, 0.516, 1, 0.599, 0.581, 0.742, |
| 62 |
0.478, 0.233, 0.599, 1, 0.741, 0.8, |
| 63 |
0.651, 0.682, 0.581, 0.741, 1, 0.798, |
| 64 |
0.826, 0.75, 0.742, 0.8, 0.798, 1), |
| 65 |
nrow = 6, ncol = 6) |
| 66 |
|
| 67 |
ncr <- nearcor(pr) |
| 68 |
nr <- ncr$cor |
| 69 |
\dontshow{ |
| 70 |
stopifnot(all.equal(nr[lower.tri(nr)], |
| 71 |
c(0.487968018215891, 0.642651880010905, 0.490638670907082, 0.64409905308119, |
| 72 |
0.808711184549399, 0.514114729435273, 0.250668810831206, 0.672351311297071, |
| 73 |
0.725832055882792, 0.596827778712155, 0.582191779051908, 0.744963163381413, |
| 74 |
0.729882058012398, 0.772150225146827, 0.813191720191943))) |
| 75 |
} |
| 76 |
plot(pr[lower.tri(pr)], |
| 77 |
nr[lower.tri(nr)]); abline(0,1, lty=2) |
| 78 |
round(cbind(eigen(pr)$values, eigen(nr)$values), 8) |
| 79 |
|
| 80 |
cat("The following will fail:\n") |
| 81 |
try(factanal(cov=pr, factors=2)) |
| 82 |
cat("and this should work\n") |
| 83 |
try(factanal(cov=nr, factors=2)) |
| 84 |
|
| 85 |
if(require("polycor")) { |
| 86 |
|
| 87 |
n <- 400 |
| 88 |
x <- rnorm(n) |
| 89 |
y <- rnorm(n) |
| 90 |
|
| 91 |
x1 <- (x + rnorm(n))/2 |
| 92 |
x2 <- (x + rnorm(n))/2 |
| 93 |
x3 <- (x + rnorm(n))/2 |
| 94 |
x4 <- (x + rnorm(n))/2 |
| 95 |
|
| 96 |
y1 <- (y + rnorm(n))/2 |
| 97 |
y2 <- (y + rnorm(n))/2 |
| 98 |
y3 <- (y + rnorm(n))/2 |
| 99 |
y4 <- (y + rnorm(n))/2 |
| 100 |
|
| 101 |
dat <- data.frame(x1, x2, x3, x4, y1, y2, y3, y4) |
| 102 |
|
| 103 |
x1 <- ordered(as.integer(x1 > 0)) |
| 104 |
x2 <- ordered(as.integer(x2 > 0)) |
| 105 |
x3 <- ordered(as.integer(x3 > 1)) |
| 106 |
x4 <- ordered(as.integer(x4 > -1)) |
| 107 |
|
| 108 |
y1 <- ordered(as.integer(y1 > 0)) |
| 109 |
y2 <- ordered(as.integer(y2 > 0)) |
| 110 |
y3 <- ordered(as.integer(y3 > 1)) |
| 111 |
y4 <- ordered(as.integer(y4 > -1)) |
| 112 |
|
| 113 |
odat <- data.frame(x1, x2, x3, x4, y1, y2, y3, y4) |
| 114 |
|
| 115 |
xcor <- cor(dat) |
| 116 |
pcor <- cor(data.matrix(odat)) # cor() no longer works for factors |
| 117 |
hcor <- hetcor(odat, ML=TRUE, std.err=FALSE)$correlations |
| 118 |
ncor <- nearcor(hcor)$cor |
| 119 |
|
| 120 |
try(factanal(covmat=xcor, factors=2, n.obs=n)) |
| 121 |
try(factanal(covmat=pcor, factors=2, n.obs=n)) |
| 122 |
try(factanal(covmat=hcor, factors=2, n.obs=n)) |
| 123 |
try(factanal(covmat=ncor, factors=2, n.obs=n)) |
| 124 |
} |
| 125 |
} |
| 126 |
\keyword{algebra} |
| 127 |
\keyword{array} |