predict_gt_pca: Predict scores of a PCA

predict.gt_pcaR Documentation

Predict scores of a PCA

Description

Predict the PCA scores for a gt_pca, either for the original data or projecting new data.

Usage

## S3 method for class 'gt_pca'
predict(
  object,
  new_data = NULL,
  project_method = c("least_squares", "simple", "plink", "OADP", "none"),
  lsq_pcs = NULL,
  block_size = NULL,
  n_cores = 1,
  as_matrix = TRUE,
  ...
)

Arguments

object

the gt_pca object

new_data

a gen_tibble if scores are requested for a new dataset

project_method

the projection method: "least_squares" (the default, as done by smartpca), "simple", "plink" (as PLINK 1.9), "OADP" (Online Augmentation, Decomposition, and Procrustes projection) or "none"; see Details.

lsq_pcs

a vector of the indices of the principal components to use for the least squares fitting (and to return). Only relevant if project_method = "least_squares". It defaults to all the components of the PCA.

block_size

number of loci read simultaneously (larger values will speed up computation, but require more memory); by default, blocks of about 128 MB.

n_cores

number of cores

as_matrix

logical, whether to return the result as a matrix (default) or a tibble.

...

no used

Details

New individuals are projected on the loadings of the PCA, with their genotypes centred and scaled as those of the PCA. The methods differ in how they deal with missing genotypes:

  • "least_squares" (the default): for each individual, the least squares fit of its standardised genotypes on the loadings of the loci genotyped in that individual (as lsqproject in smartpca). Individuals with many missing genotypes are not shrunk towards the centre of the PCA. With complete genotypes, it gives the same scores as "simple". Individuals with too few genotyped loci to fit the components get NA, with a warning. As with "simple" and "OADP", imputed genotypes are used only if the gen_tibble is set to use them (see gt_set_imputed()).

  • "simple": the product of the standardised genotypes, with missing genotypes set to 0 (the mean), by the loadings. An individual with a proportion f of missing genotypes is shrunk towards 0 by a factor of about 1 - f (e.g. to a fifth of its distance with 80% missing genotypes), so it should only be used with complete or imputed data.

  • "plink": the simple projection divided by the proportion of loci genotyped in each individual, which is how PLINK 1.9 projects the individuals outside the clusters of --pca-cluster-names (or --pca-clusters). It removes most of the shrinkage of "simple". PLINK standardises the genotypes with the allele frequencies of all the individuals in its run, reference and projected, whereas predict() uses those of the PCA (see gt_pca() to choose them): the scores are the same as PLINK's when the PCA uses the same frequencies, and PLINK's .eigenvec values are these scores divided by the singular values (object$d). Individuals with no genotyped locus get NA.

  • "OADP": the Online Augmentation, Decomposition, and Procrustes projection (Zhang et al. 2020), which corrects the shrinkage of projections with many more loci than individuals; missing genotypes are set to 0, as for "simple", with the same shrinkage.

  • "none": the product of the genotypes by the loadings with bigstatsr::big_prodMat(); it needs complete or imputed genotypes (and uses imputed genotypes automatically).

In a simulation of individuals with 20-80% missing genotypes projected on complete reference individuals, "least_squares" had no shrinkage and the smallest error, "simple" and "OADP" shrank individuals by their proportion of missing genotypes, and imputing the genotypes (by their mode) before a simple projection only halved the shrinkage. "plink" was close to "least_squares" with the frequencies of the reference individuals, and had a larger error with those of all the individuals, as PLINK does.

Value

a matrix of predictions (in line with predict using a prcomp object) or a tibble, with samples as rows and components as columns. The number of components depends on how many were estimated in the gt_pca object.

References

Zhang et al (2020). Fast and robust ancestry prediction using principal component analysis. Bioinformatics 36(11): 3439–3446.

Patterson N., Price A.L. and Reich D. (2006) Population structure and eigenanalysis. PLoS Genetics 2: e190.

Privé F., Luu K., Blum M.G.B., McGrath J.J. and Vilhjálmsson B.J. (2020) Efficient toolkit implementing best practices for principal component analysis of population genetic data. Bioinformatics 36: 4449-4457 (for the implementation of OADP in bigsnpr, which "OADP" uses).

Examples



# Create a gen_tibble of lobster genotypes
bed_file <-
  system.file("extdata", "lobster", "lobster.bed", package = "tidypopgen")
lobsters <- gen_tibble(bed_file,
  backingfile = tempfile("lobsters"),
  quiet = TRUE
)

# Remove monomorphic loci and impute
lobsters <- lobsters %>% select_loci_if(loci_maf(genotypes) > 0)
lobsters <- gt_impute_simple(lobsters, method = "mode")

# Subset into two datasets: one original and one to predict
original_lobsters <- lobsters[c(1:150), ]
new_lobsters <- lobsters[c(151:176), ]

# Create PCA object
pca <- gt_pca(original_lobsters)

# Predict
predict(pca, new_data = new_lobsters, project_method = "simple")

# Predict as PLINK 1.9
predict(pca, new_data = new_lobsters, project_method = "plink")

# Predict with OADP
predict(pca, new_data = new_lobsters, project_method = "OADP")

# Predict with least squares (the default), on the first three components
predict(pca, new_data = new_lobsters, lsq_pcs = c(1, 2, 3))

# Return a tibble
predict(pca, new_data = new_lobsters, as_matrix = FALSE)

# Adjust block.size
predict(pca, new_data = new_lobsters, block_size = 10)


tidypopgen documentation built on Nov. 28, 2026, 1:07 a.m.