Last chance! 50% off unlimited learning
Sale ends in
The effective sample size (max=TRUE
, a calculation is performed that implicitly uses optimal weights which maximize nonneg=TRUE
and if the above solution has negative weights (common), optimal non-negative weights are found instead (there are three algorithms available, see algo
).
If max=FALSE
, then the input weights are used in this calculation, and if weights are NULL
, uniform weights are used.
n_eff(kinship, max = TRUE, weights = NULL, nonneg = TRUE,
algo = c("gradient", "newton", "heuristic"), tol = 1e-10)
An
If TRUE
, returns the maximum nonneg=TRUE
). If FALSE
,
Weights for individuals (optional). If NULL
, uniform weights are used. This parameter is ignored if max=TRUE
.
If TRUE
(default) and max=TRUE
, non-negative weights that maximize algo
. This has no effect if max=FALSE
.
Algorithm for finding optimal non-negative weights (applicable only if nonneg=TRUE
and max=TRUE
and the weights found by matrix inversion are non-negative).
May be abbreviated.
If "gradient" (default), an optimized gradient descent algorithm is used (fastest; recommended).
If "newton", the exact multivariate newton's Method is used (slowest since
Tolerance parameter for "gradient" and "newton" algorithms. The algorithms converge when the norm of the step vector is smaller than this tolerance value.
A list containing n_eff
and weights
(optimal weights if max = TRUE
, input weights otherwise).
The maximum
# NOT RUN {
# Get n_eff 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 "kinship" from the genotypes "X"!
kinship <- popkin(X, subpops) # calculate kinship from X and optional subpop labels
weights <- weights_subpops(subpops) # can weigh individuals so subpopulations are balanced
# use kinship matrix to calculate n_eff
# default mode returns maximum n_eff possible across all non-negative weights that sum to one
# also returns the weights that were optimal
obj <- n_eff(kinship)
n_eff_max <- obj$n_eff
w_max <- obj$weights
# version that uses weights provided
obj <- n_eff(kinship, max = FALSE, weights = weights)
n_eff_w <- obj$n_eff
w <- obj$weights # returns input weights renormalized for good measure
# no (or NULL) weights implies uniform weights
obj <- n_eff(kinship, max = FALSE)
n_eff_u <- obj$n_eff
w <- obj$weights # uniform weights
# }
Run the code above in your browser using DataLab