% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/estimate_R.R
\name{estimate_R}
\alias{estimate_R}
\title{Estimate Covariance Matrix of Study Z-scores}
\usage{
estimate_R(Z, median = TRUE, tol = 1e-08)
}
\arguments{
\item{Z}{Matrix of z-scores, loci along rows, \code{S} studies along columns.  Missing values can be present and are appropriately ignored.}

\item{median}{If \code{TRUE}, uses \code{\link[=rho_from_median]{rho_from_median()}} to calculate covariance values from sample medians; otherwise, sample means are used.}

\item{tol}{Determinant tolerance for singularity test}
}
\value{
The \code{S}-by-\code{S} covariance matrix.
}
\description{
The default "median" estimator is expected to be more robust to outliers (present due to causal variants and linkage disequilibrium).
For completeness, the more standard "mean" estimator is also implemented, which is better when there are no outliers.
The determinant is also tested, and if its absolute value is less than \code{tol} an error is triggered, warning the user that perhaps the same study was included multiple times (as this is otherwise expected to be extremely rare).
}
\examples{

# simulate some correlated Z-scores for 3 studies and `m` loci
m <- 10000
# true correlation matrix
R <- matrix(
  c( 1.0, 0.2, 0.0,
     0.2, 1.0, 0.1,
     0.0, 0.1, 1.0 ),
  nrow = 3, ncol = 3
)
# use Cholesky decomposition to simulate via affine transform
L <- chol( R )
Z <- matrix( rnorm( m * 3 ), nrow = m, ncol = 3 ) \%*\% L

# calulate estimates
estimate_R( Z )
estimate_R( Z, median = FALSE )
# compare to naive covariance estimate, which does not assume zero mean for z-scores
cov( Z, use = 'pairwise.complete.obs' )

}
\seealso{
\code{\link[=rho_from_median]{rho_from_median()}}
}
