Tutorials › Statistical Genetics › Preparing Relationship Matrices For Downstream Models

Relatedness and Kinship Estimation · Tutorial 179 of 989

Preparing Relationship Matrices For Downstream Models

Prepare a relationship matrix that is internally consistent, correctly ordered, explicit about its scale, and traceable to sample IDs.

Intermediate 10 min read

What You'll Learn

  • Check matrix dimensions, symmetry, diagonal values, and numerical validity
  • Match matrix rows and columns to the intended sample order
  • Distinguish missing relationship estimates from estimates equal to zero
  • Identify the relationship-matrix scale and convert it only when appropriate
  • Export the matrix with a reproducible sample-ID mapping

Why Matrix Preparation Matters

A genomic relationship matrix is not ready for analysis simply because its entries have been estimated. A matrix can contain plausible-looking values and still be unusable because its row order differs from the phenotype file, its scale is undocumented, or a missing pairwise estimate has been silently treated as zero. Such problems can change which relationship is assigned to which people or alter the meaning of a model parameter.

Preparation is therefore a set of checks on the matrix as a data object: its dimensions and symmetry, the interpretation of its diagonal, the order and identity of samples, the presence of missing or non-finite entries, and the scale expected by the intended software. The goal is not to force every matrix to have the same numerical appearance. It is to ensure that the matrix supplied downstream is the one intended, with its conventions made explicit.

Definition: A genomic relationship matrix (GRM) is a square matrix whose \((i,j)\) entry summarizes estimated additive genetic relationship between samples \(i\) and \(j\), under a specified estimator and scale convention. The row and column labels identify the samples; entry values alone do not identify them.

Establish the Matrix and Its Scale

Write down the matrix definition or software convention before checking whether the values look reasonable. For example, one common centered-genotype construction uses \(x_{ij}\in\{0,1,2\}\), the allele count for sample \(i\) at marker \(j\), and the allele frequency \(p_j\). Let \(Z_{ij}=x_{ij}-2p_j\). With one commonly used normalization,

$$ G=\frac{ZZ^\mathsf{T}}{2\sum_j p_j(1-p_j)}. $$

This specifies one particular estimator and scale; other software and estimators can use different centering, marker weights, denominators, or handling of missing genotypes. Consequently, the numerical diagonal is not required to equal one in every valid GRM. Its interpretation depends on the estimator and data. The earlier material on interpreting GRM diagonals is useful for assessing their biological and estimator-specific meaning; here the practical rule is to check that the diagonal is finite and plausible under the documented construction, not to impose a universal target.

A second convention issue is whether the matrix is reported on a relationship scale or a kinship-coefficient scale. Under a common additive convention, a kinship matrix \(\Phi\) is half the corresponding relationship matrix \(G\), so that \(G=2\Phi\). This conversion is valid only when the two quantities use compatible definitions. A file called “kinship” is not enough evidence that dividing or multiplying by two is appropriate: check the estimator and software documentation, then record the convention alongside the exported file.

Worked Example: Computing a Small Matrix and Checking Its Diagonal

Suppose three hypothetical samples have allele counts at two markers as follows. The counts by sample are A: \((0,0)\), B: \((1,1)\), and C: \((2,0)\). Use the centered-genotype construction above, with marker frequencies estimated from these three samples.

At the first marker, the total allele count is \(3\) out of \(6\), so \(p_1=1/2\). At the second marker, the total is \(1\) out of \(6\), so \(p_2=1/6\). Thus the centered rows are A: \((-1,-1/3)\), B: \((0,2/3)\), and C: \((1,-1/3)\). The denominator is

$$ 2\{p_1(1-p_1)+p_2(1-p_2)\} =2\left\{\frac14+\frac{5}{36}\right\} =\frac79. $$

For example, the A–B numerator is \((-1)(0)+(-1/3)(2/3)=-2/9\), giving \(G_{AB}=(-2/9)/(7/9)=-2/7\). The A diagonal numerator is \((-1)^2+(-1/3)^2=10/9\), giving \(G_{AA}=(10/9)/(7/9)=10/7\). Carrying out the same dot products gives

$$ G= \begin{pmatrix} 10/7 & -2/7 & -8/7\\ -2/7 & 4/7 & -2/7\\ -8/7 & -2/7 & 10/7 \end{pmatrix}. $$

The matrix is symmetric, and its diagonal values are finite. They are not all one, which is not by itself an error: this is a small, frequency-dependent example using a particular normalization. The negative off-diagonal values are also not automatically errors; centered genomic relationship estimates can be negative relative to the reference population. A check should flag a value for investigation when it conflicts with the documented estimator or data, not merely because it is unfamiliar.

Core Integrity Checks

