Module 6 · Association · step 3 of 8
6.3Prepare covariates
Commands tested on Windows
In short
We prepare covariates, extra factors used to adjust the disease comparison: age and ten numbers describing patterns of genetic variation. We match them to sample IDs and check for missing values and columns that repeat information from other columns.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: 1,811 complete covariate rows that match the samples, with Age and PC1 to PC10.
Command
Module setup, needed before this step
R: module setup
source("R/guided.R")cfg <- guided_init()R command
Type or paste into the RStudio console.
1psam <- guided_table(paste0(guided_path(cfg, "5.7"), ".psam"))pcs <- guided_table(paste0(guided_path(cfg, "5.7", "pca"), ".eigenvec"))metadata <- guided_metadata(cfg)2a <- match(psam$IID, metadata$IID); b <- match(psam$IID, pcs$IID)stopifnot(!anyNA(a), !anyNA(b), !anyDuplicated(pcs$IID))covar <- data.frame(FID = psam$FID, IID = psam$IID, Age = as.numeric(metadata$Age[a]))for (name in paste0("PC", 1:10)) covar[[name]] <- as.numeric(pcs[[name]][b])stopifnot(!anyNA(covar))3design <- model.matrix(~ ., data = data.frame(SEX = as.numeric(psam$SEX), covar[, -(1:2)]))stopifnot(qr(design)$rank == ncol(design))names(covar)[1] <- "#FID"4guided_write(covar, guided_path(cfg, "6.3", "covariates.tsv"))cat(nrow(covar), "complete covariate rows written\n")# Course bookkeeping: records the counts for the next step. Do not change.guided_artifact_checkpoint(cfg, "6.3", guided_path(cfg, "6.3", "covariates.tsv"), parent = "6.1")- Note 1: Read sample details, genetic component scores and age records.
- Note 2: Match sample IDs and make a complete table of age and ten component scores.
- Note 3: Check that no model column repeats information from the other columns.
- Note 4: Save the covariate table and print its number of rows.
You need
The state saved at 5.7, the cohort eigenvectors (the PC values of each sample) from 5.7 and the metadata from 2.7.
What we do
Join age and ten principal components (PCs (opens in a new tab)) to the IDs of the retained samples. Then check that no value is missing and that the table has full rank (explained below).
Why
Covariates are extra measured factors, such as age. They adjust the comparison between cases and controls. PCs describe ancestry differences between people. If the covariates belong to the wrong samples, every test is wrong, yet the output still looks normal. A sample with a missing covariate is dropped from the model. Collinearity (opens in a new tab) means that one column can be predicted exactly from the other columns. Then the model cannot give a single answer. Full rank means that no column has this problem. We check the IDs and the rank before the scan. PLINK reads sex from the PSAM file and adds it to the model.
PLINK terminal equivalent. Optional: you do not need it for the course.
Not applicable. This step needs no PLINK operation. The R chunk is the complete command.
R reads or calculates the values it shows. It does not change the genotype files.
Expected result
Show the expected result
1,811 complete covariate rows that match the samples, with Age and PC1 to PC10. There are no duplicate or unmatched IDs. The design has full column rank.
The tested PLINK version is defined in config.R and recorded in
software_versions.txt. Validation: executable chunks passed on Windows; main test counts and significance match the expected values.
If something goes wrong
If something goes wrong
Stop at the first error. Check the input file named in the message and the previous step. Keep the command and the console log. Do not use files from a different version of the dataset. See troubleshooting (opens in a new tab).
Quick check
Why is a complete covariate table with the right number of rows not enough by itself?
Show the answer
The rows must belong to the correct sample IDs. Each row must match its own sample.
Files created
s6-3_covariates.tsv.