# NOT RUN {
## Get nEff from a genotype matrix
## Construct toy data
X <- matrix(c(0,1,2,1,0,1,1,0,2), nrow=3, byrow=TRUE) # genotype matrix
subpops <- c(1,1,2) # subpopulation assignments for individuals
## NOTE: for BED-formatted input, use BEDMatrix!
## "file" is path to BED file (excluding .bed extension)
# library(BEDMatrix)
# X <- BEDMatrix(file) # load genotype matrix object
## estimate the kinship matrix "Phi" from the genotypes "X"!
Phi <- popkin(X, subpops) # calculate kinship from X and optional subpop labels
w <- weightsSubpops(subpops) # can weigh individuals so subpopulations are balanced
# use kinship matrix to calculate nEff
# default mode returns maximum nEff possible across all non-negative weights that sum to one
# also returns the weights that were optimal
obj <- neff(Phi)
nEffMax <- obj$neff
wMax <- obj$w
# version that uses weights provided
obj <- neff(Phi, max=FALSE, w=w)
nEffW <- obj$neff
w <- obj$w # returns input weights renormalized for good measure
# no (or NULL) weights implies uniform weights
obj <- neff(Phi, max=FALSE)
nEffU <- obj$neff
w <- obj$w # uniform weights
# get nEff only, disregard weights used
nEffMax <- neff(Phi, retW=FALSE)
# }
Run the code above in your browser using DataLab