Background
This exercise illustrates how to calculate linkage disequilibrium (LD) among genetic markers.
Install and load R packages
We will analyze the mice
data included in the BGLR package. The genetics package includes the LD()
function to calculate \(D\).
rm(list=ls()) # delete the objects in the current environment
install.packages("BGLR")
install.packages("genetics")
library(BGLR)
library(genetics)
data(mice)
?mice
W <- mice.X[, 1:10] # use the first 10 markers
D
The makeGenotypes()
function converts columns in a dataframe to genotypes or haplotypes. The LD
function computes LD statistics.
?makeGenotypes
W2 <- makeGenotypes(W, convert=c(colnames(W)), method=as.genotype.allele.count)
?LD
myLD <- LD(W2)
names(myLD)
myLD$D
Alternative coding
W3 <- W # create a copy of W
# Convert to characters
W3[W3==0] <- "A/A"
W3[W3==1] <- "A/T"
W3[W3==2] <- "T/T"
W3
?makeGenotypes
W4 <- makeGenotypes(W3)
LD(W4)$r^2