Perform basic checks before exporting the matrix. First confirm that it has \(n\) rows and \(n\) columns for the intended \(n\) samples, and that each row and column has exactly one sample label. Check that every entry is numeric and finite: values such as infinity or “not a number” are not usable relationship estimates.

Next check symmetry. A relationship between samples \(i\) and \(j\) should be represented consistently in both positions, \(G_{ij}\) and \(G_{ji}\). For an estimator constructed from a shared matrix product such as \(ZZ^\mathsf{T}\), symmetry follows from the transpose identity. In stored numeric files, tiny discrepancies can arise from rounding or computation. A practical check compares the largest absolute difference \(|G_{ij}-G_{ji}|\) with a stated tolerance. If differences exceed the tolerance, investigate the estimator, export, or indexing rather than silently averaging entries.

Check the diagonal separately. Each \(G_{ii}\) describes the sample’s estimated relationship to itself under the chosen convention. It should be finite and compatible with the estimator, genotype processing, and any treatment of inbreeding. Do not replace the diagonal with one just to make the matrix look familiar. If a diagonal is unexpectedly extreme, inspect the sample’s genotype data, missingness, marker set, and calculation method before deciding how to proceed.

Positive semidefiniteness is another useful structural check when the matrix is intended as a covariance-like relationship matrix. For the common construction above, let \(c=2\sum_j p_j(1-p_j)>0\). For any vector \(v\),

$$ v^\mathsf{T}Gv =\frac{v^\mathsf{T}ZZ^\mathsf{T}v}{c} =\frac{(Z^\mathsf{T}v)^\mathsf{T}(Z^\mathsf{T}v)}{c} =\frac{\|Z^\mathsf{T}v\|^2}{c}\geq 0. $$

Thus this construction is positive semidefinite in exact arithmetic. In computation, very small negative eigenvalues may reflect numerical rounding; substantial negative eigenvalues require investigation. Other estimators, especially those using pair-specific marker sets or denominators, do not automatically inherit this guarantee. A failed check should be interpreted in light of the estimator rather than “fixed” without understanding why it failed.

Worked Example: Testing Symmetry and a Tolerance

A hypothetical exported matrix, with samples A, B, and C, is

$$ \widetilde G= \begin{pmatrix} 1.08 & 0.20 & -0.10\\ 0.2000003 & 0.92 & 0.05\\ -0.10 & 0.05 & 1.01 \end{pmatrix}. $$

Suppose the pre-specified symmetry tolerance is \(10^{-6}\). The largest mirrored-entry difference is \(|0.20-0.2000003|=0.0000003=3\times10^{-7}\), which is below the tolerance. The matrix therefore passes this numerical symmetry check. Its three diagonal entries are \(1.08\), \(0.92\), and \(1.01\); they are finite, but whether these values are plausible must be judged against the construction and data, not against a rule that all diagonals equal one. The small discrepancy may be due to rounding, but the tolerance and the check should still be recorded.

Sample Identity and Ordering

A matrix’s row and column order is part of its meaning. If the phenotype table uses a different order, matching by position rather than by ID assigns relationships to the wrong individuals. Confirm that IDs are unique, nonempty, and consistently formatted across the matrix and analysis data. Watch for duplicated IDs, leading zeros removed by software, whitespace, and family or cohort prefixes that distinguish otherwise similar IDs.

If the same samples appear in different orders, reorder both matrix dimensions using the same ID-based permutation. If \(P\) is the permutation matrix representing that reorder, the aligned matrix is \(PGP^\mathsf{T}\): rows and columns are permuted together. Reordering only rows, or only columns, does not preserve the intended sample pairs.

Worked Example: Aligning a Matrix to Phenotype Order

A hypothetical matrix has row and column order A, B, C, while the phenotype file is ordered C, A, B. Suppose the matrix is

$$ G= \begin{pmatrix} 1.1 & 0.2 & -0.1\\ 0.2 & 0.9 & 0.05\\ -0.1 & 0.05 & 1.0 \end{pmatrix}. $$

The phenotype order corresponds to old matrix indices \((3,1,2)\). Applying that same index sequence to rows and columns gives

$$ G_{\text{aligned}}= \begin{pmatrix} G_{33} & G_{31} & G_{32}\\ G_{13} & G_{11} & G_{12}\\ G_{23} & G_{21} & G_{22} \end{pmatrix} = \begin{pmatrix} 1.0 & -0.1 & 0.05\\ -0.1 & 1.1 & 0.2\\ 0.05 & 0.2 & 0.9 \end{pmatrix}. $$

The first row and column now refer to C, the second to A, and the third to B. For instance, the C–A entry remains \(-0.1\), now found in position \((1,2)\). This check is successful only if the saved labels confirm the new order; a correct-looking numeric matrix without its IDs is not enough to verify alignment.

