\HeaderA{mroot}{Smallest square root of matrix}{mroot}
\keyword{models}{mroot}
\keyword{smooth}{mroot}
\keyword{regression}{mroot}
\begin{Description}\relax
Find a square root of a positive semi-definite matrix, 
having as few columns as possible. Uses either pivoted choleski 
decomposition or singular value decomposition to do this.
\end{Description}
\begin{Usage}
\begin{verbatim}
mroot(A,rank=NULL,method="chol")
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{A}] The positive semi-definite matrix, a square root of which is 
to be found.
\item[\code{rank}] if the rank of the matrix \code{A} is known then it should 
be supplied.
\item[\code{method}] \code{"chol"} to use pivoted choloeski decompositon, 
which is fast but tends to over-estimate rank. \code{"svd"} to use 
singular value decomposition, which is slow, but is the most accurate way 
to estimate rank.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The routine uses an LAPACK SVD routine, or the LINPACK pivoted 
Choleski routine. It is primarily of use for turning penalized regression 
problems into ordinary regression problems.
\end{Details}
\begin{Value}
A matrix, \eqn{ {\bf B}}{B} with as many columns as the rank of 
\eqn{ {\bf A}}{A}, and such that \eqn{ {\bf A} = {\bf BB}^\prime}{A=BB'}.
\end{Value}
\begin{Author}\relax
Simon N. Wood \email{simon.wood@r-project.org}
\end{Author}
\begin{Examples}
\begin{ExampleCode}
  set.seed(0)
  a <- matrix(runif(24),6,4)
  A <- a%*%t(a) ## A is +ve semi-definite, rank 4
  B <- mroot(A) ## default pivoted choleski method
  tol <- 100*.Machine$double.eps
  chol.err <- max(abs(A-B%*%t(B)));chol.err
  if (chol.err>tol) warning("mroot (chol) suspect")
  B <- mroot(A,method="svd") ## svd method
  svd.err <- max(abs(A-B%*%t(B)));svd.err
  if (svd.err>tol) warning("mroot (svd) suspect")  
\end{ExampleCode}
\end{Examples}