Missing Entries and Scale Checks

Distinguish missing genotype data used to estimate relationships from missing entries in the finished matrix. Genotype missingness may be handled in several estimator-specific ways. But an NA in the matrix means that the corresponding sample pair has no recorded estimate; it does not mean that the estimated relationship is zero. Replacing NA with zero invents information and can make samples with unestimated relationships appear unrelated.

If any matrix entries are missing, identify which pairs are affected and why. Depending on the estimator and analysis requirements, a defensible response may be to recompute using a consistent marker set, apply a documented estimator-specific missing-data method, or exclude a sample when justified. A pair-specific denominator may give different pairs estimates based on different marker sets; this should be understood and documented, and can affect matrix properties such as positive semidefiniteness. Do not fill missing values simply to make a file rectangular: the matrix is already rectangular, and the question is what the absent estimate means.

Finally, document the scale as part of the data, not as a guess made later. Record the estimator, normalization, allele-frequency reference, marker set, treatment of missing genotypes, and whether the entries are relationships or kinship coefficients. If converting by a factor such as two, apply the conversion to every entry and retain the original matrix or a reproducible record of the conversion. Multiplying only off-diagonal entries, or converting a matrix without confirming compatible definitions, produces an incoherent scale.

Worked Example: Detecting a Missing Pair and Recording Scale

Suppose a hypothetical three-sample matrix has \(G_{AB}=0.12\), \(G_{BA}=\text{NA}\), and \(G_{AC}=0.00\). The first pair is not verified as symmetric: the missing value \(G_{BA}\) cannot be assumed to equal \(0.12\). The A–C value of zero, by contrast, is a recorded estimate and must not be confused with missingness.

Suppose the estimator documentation confirms that all available entries are on the relationship scale and the intended output is a compatible kinship-coefficient matrix, with \(\Phi=G/2\). Then the known values convert as \(0.12/2=0.06\) for A–B and \(0.00/2=0.00\) for A–C. The missing B–A entry remains missing until resolved; it does not become zero through scaling. Before export, the analyst must investigate the missing pair and use a justified, documented response. The scale conversion does not repair the missingness or establish symmetry.

Export for Reproducibility

A reliable export contains or accompanies enough information to reconstruct the sample correspondence. Preserve the exact ordered sample-ID list, and ensure that the matrix’s row and column order matches it. Use stable identifiers rather than row numbers alone. Save the estimator and scale description, marker and sample inclusion rules, missingness handling, software settings, and any reordering or conversion performed. If the matrix file format stores no labels, export a separate mapping file that specifies the ID for each matrix index.

A concise audit record should state the number of samples, the ID order or mapping file, matrix dimensions, symmetry tolerance and result, missing-entry count, diagonal summary, scale convention, and any structural checks performed. This record makes it possible to detect a later mismatch and to distinguish an intentional convention from an accidental file transformation.

1
Identify the matrix definition.
Record the estimator, normalization, reference frequencies, missing-genotype handling, and relationship or kinship scale.
2
Validate the entries.
Check dimensions, numeric and finite values, symmetry within a stated tolerance, diagonals, missing entries, and structural properties appropriate to the estimator.
3
Align by sample ID.
Verify unique matching IDs and apply any required permutation to rows and columns together.
4
Export with an audit trail.
Save the ordered ID mapping, matrix, conventions, checks, and all transformations needed to reproduce the delivered file.

Common Mistake and Key Takeaway

A common mistake is to assume that a matrix is ready because it is square and has values on its diagonal. For example, someone might reason, “The diagonal is not one, so I will set it to one; the missing pair is probably zero; and the phenotype file can use its own row order.” Each step changes or obscures information. The correction is to keep the matrix tied to its estimator: assess its diagonal under the documented convention, resolve missingness rather than inventing values, and align rows and columns by the same verified sample IDs.

Key takeaway: A prepared GRM is not just a square array. It is a validated set of pairwise estimates with an explicit scale, a verified sample order, transparent handling of missing entries, and an auditable ID mapping.

Check Your Understanding

Use the checks in this tutorial to answer each question.

  1. Why is a diagonal value different from one not, by itself, evidence that a GRM is invalid?
  2. A matrix has a small difference between \(G_{AB}\) and \(G_{BA}\). What should be checked before deciding whether it passes a symmetry check?
  3. What can go wrong if only the rows, but not the columns, are reordered to match a phenotype file?
  4. Why is it incorrect to replace a missing pairwise estimate with zero without justification?
  5. Under a compatible convention where \(G=2\Phi\), what happens to a recorded relationship value of \(0.18\) when expressed as a kinship coefficient? What happens to an NA entry?
  6. Name two pieces of information, in addition to the numeric matrix, that make an exported GRM auditable.
Clinical Biostats